PhantomJS无法从Java运行JS:拒绝将字符串评估为JavaScript

时间:2017-11-20 12:11:32

标签: javascript java selenium selenium-webdriver phantomjs

美好的一天,我正在开展学校项目,使用PhantomJS.exe(phantomjs-2.1.1)在Windows上使用JAVA(IntelijiIdea)和selenium(selenium-server-standalone-3.7)来遍历LinkedIn中的所有用户数据。 1.jar司机)。

我发现在LinkedIn中有许多延迟加载的元素,因此我需要在PhantomJS浏览器中执行javascript以向下滚动最后加载的元素以加载所有数据并获取所有数据。

获取特定LinkedIn用户的URL后如下:

driver.get("FullurlToLinkedInUser");

然后我能够成功收到页面上的最后一个元素,我正在尝试这段代码(不是我自己的,复制自:Scrolling with phantomJs Selenium):

public synchronized WebDriver scrollToBottom(WebDriver driver, WebElement element,int time) throws InterruptedException {
    String oldpage="";
    String newpage="";


    do{
        oldpage=driver.getPageSource();
        ((JavascriptExecutor) driver)
                .executeScript("window.scrollTo(0, (document.body.scrollHeight))");
        this.wait(time);
        newpage=driver.getPageSource();
    }while(!oldpage.equals(newpage));
    return driver;
}

在调用此函数之后:

driver = scrollToBottom(driver,myWebElement, 2);

我会收到此错误:

{"errorMessage":"
Refused to evaluate a string as JavaScript because 'unsafe- eval' is not an allowed source of script in the following Content Security Policy directive ... }

我正在创建WebDriver,如下所示:

DesiredCapabilities caps = new DesiredCapabilities();
    ((DesiredCapabilities) caps).setJavascriptEnabled(true);
    ((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
    ((DesiredCapabilities) caps).setCapability(
            PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
            "C:/Users/Bernad/Desktop/FIIT/ING/SEM1/AOVS/JAVA_selenium_phantomjs/phantomjs.exe"   //java web start / jnpl file...
            // "/Controller/phantomjs.exe"
    );

    //SET enabled javascript for php script on WEB page to transform it to picture:
    caps.setJavascriptEnabled(true);
    String [] phantomJsArgs = {"--web-security=no", "--ignore-ssl-errors=yes"};
    caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomJsArgs);

    //CREATING WEB driver
    driver = new PhantomJSDriver(caps);

而且我不知道这是否与我之前遇到的某些错误有关,在我登录LinkedIn并继续关联人员之后异步发生在驱动程序中,这些错误不会导致PhantomJS崩溃,但发生,错误如:

[ERROR - 2017-11-20T11:25:47.737Z] Session [8dd6f810-cde5-11e7-9a5e-a3938000ab70] - page.onError - msg: TypeError: null is not an object (evaluating 'c.classList')

phantomjs://platform/console++.js:263 in error
[ERROR - 2017-11-20T11:25:47.737Z] Session [8dd6f810-cde5-11e7-9a5e-a3938000ab70] - page.onError - stack:
h (https://static.licdn.com/scds/concat/common/js?h=3kp2aedn5...:71 

(anonymous function) (https://static.licdn.com/scds/concat/common/js h=3kp2aedn5pmamdr4dk4n8atur-3ti5bgrnb6id...:71)
(anonymous function) (https://static.licdn.com/scds/concat/common/js?h=3kp2aedn5pmamdr4dk4n8atur-3ti5bgrnb6idjt...:71)
(anonymous function) (https://static.licdn.com/scds/concat/common/js?h=3kp2aedn5pmamdr4dk4n8atur-3ti5bgrnb6idjtk0w...:71)

phantomjs://platform/console++.js:263 in error
[ERROR - 2017-11-20T11:25:49.998Z] Session [8dd6f810-cde5-11e7-9a5e-a3938000ab70] - page.onError - msg: [object XMLHttpRequest]

我确信我的WebElement发送功能存在。拜托,请你帮忙,因为我没有找到类似的JAVA实施解决方案。提前谢谢。

0 个答案:

没有答案