我正在尝试使用HtmlUnitDriver在后台运行我的脚本,但它每次都抛出Nosuchelementexection,但它获取了当前的url。
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example{
public static void main(String[] args) {
WebDriver driver = new HtmlUnitDriver(true);
driver.get("http://www.google.com");
String url=driver.getCurrentUrl();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
System.out.println(url);
String Text=driver.findElement(By.id("gbqfba")).getText();
System.out.println(Text);
driver.findElement(By.xpath("//*[@id='gbqfba']")).getSize().getHeight();
driver.findElement(By.xpath("//*[@id='gbqfba']")).getSize().getWidth();
driver.findElement(By.xpath("//*[@id='gbqfba']")).click();
driver.quit();
}}
您是否尝试添加 Thread.sleep 以查看它是否是计时问题?