該應用程式將加載系統默認瀏覽器,加載一個特殊的網站,然后自動登錄
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");
uj5u.com熱心網友回復:
要在元素上單擊(),您需要為elementToBeClickable()誘導WebDriverWait,您可以使用以下任一定位器策略:
選擇器:
Driver.get("http://www.playok.com/en/spades/"); new WebDriverWait(Driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.ckbut.but0"))).click();路徑:
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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/425788.html
