Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA Selenium Webdriver在屏幕截图中捕获错误区域_Selenium_Webdriver_Capture_Region - Fatal编程技术网

JAVA Selenium Webdriver在屏幕截图中捕获错误区域

JAVA Selenium Webdriver在屏幕截图中捕获错误区域,selenium,webdriver,capture,region,Selenium,Webdriver,Capture,Region,我正在使用SeleniumWeb驱动程序(Java)来自动化我们的Web应用程序。 我需要捕获并比较所有浏览器中应用程序的每个图标 首先,我在Firefox中打开了应用程序,用xpath捕获了图标图像,然后将它们保存在特定路径。 稍后在其他浏览器中打开应用程序时比较保存的图像 为此,我使用了下面的代码来捕获图像,但是元素图像没有捕获,屏幕中的一些未知区域正在保存 请帮助,如何获得元素的正确图像 File screenshot = ((TakesScreenshot)driver).getScre

我正在使用SeleniumWeb驱动程序(Java)来自动化我们的Web应用程序。 我需要捕获并比较所有浏览器中应用程序的每个图标

首先,我在Firefox中打开了应用程序,用xpath捕获了图标图像,然后将它们保存在特定路径。 稍后在其他浏览器中打开应用程序时比较保存的图像

为此,我使用了下面的代码来捕获图像,但是元素图像没有捕获,屏幕中的一些未知区域正在保存

请帮助,如何获得元素的正确图像

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage  fullImg = ImageIO.read(screenshot);
Point point = x.getLocation();
//Get width and height of the element
int eleWidth = x.getSize().getWidth();
int eleHeight = x.getSize().getHeight();
Rectangle rect = new Rectangle(point.getX(),point.getY(),eleWidth, eleHeight);
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), rect.width, rect.height);
ImageIO.write(eleScreenshot, "png", screenshot);
//Copy the element screenshot to disk
FileUtils.copyFile(screenshot, new File("E:\\ICONS\\Icon1.jpg"));

我对以前的代码所做的更改是,根据我的应用程序分辨率,将值添加到X坐标,并将静态值传递到Y坐标。

您的代码与此答案中给出的代码非常相似-。行
Rectangle rect=新矩形(point.getX(),point.getY(),eleWidth,eleHeight)不是必需的。您可以编写-
buffereImage eleScreenshot=fullImg.getSubimage(point.getX(),point.getY(),eleWidth,eleHeight)。我只是将您的代码与问题链接中的代码进行了比较。是的,最初我搜索了代码以捕获Stackoverflow中的代码并编写了,但它对我不起作用。因此再次发布了问题。在获得
x.getLocation()之前,您知道为什么它没有捕获我的应用程序中的特定区域吗,可以尝试显式等待元素x可见/可单击吗?另外,发布您试图查找元素x.WebElement x=driver.findElement的代码(By.xpath(“/*[@id='CCDLinkedformToolbar\u cmdPrint']);《睡眠》(2000年);
    driver.switchTo().defaultContent();
    driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='CWinBtn']")));
    WebElement ele =driver.findElement(By.xpath("//*[@id='CCDLinkedformToolbar_cmdPrint']"));
    try{         
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    BufferedImage  fullImg = ImageIO.read(screenshot);
    Point point = ele.getLocation();
    int eleWidth = ele.getSize().getWidth();
    int eleHeight = ele.getSize().getHeight();
    BufferedImage eleScreenshot= fullImg.getSubimage(point.getX()+30, 95, eleWidth, eleHeight);
    ImageIO.write(eleScreenshot, "png", screenshot);
    FileUtils.copyFile(screenshot, new File("E:\\ ICONS\\Icon1.png"));
    }
    catch(Exception e){
         e.printStackTrace();
    }