单击python selenium中的选定元素无法使用WebDriverWait / EC /

时间:2018-05-27 09:46:37

标签: python python-3.x selenium selenium-chromedriver

这是我关注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脚本的独立会话。

为什么以前的工作似乎不是更标准的做法呢?

1 个答案:

答案 0 :(得分:1)

iframe是主DOM的节点。致电driver.switch_to_frame(captcha_iframe)后,您切换到了iframe&#39; <{1}}所在的DOM和节点不再可访问。

因此,如果要跳过By.XPATH, iframe_xpath

,它仍然可以从主DOM访问