2017-02-16 79 views
-1

是否有可能使用硒web驅動程序捕獲網絡用戶界面的特定部分(如使用圖像的座標?不是整個頁面!)?我用java寫我的腳本。謝謝屏幕截圖使用硒webdriver的網絡的一部分

+1

不是開箱即用的。相反,截取整個頁面,獲取所需元素的邊界和座標並相應地裁剪圖像。 – qqilihq

+0

感謝您的回覆@qqilihq –

+0

@jainishkapadia我相信您分享的鏈接正在瀏覽整個網頁。我擔心的是多個屏幕截圖是網頁的一部分或內部(可能位於隨機位置)。 –

回答

0

想和大家分享我如何使用Multiscreenshot罐子解決了這個問題。謝謝。

import multiScreenShot.MultiScreenShot; 

WebElement object1 = driver.findElement(By.Id("MyObject")); 
object1.click(); 
MultiScreenShot multiScreens = new MultiScreenShot("C:\\", "test"); // Path where you save the image 
multiScreens.elementScreenShot(driver, object1); 
WebElement object2 = driver.findElement(By.Id("YourObject")); 
object2.click(); 
multiScreens.elementScreenShot(driver, object2); 
0

我認爲這部分代碼會幫助你。

driver.get("http://www.google.com"); 
 
WebElement ele = driver.findElement(By.id("hplogo")); 
 

 
// Get entire page screenshot 
 
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
 
BufferedImage fullImg = ImageIO.read(screenshot); 
 

 
// Get the location of element on the page 
 
Point point = ele.getLocation(); 
 

 
// Get width and height of the element 
 
int eleWidth = ele.getSize().getWidth(); 
 
int eleHeight = ele.getSize().getHeight(); 
 

 
// Crop the entire page screenshot to get only element screenshot 
 
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), 
 
    eleWidth, eleHeight); 
 
ImageIO.write(eleScreenshot, "png", screenshot); 
 

 
// Copy the element screenshot to disk 
 
File screenshotLocation = new File("C:\\images\\GoogleLogo_screenshot.png"); 
 
FileUtils.copyFile(screenshot, screenshotLocation);

+0

這對我來說很好。我覺得這會對我的項目起作用。感謝@Lucky的幫助。 –