无法在Python Selenium中找到元素

时间:2017-08-03 06:25:52

标签: python selenium xpath frame

我正在尝试使用python selenium找到一个元素,并且具有以下代码:

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

我收到的错误是:

  

selenium.common.exceptions.NoSuchElementException:消息:没有这样的   element:无法定位元素:   {“method”:“xpath”,“selector”:“// * [@ id ='awdType']”}(会话信息:   铬= 59.0.3071.115)

为什么它可能无法找到此元素的任何想法?我通过复制它并使用切换帧来使用精确的xpath。谢谢!

1 个答案:

答案 0 :(得分:1)

出现此问题的原因是ajax或jquery加载了awdType。 您应该使用selenium Waits.有两种类型的显式和隐式等待。避免使用隐式等待。

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