Reputation: 128
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="explorer/explorer.aspx?root=user";" 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).
Upvotes: 1
Views: 322
Reputation: 5774
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]'))
Upvotes: 3
Reputation: 193308
Presumably you are trying to click()
on the <span>
element with text as Open and to achieve that you have to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following solutions:
Using CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.SectionMethod[onclick*='explorer/explorer']"))).click()
Using XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='SectionMethod' and contains(@onclick,'explorer/explorer')][text()='Open']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Additionally, as you are using chrome=75.0.3770.100
you need to update ChromeDriver to ChromeDriver 75.0.3770.90 (2019-06-13)
Upvotes: 1
Reputation: 649
Your correct xpath is:
//span[@class='SectionMethod' and text() = 'Open']
Upvotes: 1