处理/接受Selenium中的Cookie弹窗

huangapple go评论81阅读模式
英文:

Handle/Accept cookie pop-up in Selenium

问题

package seleniumTestPack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.Cookie;

@SuppressWarnings("unused")
public class firstSelTest {
    public static void main(String[] args) throws InterruptedException {
        ChromeOptions options = new ChromeOptions();

        // 添加 Chrome 参数以禁用通知 --disable-notifications
        options.addArguments("--disable-notifications");

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\vmyna\\Downloads\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver(options);

        driver.get("https://www.zyyah.com/homeowner-lifestyle-perfected-home-value-protected/");

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

        driver.switchTo().frame(0);
        driver.getWindowHandles();

        driver.switchTo().alert().accept();
        By cookies_accept = By.xpath("//*[@id='cookie-law-info-bar']");
        By cookies_gotIt = By.xpath("//*[@id='cookie_action_close_header']");
        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();

        driver.findElement(By.xpath("//*[@id='et-boc']/div/div/div[4]/div/div/div[2]/div[1]")).click();

        Thread.sleep(10000);
        driver.quit();
    }
}
英文:

I am trying to 'accept the cookies' on the homepage, but my code doesn't work. I tried to get the new window handles and then identify the subsequent Xpath for the frame and Accept button but it never work.

package seleniumTestPack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.Cookie;
@SuppressWarnings("unused")
public class firstSelTest {
public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
//Add chrome switch to disable notification - "**--disable-notifications**"
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\vmyna\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.zyyah.com/homeowner-lifestyle-perfected-home-value-protected/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.switchTo().frame(0);
driver.getWindowHandles();
driver.switchTo().alert().accept();
By cookies_accept = By.xpath("//*[@id=\"cookie-law-info-bar\"]");
By cookies_gotIt = By.xpath("//*[@id=\"cookie_action_close_header\"]");
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();
driver.findElement(By.xpath("//*[@id=\'et-boc\']/div/div/div[4]/div/div/div[2]/div[1]")).click();
Thread.sleep(10000);
driver.quit();
}
}

答案1

得分: 0

你的情况下无需切换框架,因为你的页面中没有框架。只需检查“接受Cookies”并点击。

driver.get("https://www.zyyah.com/homeowner-lifestyle-perfected-home-value-protected/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("cookie_action_close_header")).click();

切换到框架的使用:

https://www.browserstack.com/guide/handling-frames-in-selenium

警告框的使用:

https://www.browserstack.com/guide/alerts-and-popups-in-selenium

英文:

You no need to switch frame in your case, because there is no frame present in your page. Just inspect the "Accept Cookies" and click on that.

driver.get("https://www.zyyah.com/homeowner-lifestyle-perfected-home-value-protected/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.id("cookie_action_close_header")).click();

Use of Switch to Frame:

https://www.browserstack.com/guide/handling-frames-in-selenium

Use of Alert:

https://www.browserstack.com/guide/alerts-and-popups-in-selenium

答案2

得分: 0

以下是翻译好的代码部分:

WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String url = "https://www.zyyah.com/homeowner-lifestyle-perfected-home-value-protected/";
Driver.get(url);
Driver.findElement(By.id("cookie_action_close_header")).click();
System.out.println("completed");
英文:

The code below worked for me. "Accept Cookies" button is not under any pop-window. So no frame or pop-up window is present here. Here, we just need to locate "Accept Cookies" button and click on it.

	WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String url = "https://www.zyyah.com/homeowner-lifestyle-perfected-home-value-protected/";
Driver.get(url);
Driver.findElement(By.id("cookie_action_close_header")).click();
System.out.println("completed");

答案3

得分: 0

WebElement cookieAccept = driver.findElement(By.id("gdpr-banner-accept"));

// 显式等待
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));

wait.until(ExpectedConditions.visibilityOf(cookieAccept));

wait.until(ExpectedConditions.elementToBeClickable(cookieAccept));

cookieAccept.click();
英文:
		WebElement cookieAccept = driver.findElement(By.id("gdpr-banner-accept"));
// Explicit Wait
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
wait.until(ExpectedConditions.visibilityOf(cookieAccept));
wait.until(ExpectedConditions.elementToBeClickable(cookieAccept));
cookieAccept.click();

huangapple
  • 本文由 发表于 2020年8月21日 13:14:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63516881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定