Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium PhantomJS Java - Refused to evaluate a string because 'unsafe-eval' is not an allowed

Tags:

I'm playing with Selenium and PhantomJS. I'm trying to draw all the elements from a web page. When I retrieve some web page and then I try to get the position of any web element I get this error when I select the web element on my code:

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"}}

My code is the following. I'm using Jsoup to get the elements because with selenium I often got the same error as previously mentioned:

    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();
                }
            }
        }
    }

Why I can't get the position from an element?
I'm using selenium 2.46, PhantomJS 2.0.0 and, for example, this page have problems https://github.com/ariya/phantomjs/issues/13114

like image 421
Andrés Córdova Avatar asked May 02 '16 15:05

Andrés Córdova


1 Answers

Have you tried using chromedriver or geckodriver? PhantomJS hasn't been under active development in a while so you might run into some issues with it.

http://chromedriver.chromium.org/

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

like image 95
Zach Avatar answered Oct 18 '22 00:10

Zach