繁体   English   中英

我如何接受特定站点上的 cookie?

[英]How can I accept cookies on a particular site?

在用 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 横幅并从屏幕上清除它们

为了获得更具体的答案,我建议发布HTML页面源,其中包括cookie接受按钮的HTML。

根据我接受Cookie的经验,您可能不得不将弹出窗口视为警报:

driver.switchTo().alert().accept;

使用以下代码接受两个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();
package com.selenium_abcd;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class cookiesDelete {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");
        WebDriver dr = new ChromeDriver();
        dr.manage().deleteAllCookies();
        dr.manage().deleteCookieNamed("Cookie name");
        

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM