在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java  Python  HTML/ selenium + java 屏幕截圖

selenium + java 屏幕截圖

我使用selenium 進(jìn)行自動化測試,驅(qū)動使用的chrome瀏覽器的驅(qū)動,在用到selenium 的截屏功能的時候發(fā)現(xiàn)一個問題,就是截圖的結(jié)果只是當(dāng)前頁面顯示出來的部分,并不是整個頁面的全部內(nèi)容。

但是之前在使用phantomjs進(jìn)行截圖的時候,就可以完全把整個界面,超過一屏幕的部分全部截取下來,

File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

截屏代碼使用的是這個。

第二個問題:
我看其他語言或者驅(qū)動實(shí)現(xiàn)截屏功能的時候,有一個單元素的截圖功能,

WebElement element = driver.findElement(By.id("header"));
File file1 = element.getScreenshotAs(OutputType.FILE);

但是這個在 java + selenium + chrome 瀏覽器下報(bào)錯,
報(bào)錯內(nèi)容如下:

Exception in thread "main" org.openqa.selenium.UnsupportedCommandException: unknown command: session/d1d93b03a5610dccfa22f775a7dd5bad/screenshot/0.34969357638110843-1
Command duration or timeout: 8 milliseconds
Build info: version: '2.53.1', revision: 'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: 'DESKTOP-L6804PP', ip: '192.168.99.156', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1), userDataDir=C:\Users\zzg\AppData\Local\Temp\scoped_dir54824_4941}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=64.0.3282.167, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: d1d93b03a5610dccfa22f775a7dd5bad
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
    at org.openqa.selenium.remote.RemoteWebElement.getScreenshotAs(RemoteWebElement.java:447)

請大佬給指明一下是哪里的問題!

回答
編輯回答
絯孑氣

對于 Selenium 本身不是很熟,但或許以下兩個來自 StackOverFlow 的回答可以幫到你:

1、截取整個頁面,核心思路是添加 Chrome WebDriver 參數(shù)。
https://stackoverflow.com/que...

2、獲取指定元素的截圖,這個相對復(fù)雜一點(diǎn),核心思路就是截完整圖之后,用圖片處理類根據(jù)需要獲取的元素位置再進(jìn)行圖片處理。
https://stackoverflow.com/que...

如果以上兩個回答解決了你的問題,記得給 StackOverFlow 原作者一個 upvote。:)

Update on 2018-03-12: 根據(jù)題主的測試反饋,第一個答案提供了 full screen (全屏)的截圖方案,并不是 full web page。

2018年6月22日 00:10