gpt4 book ai didi

java - 我如何接受特定网站上的 cookie?

转载 作者:行者123 更新时间:2023-12-02 09:31:05 25 4
gpt4 key购买 nike

在使用 Selenium Java 为招聘网站编写基本搜索测试时,我在尝试接受网站上显示的 cookie 警告时遇到了问题。

该网站有 2 个 cookie 通知,中间层和顶层横幅相互重叠。

如果有任何建议(我是 Selenium Java 的新手!),我将不胜感激,这些建议可以让我克服这个有点令人恼火(但很小)的问题。

这是我使用的代码,但没有效果(两个 cookie 横幅仍然保留):

我尝试了下面详细介绍的 xpath 方法

import java.util.Arrays;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Keys;
import java.util.concurrent.TimeUnit;

importnet.bytebuddy.agent.builder.AgentBuilder.RedefinitionStrategy.DiscoveryStrategy.Explicit;//这些是从通过项目级别构建路径>外部库提供的Selenium包导入的

public class Demo4SeleniumTypeAndClickXPathExperis {

public static void main(String[] args) {


System.setProperty("webdriverchrome.driver", "C:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();


driver.get("https://www.experis.co.uk/");//Browser URL

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//这里是有问题的项目,似乎没有取得任何结果

driver.findElement(By.xpath("//button[@title='Accept Cookies']")).submit();


driver.findElement(By.xpath("//*[@id=\"query\"]")).sendKeys("test or tester or qa");
driver.findElement(By.xpath("//*[@id=\"search\"]/span/div/div[1]/input")).clear();
driver.findElement(By.xpath("//*[@id=\"search\"]/span/div/div[1]/input")).sendKeys("Bristol");
driver.findElement(By.xpath("//*[@id=\"search\"]/span/div/div[1]/input")).sendKeys(Keys.RETURN);
driver.findElement(By.xpath("//*[@id=\"search\"]/span/div/div[1]/input")).submit();

driver.findElement(By.xpath("//*[@id=\"search\"]/span/div/div[1]/input")).submit();
//driver.close();




}

private static Object navigate() {
// TODO Auto-generated method stub
return null;

我希望能够接受 cookie 横幅并将其从屏幕上清除

最佳答案

使用下面的代码接受 cookie 警报。我已在您在代码中提供的 URL 上测试了代码。

    By cookies_accept = By.xpath("//*[@title='Accept Cookies']");
By cookies_gotIt = By.xpath("//a[text()='Got it!']");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(cookies_accept)).click();
wait.until(ExpectedConditions.invisibilityOfElementLocated(cookies_accept));
wait.until(ExpectedConditions.elementToBeClickable(cookies_gotIt)).click();

关于java - 我如何接受特定网站上的 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57971604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com