1

I have a problem running selenium tests with phantomjs from Java. I am aware that phantomjs doesn't support flash. I am running dual tests with chromedriver without flash enabled and phantom. on a page that chromedriver displays no flash plugin enabled, it just loads the rest of the page and reads it correctly. The phantom tests throw an exception of timeout here:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'"+s +"')]")));

any ideas?

Full stacktrace:

org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for visibility of element located by By.xpath: //span[contains(text(),'Swiss Super League')]
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'countZero', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-40-generic', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, handlesAlerts=false, databaseEnabled=false, version=1.9.0, platform=LINUX, browserConnectionEnabled=false, proxy={proxyType=direct}, nativeEvents=true, acceptSslCerts=false, driverVersion=1.0.3, locationContextEnabled=false, webStorageEnabled=false, browserName=phantomjs, takesScreenshot=true, driverName=ghostdriver, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 9422d0d0-7731-11e4-9730-f784ef9dac1a
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:79)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:228)
    at com.hawaii.gatherer.parsers.betfred.parse(betfred.java:88)
    at com.hawaii.gatherer.parsers.Parser.call(Parser.java:93)
    at com.hawaii.gatherer.parsers.Parser.call(Parser.java:27)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.openqa.selenium.NoSuchElementException: Error Message => 'Unable to find element with xpath '//span[contains(text(),'Swiss Super League')]''
 caused by Request => {"headers":{"Accept":"application/json, image/png","Connection":"Keep-Alive","Content-Length":"73","Content-Type":"application/json; charset=utf-8","Host":"localhost:18409"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//span[contains(text(),'Swiss Super League')]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/9422d0d0-7731-11e4-9730-f784ef9dac1a/element"}
Command duration or timeout: 795 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'countZero', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-40-generic', java.version: '1.8.0_25'
Driver info: org.openqa.selenium.phantomjs.PhantomJSDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, handlesAlerts=false, databaseEnabled=false, version=1.9.0, platform=LINUX, browserConnectionEnabled=false, proxy={proxyType=direct}, nativeEvents=true, acceptSslCerts=false, driverVersion=1.0.3, locationContextEnabled=false, webStorageEnabled=false, browserName=phantomjs, takesScreenshot=true, driverName=ghostdriver, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 9422d0d0-7731-11e4-9730-f784ef9dac1a
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:445)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:357)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:730)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:39)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:130)
    at org.openqa.selenium.support.ui.ExpectedConditions$4.apply(ExpectedConditions.java:126)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:208)
    ... 7 more
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has been taken
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'countZero', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-40-generic', java.version: '1.8.0_25'
Driver info: driver.version: RemoteWebDriver
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:125)
    ... 17 more
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Error Message => 'Unable to find element with xpath '//span[contains(text(),'Swiss Super League')]''
 caused by Request => {"headers":{"Accept":"application/json, image/png","Connection":"Keep-Alive","Content-Length":"73","Content-Type":"application/json; charset=utf-8","Host":"localhost:18409"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//span[contains(text(),'Swiss Super League')]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/9422d0d0-7731-11e4-9730-f784ef9dac1a/element"}
Build info: version: '2.41.0', revision: '3192d8a6c4449dc285928ba024779344f5423c58', time: '2014-03-27 11:29:39'
System info: host: 'countZero', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-40-generic', java.version: '1.8.0_25'
Driver info: driver.version: unknown

code to reproduce the problem:

    public void test() throws InterruptedException {
    //System.setProperty("webdriver.chrome.driver", Application.chromeDriverPath);
    //WebDriver driver= new ChromeDriver();
    WebDriver driver=new PhantomJSDriver();
    driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
    driver.manage().window().setSize(new Dimension(1280, 800));

    driver.get("http://www.betfred.com/sport");

    WebElement footballLink=driver.findElement(By.linkText("Football"));
    footballLink.click();

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement europeanLink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("European Leagues")));
    europeanLink.click();

        wait = new WebDriverWait(driver, 10);
        WebElement leagueLink= wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Italian Serie A")));
        leagueLink.click();

        wait = new WebDriverWait(driver, 10);
        WebElement finalLeagueLink=wait.until(ExpectedConditions.elementToBeClickable((By.xpath("//a[contains(text(),'Italian Serie A') and contains(@class,'action')]"))));
        Thread.sleep(1000);
        finalLeagueLink.click();

    driver.switchTo().frame("sportframe");

    wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'Italian Serie A')]")));

}

note that if you change it to chromedriver no error is thrown and you can see the page loaded. with phantom the page is not loaded, and hence the error. maybe I should file this as a bug report in github?

EDIT

I have submitted a new issue here https://github.com/ariya/phantomjs/issues/12784

arisalexis
  • 2,079
  • 2
  • 21
  • 42
  • I don't understand the downvotes. I corrected the crash = throw exception from this block of code. – arisalexis Nov 28 '14 at 10:14
  • site doesn't use SSL. the URL is http://www.betfred.com/sport when you go in a specific league football->european cups->dutch eredivisie-> matches. what more can I post? the exception is posted in the other comment. The screenshot have the page normally rendered, it just doesn't load the final part when I click on the link, as chrome does. This is the page that has content on top and in the bottom has a no flash message. I don't try to make a game, sorry and thanks for the effort. – arisalexis Nov 28 '14 at 20:24
  • unfortunately, the links change. I will update it now but it will be outdated in a couple of weeks. – arisalexis Dec 20 '14 at 15:38

2 Answers2

0

PhantomJS has a default viewport size of 400x300. For most sites that use fixed widths this is too small which is why a scrollbar appears and this is why when using the Selenium Webdrivers elements cannot be interacted with, because they are hidden out of view. For other sites the mobile site will be shown (which is probably based on media queries) and the elements used may be different from the desktop site (and have a different selector).

To solve this, you should set the viewport to something desktop-like before opening the page (as shown here):

driver.manage().window().setSize(new Dimension(1280, 800));

This has very likely nothing to do with the fact that flash objects are present on the page and PhantomJS can't load them. If it indeed had something to do with this, the script/PhantomJS would have crashed sooner.


It might also be good to set the user agent string to something that represents the capabilities of PhantomJS. Some sites (e.g. google) will use network protocols or deliver JavaScript that is not runnable in PhantomJS 1.x. So for PhantomJS 1.x this would be

String userAgent = "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1";
System.setProperty("phantomjs.page.settings.userAgent", userAgent);
Community
  • 1
  • 1
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
0

Phantom Driver doesn't have support for Flash technology. This doing only Java Script .