如何使用 Selenium 自动接受来自 java 应用程序的 cookie 弹出窗口
How to automate accept cookies pop-up from java app using Selenium
应用程序将加载系统默认浏览器,加载一个特殊的网站,然后自动登录
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class theurl {
public static void main(String[] args) {
String url = "http://www.playok.com/en/spades/";
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
但在尝试登录之前,首先应该自动接受 cookie;有没有一种简单的方法而不是使用外部库?如果没有,哪个图书馆可以完成这项工作
我试过这段代码,但没有帮助:
WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String url = "http://www.playok.com/en/spades/";
Driver.get(url);
Driver.findElement(By.id("cookie_action_close_header")).click();
System.out.println("completed");
至 on the element you need to induce for the and you can use either of the following :
cssSelector:
Driver.get("http://www.playok.com/en/spades/");
new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.ckbut.but0"))).click();
xpath:
Driver.get("http://www.playok.com/en/spades/");
new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='ckbut but0' and text()='ACCEPT']"))).click();
应用程序将加载系统默认浏览器,加载一个特殊的网站,然后自动登录
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class theurl {
public static void main(String[] args) {
String url = "http://www.playok.com/en/spades/";
if(Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
try {
desktop.browse(new URI(url));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
}else{
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("xdg-open " + url);
} catch (IOException e) {
e.printStackTrace();
}
}
}
但在尝试登录之前,首先应该自动接受 cookie;有没有一种简单的方法而不是使用外部库?如果没有,哪个图书馆可以完成这项工作
我试过这段代码,但没有帮助:
WebDriver Driver = new ChromeDriver();
Driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
String url = "http://www.playok.com/en/spades/";
Driver.get(url);
Driver.findElement(By.id("cookie_action_close_header")).click();
System.out.println("completed");
至
cssSelector:
Driver.get("http://www.playok.com/en/spades/"); new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.ckbut.but0"))).click();
xpath:
Driver.get("http://www.playok.com/en/spades/"); new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='ckbut but0' and text()='ACCEPT']"))).click();