HtmlUnit无法单击Selenium,Java中的跨度

时间:2017-08-25 19:11:59

标签: java selenium htmlunit htmlunit-driver

我正在使用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)

1 个答案:

答案 0 :(得分:0)

根本原因是你的HtmlUnitDriver没有执行JavaScript。你必须打开它。

然而,由于网站存在一些错误,驱动程序将崩溃。因此,您必须自定义HtmlUnitDriver以允许这些错误。

您需要做的是继承HtmlUnitDriver并覆盖module.js:471 throw err; ^ Error: Cannot find module 'heimdalljs-logger' at Function.Module._resolveFilename (module.js:469:15) at Function.Module._load (module.js:417:25) ... 以关闭一些错误抛出。

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;
    }
}

P.S。我认为你应该选择PhantomJS或SlimmerJS。