硒GDPR非接触性异常

2024-10-02 02:28:19 发布

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

我想从中获取一些数据“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()

我总是不例外

老实说,我发现Xpath真的很奇怪,但我是从Google Chrome inspect得到的

感谢每一个解决方案建议或提示:)


Tags: httpsdivbrowserhtmlwwwbodybuttonelement
2条回答

我知道这个问题很老, 但我想提供我自己的解决方案! 第一步是识别您实际查看的表单的“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()

单击iframe内的Accept All按钮。需要先切换到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()

相关问题 更多 >

    热门问题