Java Selenium Webdriver:如何绕过谷歌;“接受饼干”;对话框

Java Selenium Webdriver:如何绕过谷歌;“接受饼干”;对话框,java,selenium,selenium-webdriver,browser-automation,selenium-firefoxdriver,Java,Selenium,Selenium Webdriver,Browser Automation,Selenium Firefoxdriver,我在Java中有一个非常简单的SeleniumWebDriver项目,我正在使用FireFox驱动程序 我的目标是导航到Google的页面()并在提示时接受 Cookie可以点击“我同意”按钮来摆脱它,并继续进一步的自动化过程。但由于某些原因,我无法让浏览器找到它 这是我目前使用的说明: package main; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.sel

我在Java中有一个非常简单的SeleniumWebDriver项目,我正在使用FireFox驱动程序

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

这是我目前使用的说明:

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()方法似乎不是 在这种情况下,两种方法都可以

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




是因为你不能用SeleniumWebDriver自动化一些东西,还是我误解了一些重要的概念


非常感谢所有提示

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

driver.switchTo().frame(yourFrame);
找到“接受”按钮并单击后:

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

我使用了与JU相同的解决方案,但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.
从selenium导入webdriver
从selenium.common.Exception导入NoTouchElementException
导入时间
driver=webdriver.Chrome('chromedriver.exe')
司机,上车https://google.com/xhtml')
时间。睡眠(2)#秒,直到弹出窗口出现
尝试:#2个不同的弹出窗口

frame=driver。通过xpath('/*[@id=“cnsw”]/iframe')查找元素?#也许谷歌希望你主动同意他们的条款,并有意构建他们的界面以要求人机交互。但你的意思是它可以阻止Selenium WebDriver实际执行其指令吗?谁知道呢?它内部可能类似于“不是机器人”。尽管如此,这不是一个确定的/专家的意见,其他人可能应该回答。非常感谢!这解决了我一段时间以来一直在挣扎的问题。我只能通过索引找到iframe,因为页面源代码无法显示任何与之相关的id或name属性。Yw:)也可以工作,或者您可以尝试使用源代码属性查找iframe,可能类似这样:
driver.findElements(by.xpath(//iframe[startswith(getAttribute(“src”),“approvement.google.com”))
非常感谢,我会尝试一下。欢迎使用Stack Overflow,避免问只包含代码的问题,您可以通过快速总结您要做的事情来改进您的问题,这将使用户更容易理解您的问题
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.