Java 如果有flash插件,phantomjs无法加载页面
我在使用Java中的phantomjs运行selenium测试时遇到问题。 我知道phantomjs不支持flash。我正在运行chromedriver的双重测试,没有启用flash和phantom。在chromedriver显示未启用flash插件的页面上,它只加载页面的其余部分并正确读取。 幻影测试在此处引发超时异常:Java 如果有flash插件,phantomjs无法加载页面,java,selenium,phantomjs,selenium-chromedriver,Java,Selenium,Phantomjs,Selenium Chromedriver,我在使用Java中的phantomjs运行selenium测试时遇到问题。 我知道phantomjs不支持flash。我正在运行chromedriver的双重测试,没有启用flash和phantom。在chromedriver显示未启用flash插件的页面上,它只加载页面的其余部分并正确读取。 幻影测试在此处引发超时异常: WebDriverWait wait = new WebDriverWait(driver, 10); wait.until(ExpectedConditions.visib
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'"+s +"')]")));
有什么想法吗
完整堆栈跟踪:
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
重现问题的代码:
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')]")));
}
请注意,如果将其更改为chromedriver,则不会引发任何错误,并且可以看到加载的页面。使用phantom时,不会加载页面,因此会出现错误。也许我应该在github中将此作为错误报告归档
编辑
我在这里提交了一个新版本PhantomJS的默认视口大小为400x300。对于大多数使用固定宽度的站点来说,这太小了,这就是为什么会出现滚动条的原因,这也是为什么在使用Selenium Webdrivers元素时无法与之交互的原因,因为它们隐藏在视图之外。 对于其他站点,将显示移动站点,该站点可能基于媒体查询,使用的元素可能不同于桌面站点,并且具有不同的选择器 要解决此问题,应在打开页面之前将视口设置为类似桌面的状态,如图所示: 这很可能与页面上存在flash对象,而PhantomJS无法加载这些对象这一事实无关。如果它真的与此有关,那么脚本/幻影JS会更快崩溃 还可以将用户代理字符串设置为表示PhantomJS功能的内容。一些网站,如谷歌,将使用网络协议或提供在PhantomJS 1.x中无法运行的JavaScript。所以对于PhantomJS 1.x来说
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);
幻影驱动程序不支持闪存技术。这只是在做Java脚本。我不理解下面的投票。我纠正了这段代码中的crash=throw异常。该站点不使用SSL。当你进入一个特定的足球联赛->欧洲杯->荷兰足球联赛->比赛时,URL就会显示出来。我还能发什么?例外情况在另一条评论中发布。屏幕截图通常呈现页面,当我点击链接时,它不会像chrome那样加载最后一部分。这是一个页面,上面有内容,底部有一条无flash消息。我不想做游戏,抱歉,谢谢你的努力。不幸的是,链接改变了。我现在会更新,但几周后就会过时。这并不能解决问题。这是整个例外
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);