pythonseleniumxpathcss-selectorswebdriverwait

My selenium program fails to find element


I am trying to perform a web automatization with python and selenium in chrome. The thing is im trying to locate a button which has not id or class name.

The xpath is:

 //*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]

And the html code is

<span class="SectionMethod" onclick="window.location.href=&quot;explorer/explorer.aspx?root=user&quot;;" style="cursor:pointer;text-decoration:underline;color:CadetBlue;">Open</span>

It's a button called open, but there are other buttons like that one with the same text and class so i cant locate by text.

This is my code:

from selenium import webdriver


driver = webdriver.Chrome(chrome_options=chromeOptions, desired_capabilities=chromeOptions.to_capabilities())

driver.get("..............") 


driver.find_element_by_xpath('//*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]')

This is the error i am getting:

NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Form1"}
  (Session info: chrome=75.0.3770.100)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.16299 x86_64).

Solution

  • It's likely you are looking for an element before it has loaded. As per the example from the documentation

    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
    
    driver = webdriver.Firefox()
    driver.get("http://somedomain/url_that_delays_loading")
    try:
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "myDynamicElement"))
        )
    finally:
        driver.quit()
    

    In your case:

    EC.presence_of_element_located((By.ID, "myDynamicElement"))
    

    Will be

    EC.presence_of_element_located((By.XPATH, '//*[@id="Form1"]/table[1]/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td/span[1]'))