当我使用无头浏览器phatomjs运行java代码时,出现此错误,请帮我解决此问题?
得到以下错误
Server.CreateObject("...")
软件
Jar文件版本:phantomjs://platform/console++.js:263 in error
exe版本:Phatomjsdriver:1.2.1
IDE:Eclipse的
编程语言:phantomjs-2.1.1-windows
我执行的代码 -
java
我也试过这个功能 -
System.setProperty("phantomjs.binary.path", phatomPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("-incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new PhantomJSDriver(capabilities);
driver.get("https://www.*********.com/home.html?product=testCard");
String strPageTitle = driver.getTitle();
System.out.println("Page title: - "+strPageTitle);
WebElement nameInputField = driver.findElement(By.id("card"));
nameInputField.sendKeys("*************");
driver.findElement(By.id("go")).click();
try{
//WebDriverWait wait = new WebDriverWait(driver, 40);
//WebElement tableElement = wait.until(ExpectedConditions.elementToBeClickable(By.className("reportTable")));
WebElement tableElement = driver.findElement(By.className("reportTable"));
System.out.println("taleElemen"+tableElement);
java.util.List<WebElement> rows = tableElement.findElements(By.tagName("tr"));
for (WebElement row : rows) {
java.util.List<WebElement> cols = row.findElements(By.tagName("td"));
for (WebElement col : cols) {
testData.put(col.getText().split(":")[0],col.getText().split(":")[1]);
}
}
WebElement avlBalance = driver.findElement(By.xpath("//td[.='Available Gift Card Balance']/../td[last()]"));
testData.put("Available Gift Card Balance",avlBalance.getText());
//lstSting.add(avlBalance.getText());
}catch(Exception e){
System.out.println(e);
}
我得到的另一个例外是 -
caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
phatomPath2
);
答案 0 :(得分:0)
我们需要以这种方式调用click方法 -
我使用了给定的代码,因为对我而言name
和div id
( go )是相同的。
WebElement element = driver.findElement(By.name("go"));
try {
safeJavaScriptClick(element);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
方法 -
public static void safeJavaScriptClick(WebElement element) throws Exception {
try {
if (element.isEnabled() && element.isDisplayed()) {
System.out.println("Clicking on element with using java script click");
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
} else {
System.out.println("Unable to click on element");
}
} catch (StaleElementReferenceException e) {
System.out.println("Element is not attached to the page document " + e.getStackTrace());
} catch (NoSuchElementException e) {
System.out.println("Element was not found in DOM " + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to click on element " + e.getStackTrace());
}
}
这是常见的错误,不会损害我们当前的执行。
phantomjs://platform/console++.js:263 in error
要检查点击页面上的内容,我们需要这样做 -
WebElement tableElement = wait.until(ExpectedConditions.elementToBeClickable(By.className("reportTable")));
这对我来说非常有用。