简体   繁体   中英

Embedding javascript for parsing and clicking a webpage in selenium ghost drivers

I am trying to parse Google search result page with Ghost driver, for which I need to parse the second search result page. So using Java script, I am trying to go through the source (html) of search result page and navigating by clicking(with Ghost driver) the page numbers below it. Then it throws following Exception.

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

driver = new HtmlUnitDriver();

    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("document.querySelectorAll('#foot span div table tbody tr td a')[2].click();");




Exception in thread "main" java.lang.UnsupportedOperationException: Javascript is not enabled for this HtmlUnitDriver instance
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.getPageToInjectScriptInto(HtmlUnitDriver.java:503)
    at org.openqa.selenium.htmlunit.HtmlUnitDriver.executeScript(HtmlUnitDriver.java:458)
    at ATest.main(ATest.java:46)

That's not using GhostDriver; that's using HtmlUnit. You have two choices. You can either enable JavaScript for HtmlUnit, which would be done through the desired capabilities of the driver, or you can actually use GhostDriver, which is a part of the PhantomJS project. In your Java code, you'd create a PhantomJSDriver object, which would be used to navigate to manipulate the resulting page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM