在Python Selenium中找不到元素

2024-10-01 13:38:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用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: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='awdType']"} (Session info: chrome=59.0.3071.115)

你知道为什么它找不到这个元素吗?我通过复制和交换帧来使用精确的xpath。谢谢!在


Tags: to定位id元素byhtmldriverselenium
1条回答
网友
1楼 · 发布于 2024-10-01 13:38:11

问题的发生是因为awdType是通过ajax或jquery加载的。 您应该使用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')))

或者

^{pr2}$

相关问题 更多 >