【问题标题】:Selenium Webdriver: How to bypass Google "accept-cookies" dialog boxSelenium Webdriver:如何绕过 Google 的“接受 cookie”对话框
【发布时间】:2021-04-24 03:36:26
【问题描述】:

我有一个非常简单的 Java Selenium WebDriver 项目,我在其中使用 FireFox 驱动程序。

我的目标是导航到 Google 的页面 (https://www.google.com) 并在提示接受时 Cookies 可以点击“我同意”按钮来摆脱它并进一步继续自动化过程。但由于某种原因,我无法让浏览器找到它。

这是我目前使用的指令:

package main;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SeleniumGoogleTest {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.com");
        WebElement acceptButton = driver.findElement
        (By.xpath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/div[2]"));
        
    
    }
}


我不知道为什么浏览器无法找到它并激活/启用页面的那部分 既没有隐式等待也没有显式等待。 Thread.sleep() 方法似乎不是 这种情况下的解决方案。

我在运行应用程序时收到的唯一错误消息是“无法定位元素”。




是您实际上无法使用 Selenium WebDriver 自动化某些东西,还是我在这里误解了一些重要概念?

非常感谢所有提示!

【问题讨论】:

  • 也许 Google 希望您主动同意他们的条款并故意构建他们的界面以要求人工交互。
  • 但是你的意思是它可以阻止 Selenium WebDriver 实际执行它的指令吗?
  • 谁知道?它可能在内部类似于“不是机器人”。不过,这不是一个确定的/专家意见,其他人可能应该回答。

标签: java selenium selenium-webdriver browser-automation selenium-firefoxdriver


【解决方案1】:

弹出窗口位于 iFrame 上,首先您必须切换到 iFrame:

driver.switchTo().frame(yourFrame);

找到接受按钮后,点击:

driver.findElement(By.id("id")).click();

【讨论】:

  • 非常感谢!这解决了我一段时间以来一直在努力解决的问题。我只能按索引找到 iframe,因为页面源无法显示任何与其相关的 id 或 name 属性。
  • Yw :) 这也可以工作,或者您可以尝试使用源属性查找 iFrame,可能是这样的:driver.findElements(By.xpath("//IFrame[startswith(getAttribute("src"),"consent.google.com")"));
【解决方案2】:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Chrome('chromedriver.exe')

driver.get('https://google.com/xhtml')

time.sleep(2) # seconds until popup appears

try: # 2 different popups
    frame = driver.find_element_by_xpath('//*[@id="cnsw"]/iframe')
    driver.switch_to.frame(frame)
    driver.find_element_by_xpath('//*[@id="introAgreeButton"]').click()

except NoSuchElementException:
    driver.find_element_by_xpath('//*[@id="zV9nZe"]').click()

【讨论】:

  • 欢迎来到 Stack Overflow,避免提出仅包含代码的问题,您可以通过快速总结您正在尝试做的事情来改进您的问题,这将使用户更容易更好地理解您的问题
【解决方案3】:

我使用了与 Jus 相同的解决方案,但 Chrome 从那时起更改了“我同意”按钮 ID

更新后的解决方案应该是这样的

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
driver = webdriver.Chrome('chromedriver.exe')

driver.get('https://google.com/xhtml')

time.sleep(2) # seconds until popup appears

try: # 2 different popups
    frame = driver.find_element_by_xpath('//*[@id="cnsw"]/iframe') #<-locating chrome cookies consent frame 
    driver.switch_to.frame(frame) 
    driver.find_element_by_xpath('//*[@id="introAgreeButton"]').click()#<-looking for introAgreeButton button, but seems google has changed its name since and  it only works in old chrome versions.

except NoSuchElementException:
    driver.find_element_by_xpath('//*[@id="L2AGLb"]').click() #<- pay attention to new id.

如果这个 id 过期,我建议你像这样自己检查元素:

Inspect Element

点击两次“检查元素”

Locating 'I agree' button's id

【讨论】:

    【解决方案4】:

    您可以通过更新 cookie 来处理它“同意”删除旧的,因为值为“PENDING”并使用以下值添加它。

    driverManager.driver.manage().deleteCookieNamed ("CONSENT");
    driverManager.driver.manage().addCookie(new Cookie("CONSENT","YES+shp.gws-"+LocalDate.now().toString().replace("-","")+"-0-RC2.en+FX+374"));
    driverManager.driver.navigate().refresh();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 2021-12-08
      相关资源
      最近更新 更多