Reputation: 601
I'm trying to locate an element using python selenium, and have the following code:
zframe = driver.find_element_by_xpath("/html/frameset/frameset/frame[5]")
driver.switch_to.frame(zframe)
findByXpath("/html/body/form/table/tbody/tr/td[2]/label[3]").click()
element = driver.find_element_by_xpath("//*[@id='awdType']")
I'm getting the error that:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='awdType']"} (Session info: chrome=59.0.3071.115)
Any ideas why it may not be able to locate this element? I used the exact xpath by copying it and also switched frames. Thanks!
Upvotes: 0
Views: 5218
Reputation: 2513
The problem occurs because awdType
is loaded by ajax or jquery.
You should use selenium Waits. There is two type of waits explicit and implicit.Avoid using implicit wait.
# Explicit wait example
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver,20)
element = wait.until(EC.element_to_be_clickable((By.ID, 'awdType')))
OR
# implicit wait example
driver.implicitly_wait(10) # seconds
element = driver.find_element_by_xpath("//*[@id='awdType']")
Upvotes: 2