我正在使用Selenium用Java编写一个自动化工具。如果WebDriver是FirefoxDriver,则以下代码工作正常,但是如果我尝试用HtmlUnitDriver替换它,当它应该单击跨度时,它会崩溃。我尝试通过CSS选择器和XPath来定位跨度。使驱动程序等待元素加载
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='home']/div[3]/div[3]/div[10]/div[3]/div[2]/div[1]/a/span")));
也不能解决问题。代码为:
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_52);
//driver.setJavascriptEnabled(true);
WebDriverWait wait = new WebDriverWait(driver, 10);
//logs in
driver.get("https://www.tribalwars.net/");
driver.findElement(By.id("user")).clear();
driver.findElement(By.id("user")).sendKeys(username);
driver.findElement(By.cssSelector("div.right.login > div.wrap")).click();
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.cssSelector("a.btn-login")).click();
//It executes fine up until this point
driver.findElement(By.cssSelector("span.world_button_active")).click();
/*
This won't work either
driver.findElement(By.xpath(".//*[@id='home']/div[3]/div[3]/div[10]/div[3]/div[2]/div[1]/a/span")).click();
*/
我给出的错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Returned node was not a DOM element
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'PC', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-53-generic', java.version: '1.8.0_131'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByCssSelector(HtmlUnitDriver.java:1247)
at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1965)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1601)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1961)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:756)
at Bot.Test.build(Test.java:86)
at Bot.Test.main(Test.java:42)
根本原因是你的HtmlUnitDriver没有执行JavaScript。你必须打开它。
但是,驱动程序将崩溃,因为网站有一些错误。因此,您必须自定义 HtmlUnitDriver 以允许这些错误。
您需要做的是继承 HtmlUnitDriver 并重写modifyWebClient
以关闭一些错误抛出。
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
public class MyHtmlUnitDriver extends HtmlUnitDriver {
public MyHtmlUnitDriver(BrowserVersion version, boolean enableJavascript) {
super(version, enableJavascript);
}
protected WebClient modifyWebClient(WebClient client) {
WebClient modifiedClient = super.modifyWebClient(client);
Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
modifiedClient.getOptions().setThrowExceptionOnScriptError(false);
modifiedClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
modifiedClient.getOptions().setPrintContentOnFailingStatusCode(false);
return modifiedClient;
}
}
下面的代码显示了一个示例。
public void test() {
// Use a custom HtmlUnitDriver
WebDriver hd = new MyHtmlUnitDriver(BrowserVersion.FIREFOX_52, true);
WebDriverWait wait = new WebDriverWait(hd, 10);
hd.get("https://www.tribalwars.net/");
hd.findElement(By.id("user")).clear();
hd.findElement(By.id("user")).sendKeys(username);
hd.findElement(By.cssSelector("div.right.login > div.wrap")).click();
hd.findElement(By.id("password")).clear();
hd.findElement(By.id("password")).sendKeys(password);
hd.findElement(By.cssSelector("a.btn-login")).click();
// Add Wait here
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("span.world_button_active")));
hd.findElement(By.cssSelector("span.world_button_active")).click();
附言我认为你应该选择PhantomJS或SlimmerJS。