Selenium PhantomJS Java - 拒绝评估字符串,因为'unsafe-eval'不是允许的
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Selenium PhantomJS Java - 拒绝评估字符串,因为'unsafe-eval'不是允许的相关的知识,希望对你有一定的参考价值。
我正在玩Selenium和PhantomJS。我正在尝试从网页中绘制所有元素。当我检索一些网页然后我尝试获取任何web元素的位置时,当我在我的代码上选择web元素时,我收到此错误:
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".
","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
以上是关于Selenium PhantomJS Java - 拒绝评估字符串,因为'unsafe-eval'不是允许的的主要内容,如果未能解决你的问题,请参考以下文章
selenium phantomjs java无界面浏览器环境搭建
[Selenium+Java] Selenium with HTMLUnit Driver & PhantomJS
Java之网络爬虫WebCollector2.1.2+selenium2.44+phantomjs2.1.1
Selenium + Firefox到Selenium + PhantomJS [关闭]
Ghostdriver 1.2.1 + PhantomJS 2.0 + 最新的 Selenium 在 Java 中找不到变量错误