使用WebDriverWait/EC/by方式,点击Python Selenium中的选定元素不起作用

2024-10-02 00:31:25 发布

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

这就是我关注的文件http://selenium-python.readthedocs.io/waits.html

引发异常:

driver = webdriver.Chrome(CHROME_DRIVER_PATH)
iframe_xpath = "//*[@id='targetForCaptcha1']/div/div/iframe"
captcha_iframe = driver.find_element_by_xpath(iframe_xpath)

driver.switch_to_frame(captcha_iframe)
checkBox = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, iframe_xpath)))
checkBox.click()
raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

但这是可行的:

driver = webdriver.Chrome(CHROME_DRIVER_PATH)
iframe_xpath = "//*[@id='targetForCaptcha1']/div/div/iframe"
captcha_iframe = driver.find_element_by_xpath(iframe_xpath)

action=ActionChains(driver)
action.move_to_element(captcha_iframe)
action.click().perform()

以上两个会话是从零开始运行python脚本的独立会话

为什么以前的工作似乎是更标准的工作方式


Tags: pathdividdriverseleniumactionelementchrome
1条回答
网友
1楼 · 发布于 2024-10-02 00:31:25

Iframe是主DOM的一个节点。在调用driver.switch_to_frame(captcha_iframe)之后,您切换到iframes的DOM,By.XPATH, iframe_xpath所在的节点不再可访问

因此,如果要跳过driver.switch_to_frame(captcha_iframe)行,应该仍然可以从主DOM访问它

相关问题 更多 >

    热门问题