Selenium webdriver找不到比DOM更晚的元素

发布于 2025-02-05 17:26:54 字数 1115 浏览 1 评论 0原文

我一直在尝试使用webcrap 此页面使用Selenium,并且您可能会在第一个负载上注意到弹出窗口似乎是关于同意饼干的。此弹出窗口左右出现在DOM的其余部分。 问题是,即使添加睡眠WebDriverWait驱动程序仍然无法单击弹出窗口的“接受”按钮。

检查页面给出了元素的XPath/html/html/hody/div/div/div [2]/div [4]/button [1],所以我尝试执行它是:

driver.get("https://www.planespotters.net/airlines")
time.sleep(5)
driver.find_element(By.XPATH, '/html/body/div/div[2]/div[4]/button[1]').click()

无效。我知道该元素不存在。

我尝试了一下:

driver.execute_script('document.querySelector("#notice > div.message-component.message-row.button-container > button.message-component.message-button.no-children.focusable.primary.sp_choice_type_11").click()')

以及

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div[2]/div[4]/button[1]'))).click()

有关如何绕过此弹出窗口的任何建议?

I've been trying to webscrap this page using selenium, and as you may notice on the first load a pop-up appears about agreeing on cookies. This pop-up appears around a second later that the rest of the DOM.
The problem is, that even after adding a sleep or WebDriverWait the driver still cannot click on the Accept button from the pop-up.

Inspecting the page gives that the Xpath of the element is /html/body/div/div[2]/div[4]/button[1] so I tried doing it as:

driver.get("https://www.planespotters.net/airlines")
time.sleep(5)
driver.find_element(By.XPATH, '/html/body/div/div[2]/div[4]/button[1]').click()

with no effect. I get that the element does not exist.

I tried it even with:

driver.execute_script('document.querySelector("#notice > div.message-component.message-row.button-container > button.message-component.message-button.no-children.focusable.primary.sp_choice_type_11").click()')

and

WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '/html/body/div/div[2]/div[4]/button[1]'))).click()

Any suggestions on how to bypass this pop-up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我纯我任性 2025-02-12 17:26:54

如果您在DevTools上滚动,您会注意到您的模态窗口位于iFrame中。我询问了“ Planespotter”标头图像(因为它是最重要的项目),您可以清楚地看到它:

首先要切换到您的iframe-这会找到URL包含“隐私”的iFrame:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'privacy')]")))

然后您可以完成操作。请确保也将同步保留在那里。

然后,您可以切换回主窗口并进行测试

driver.switch_to_default_content()

If you scroll up on devtools you'll notice your modal window is in an iframe. I queried the 'planespotters' header image (because it's the top item) and you can see it quite clearly:
devtoolsandplanespottsersite

For selenium switch to your iframe first - this finds the iframe wher the url contains 'privacy':

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'privacy')]")))

Then you can complete your action. Be sure to keep that sync in there too.

Then you can switch back to your main window and carry in with your test

driver.switch_to_default_content()
西瓜 2025-02-12 17:26:54

您可以尝试引入隐式等待驱动程序。每个脚本都会发生一次,并会影响所有对象。这将等待10秒钟,然后再丢失,或者在准备就绪时继续进行:

driver.implicitly_wait(10)

You can try introducing an implicit wait for your driver. This will happen once per script and will affect all objects. This will wait 10 seconds before throwing an error, or continues whenever it is ready:

driver.implicitly_wait(10)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文