Python 如何使用Selenium与cookies模式交互?

Python 如何使用Selenium与cookies模式交互?,python,selenium,Python,Selenium,我正在寻找自动点击这个页面,我正在使用它作为测试,因为我只是在学习Selenium 当我加载页面时,我无法让我的代码自动接受并关闭cookies覆盖,这将阻止我获得更多信息 我尝试了各种方法来识别元素,但每次都收到相同的消息 我相信这是我需要识别的元素的HTML: <a class="call" tabindex="0" role="button">Agree and Proceed</a> 错误消息是: <a class="call" tabindex="0"

我正在寻找自动点击这个页面,我正在使用它作为测试,因为我只是在学习Selenium

当我加载页面时,我无法让我的代码自动接受并关闭cookies覆盖,这将阻止我获得更多信息

我尝试了各种方法来识别元素,但每次都收到相同的消息

我相信这是我需要识别的元素的HTML:

<a class="call" tabindex="0" role="button">Agree and Proceed</a>
错误消息是:

<a class="call" tabindex="0" role="button">Agree and Proceed</a>

隐藏cookie横幅的最简单方法是显示gdrp合规性。当您接受cookie模式时,请在浏览器上为您的系统写入cookie

你可以做同样的事情。打开浏览器后,编写此cookie并重新加载浏览器。然后,cookie模式将不会在功能中打开

self.webdriver.add_cookie({'name' : 'notice_gdpr_prefs', 'value' : '0,1,2:', 'domain' : self.store['base'] + url})
        self.webdriver.add_cookie({'name' : 'notice_preferences', 'value' : '2:', 'domain' : self.store['base'] + url})
我对python cookie编写没有太多的了解。您可以按照本教程编写cookie


在要测试的URL中,cookie位于iframe中。为了点击这个iframe中的某个东西,首先你必须找到iframe。然后必须切换到iframe。之后,您可以返回默认页面

frame_reference = driver.find_element_by_class_name("gwt-Frame")
driver.switch_to.frame(frame_reference)
然后找到您的元素(同意并继续)并单击它

然后:


有一个iframe,其标题为“信任Cookie许可管理器”。您需要首先切换到iframe来访问元素

诱导
WebDriverWait
frame\u变为可用,并切换为可用
使
WebDriverWait
元素可点击

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()
url = "https://www.pgatour.com/competition/2019/safeway-open/leaderboard.html"
browser.get(url)
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,'//iframe[@title="TrustArc Cookie Consent Manager"]')))
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//a[@class='call'][text()='Agree and Proceed']"))).click()
浏览器快照:

关闭按钮,请在关闭后添加此代码

WebDriverWait(browser,5).until(EC.element_to_be_clickable((By.XPATH,"//a[@id='gwt-debug-close_id'][@class='close']"))).click()
WebDriverWait(browser,5).until(EC.element_to_be_clickable((By.XPATH,"//a[@id='gwt-debug-close_id'][@class='close']"))).click()