HtmlUnit 无法点击 Selenium 中的 span,Java
HtmlUnit unable to click on spans in Selenium, Java
我正在 Java 使用 Selenium 编写自动化工具。如果 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.call(HtmlUnitDriver.java:1965)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.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();
P.S。我认为你应该改用 PhantomJS 或 SlimmerJS。
我正在 Java 使用 Selenium 编写自动化工具。如果 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.call(HtmlUnitDriver.java:1965)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.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();
P.S。我认为你应该改用 PhantomJS 或 SlimmerJS。