在 java 中使用 selenium 我想点击 cookie 的 "Accept" 按钮

Using selenium in java I want to click on the "Accept" button of the cookie

使用 java selenium 我想按下“接受按钮”。我使用了定位器,如:cssselectorxpathrelpathtagclick() 方法,但没有用。 第二种方法是使用 DeleteAllCookieDeleteCookieByNameAddCoockie 获取 cookie,但没有用。

我认为可以使用 Thread.sleep,但我不知道如何识别元素,也不知道如何编写脚本,所以我可以单击那个“接受”。我认为 selenium java 无法识别该按钮。

可能当您加载网页时,cookie 策略横幅尚未出现,几秒钟后出现。因此,当您尝试查找它不存在的元素时。 但别担心,Selenium 有一个工具,你必须以编程方式等待元素出现,语法如下:

WebDriverWait w = new WebDriverWait(driver, 600);
w.until(ExpectedConditions.presenceOfElementLocated(
        By.cssSelector("button[attribute='value']")));

WebElement cookieButton = driver.findElement(By.cssSelector("button[attribute='value']"));

cookieButton.click()

基本上你创建一个新的 WebDriverWait 实例并将驱动程序和你想要等待的最大毫秒数作为参数传递,然后你使用你喜欢的选择器使用 until 方法设置等待条件。