硒GDPR NoSuchElementException

问题描述

我想从“ https://www.techadvisor.co.uk/review/wearable-tech/”中抓取一些数据。我发现使用Beautifulsoup遍历页面不起作用。这就是为什么我尝试用硒打开它的原因。无法找到克服GDPR阻止程序的“全部接受”按钮。

我尝试过:

browser = webdriver.Chrome()
browser.get("https://www.techadvisor.co.uk/review/wearable-tech/")
# button = browser.find_element_by_xpath('/html/body/div/div[3]/div[5]/button[2]')
# webdriverwait(browser,20).until(EC.element_to_be_clickable((By.XPATH,"html/body/div/div[3]/div[5]/button[2]"))).click()

我总是收到NoSuchElementException

说实话,我发现Xpath确实很奇怪,但是我是从Google Chrome浏览器中得到的。

每个解决方案的建议或技巧都值得赞赏:)

解决方法

要单击Accept All内的iframe按钮。您需要先切换到iframe才能单击按钮。

得出WebDriverWait()并等待frame_to_be_available_and_switch_to_it()并使用以下CSS选择器。

产生WebDriverWait()并等待element_to_be_clickable()并使用以下xpath选择器。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Chrome()
browser.get("https://www.techadvisor.co.uk/review/wearable-tech/")
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='sp_message_iframe']")))
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//button[text()='Accept All']"))).click()
,

我知道这个问题很老了, 但我想提供自己的解决方案! 第一步是识别您实际查看的表单的“ id”,然后您需要将重点放在表单上!

driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="gdpr-consent-notice"]'))

    cookies = driver.find_element_by_xpath('/html/body/app-root/app-theme/div/div/app-notice/app-theme/div/div/app-home/div/div[3]/div[2]/a[3]/span')

    cookies.click()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...