java - Selenium PhantomJS Java - 拒绝评估字符串,因为不允许使用 'unsafe-eval'

标签 java selenium phantomjs jsoup

我正在玩 Selenium 和 PhantomJS。我正在尝试从网页中绘制所有元素。当我检索某个网页然后尝试获取任何网页元素的位置时,当我在代码中选择网页元素时出现此错误:

org.openqa.selenium.WebDriverException: {"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: \"script-src assets-cdn.github.com\".\n","request":{"headers":{"Accept-Encoding":"gzip,deflate","Cache-Control":"no-cache","Connection":"Keep-Alive","Host":"localhost:26310","User-Agent":"Apache-HttpClient/4.4.1 (Java/1.8.0_45)"},"httpVersion":"1.1","method":"GET","url":"/location","urlParsed":{"anchor":"","query":"","file":"location","directory":"/","path":"/location","relative":"/location","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/location","queryKey":{},"chunks":["location"]},"urlOriginal":"/session/77ee7e10-1077-11e6-9f8f-1f750417371e/element/%3Awdc%3A1462201609875/location"}}

我的代码如下。我正在使用 Jsoup 来获取元素,因为使用 selenium 我经常遇到与前面提到的相同的错误:

    WebDriver driver = new PhantomJSDriver();
    driver.manage().window().setSize(new Dimension(1366, 768));
    driver.get(URL);
    Document doc = Jsoup.parse(driver.getPageSource());
    Elements e1 = doc.body().getAllElements();
    ArrayList<String> tags = new ArrayList<>();
    for (Element e : e1) {
        if (tags.indexOf(e.tagName()) == -1) {
            tags.add(e.tagName());
            List<WebElement> query = null;
            if (driver.findElements(By.tagName(e.tagName())).size() < 1) {
                continue;
            }
            try {
                query = driver.findElements(By.tagName(e.tagName()));
            } catch (Exception ex) {
                continue;
            }
            for (WebElement temp1 : query) {
                try {
                    Point po = temp1.getLocation();
                    Dimension d = temp1.getSize();
                    if (d.width <= 0 || d.height <= 0 || po.x < 0 || po.y < 0) {
                        continue;
                    }
                    graph.draw(new Rectangle(po.x, po.y, d.width, d.height));
                } catch (WebDriverException ex) {
                    System.out.println("Error!");
                    ex.printStackTrace();
                }
            }
        }
    }

为什么我无法从元素中获取位置?
我正在使用 selenium 2.46、PhantomJS 2.0.0,例如,此页面有问题 https://github.com/ariya/phantomjs/issues/13114

最佳答案

您是否尝试过使用 chromedriver 或 geckodriver? PhantomJS 有一段时间没有积极开发,因此您可能会遇到一些问题。

http://chromedriver.chromium.org/

https://github.com/mozilla/geckodriver/releases

关于java - Selenium PhantomJS Java - 拒绝评估字符串,因为不允许使用 'unsafe-eval',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36985943/

相关文章:

java - 有人可以帮助我在 rest-api 中使用文本文件吗?

java - 包括给定包外部的类的反射(Dropwizard + Swagger)

c# - 如何防止 Selenium C# 中 PageFactory 中的 StaleElementReferenceException?

javascript - 将带有函数的 Javascript 对象转换为字符串

javascript - echo 用户代理 Casperjs

javascript - 通过电子邮件发送生成的 Highchart 图像

java - 在 Ant 属性(property)中间逃脱一美元

java - AsyncTask/Handler 滞后 UI

java - Selenium WebDriver 使用三个框架。无法找到该元素

在selenium中选择javascript