2

I'm just a beginner with programming and I'm trying to learn how to automate web procedures. I am working based on https://www.coeweb.istat.it/.

I need to find elements of the page and I already tried with driver.find_element_by_xpath but since I'm not very familiar with html code, I'm not even sure to be using the right piece of code as argument for that method. Can someone please send me as an example of code to click on the link qui. in bold right in the middle of the page please? This is what I've tried:

from selenium import webdriver
from selenium.webdriver.common.by import By
url = "https://www.coeweb.istat.it/"
driver = webdriver.Chrome()
driver.get(url)
link = driver.find_element_by_xpath('//[@id="AutoNumber1"]/tbody/tr[3]/td[2]/p[3]/font/a/strong')
link.click()

Still, I get the error as:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="AutoNumber1"]/tbody/tr[3]/td[2]/p[3]/font/a/strong"}
1
  • Try with this css selector a[href="lnkdati.htm"] or skip clicking the button entirely and just navigate to the link with driver.get("https://www.coeweb.istat.it/lnkdati.htm") Commented Jul 5, 2018 at 15:53

2 Answers 2

2

You have to wait until it will be clickable:

# I have fixed xpath
element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//*[@id='AutoNumber1']//a[contains(., 'qui.')]"))
    )
element.click()

Note: you have to do some imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

There is also possible to use CSS Selector to locate the element:

#AutoNumber1 > tbody > tr:nth-child(3) > td:nth-child(2) > p:nth-child(3) > font > a

and

element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.CSS_SELECTOR, "#AutoNumber1 > tbody > tr:nth-child(3) > td:nth-child(2) > p:nth-child(3) > font > a"))
    )
element.click()

EDIT: you have to switch to the frame before locate your element like this:

driver.switch_to.frame(driver.find_element_by_xpath("//frame[@name = 'principale']"))

So the full code will be like this:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = "https://www.coeweb.istat.it/"
driver = webdriver.Chrome()
driver.get(url)

driver.switch_to.frame(driver.find_element_by_xpath("//frame[@name = 'principale']")) # switches to frame
element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//*[@id='AutoNumber1']//a[contains(., 'qui.')]"))
    )
element.click()
driver.switch_to.default_content() # switch back to default content

Explanation: you cannot interact with the elements which are in iframe or in a frame. To be able to do this, you have to find a frame, switch to it, do stuff and then switch back to default content

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks I tried but this is what i get now "selenium.common.exceptions.TimeoutException: Message: ". This is what I typed: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC url = "coeweb.istat.it" driver = webdriver.Chrome() driver.get(url) element = WebDriverWait(driver, 5).until( EC.element_to_be_clickable((By.XPATH, '//[@id="AutoNumber1"]/tbody/tr[3]/td[2]/p[3]/font/a/strong')) ) element.click()
I have fixed xPath, please try one more time
I tried again but I still get "selenium.common.exceptions.TimeoutException: Message:". I don't really know why.
I have found the problem and added the working code, please have a look. If you are satisfied with my answer, please mark it by clicking on the grey check button below downvote button.
Sorry I'm new and not very practical with the site.. I must have clicked two times
2

As per the URL you have shared the link with text as qui. is withan a <frame>. So you have to switch to the desired frame inducing WebDriverWait and then again induce WebDriverWait while looking out for the element as follows:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    url = "https://www.coeweb.istat.it/"
    driver.get(url)
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"principale")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//table[@id='AutoNumber1']//tr//td//p//a[@href='lnkdati.htm' and .//strong[contains(.,'qui.')]]"))).click()
    
  • Browser Snapshot:

www_coeweb_istat_it

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.