简体   繁体   English

无法在Python Selenium中找到元素

[英]Unable to locate element in Python Selenium

I'm trying to locate an element using python selenium, and have the following code: 我正在尝试使用python硒定位元素,并具有以下代码:

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) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // * [@ id ='awdType']”}(会话信息:铬= 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. 我通过复制和切换帧使用了确切的xpath。 Thanks! 谢谢!

The problem occurs because awdType is loaded by ajax or jquery. 发生此问题是因为awdType是由ajax或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']")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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