简体   繁体   中英

Unable to locate element in Python Selenium

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!

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']")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM