处理“接受 Cookie"在 Python 中使用 Selenium 弹出窗口 [英] Handling "Accept Cookies" popup with Selenium in Python

查看:100
本文介绍了处理“接受 Cookie"在 Python 中使用 Selenium 弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用 selenium 抓取这个房地产网站的一些信息.但是,当我访问该网站时,我需要接受 cookie 才能继续.这仅在机器人访问网站时发生,而不是在我手动执行时发生.当我尝试通过 xpath 或 id 查找相应的元素时,正如我在手动检查页面时找到的那样,我收到以下错误.

I've been trying to scrape some information of this real estate website with selenium. However when I access the website I need to accept cookies to continue. This only happens when the bot accesses the website, not when I do it manually. When I try to find the corresponding element either by xpath or id, as I find it when I inspect the page manually I get following error.

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{方法":xpath",选择器":///*[@id="uc-btn-accept-banner"]"}

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="uc-btn-accept-banner"]"}

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC



PATH = "/usr/bin/chromedriver"
driver = webdriver.Chrome(PATH)

driver.get("https://www.immoweb.be/en/search/house/for-sale?countries=BE&page=1&orderBy=relevance")
driver.find_element_by_xpath('//*[@id="uc-btn-accept-banner"]').click()

有谁知道如何解决这个问题?为什么我找不到元素?

Does anyone know how to get around this? Why can't I find the element?

下面是接受 cookie 弹出窗口的图像.

Below is an image of the accept cookies popup.

这是与继续浏览"按钮对应的 HTML.XPATH 同上.

This is the HTML corresponding to the button "Keep browsing". The XPATH is as above.

<button aria-label="Keep browsing" id="uc-btn-accept-banner" class="uc-btn-new  uc-btn-accept">Keep browsing
          <span id="uc-optin-timer-display"></span></button>

推荐答案

你们很亲近!

如果您在新浏览器中打开您的页面,您会注意到页面已完全加载,然后,稍后会出现您的弹出窗口.

If you open your page in a new browser you'll note the page fully loads, then, a moment later your popup appears.

selenium 中的默认等待策略就是页面被加载.页面加载和显示之间的绘制延迟导致您的脚本失败.

The default wait strategy in selenium is just that the page is loaded. That draw delay between page loaded and display appearing is causing your scripts to fail.

您有两个不错的同步选项.

You have two good synchronisation options.

1/包括对驱动程序的隐式等待.这对每个脚本执行一次并影响所有对象.这会在抛出任何错误之前等待 10 秒,或者在它准备好后继续:

1/ Include an implicit wait for your driver. This is done once per script and affects all objects. This waits 10 seconds before throwing any error, or continues when it's ready:

PATH = "/usr/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.implicitly_wait(10)

driver.get("https://www.immoweb.be/en/search/house/for-sale?countries=BE&page=1&orderBy=relevance")
driver.find_element_by_xpath('//*[@id="uc-btn-accept-banner"]').click()

2/仅对您的对象进行显式等待:

2/ Do a explicit wait on your object only:

PATH = "/usr/bin/chromedriver"
driver = webdriver.Chrome(PATH)

driver.get("https://www.immoweb.be/en/search/house/for-sale?countries=BE&page=1&orderBy=relevance")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="uc-btn-accept-banner"]'))).click()

关于等待策略的更多信息是这里

More info on the wait strategies is here

这篇关于处理“接受 Cookie"在 Python 中使用 Selenium 弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆