gpt4 book ai didi

javascript - 屏幕截图中没有出现突出显示元素?请

转载 作者:行者123 更新时间:2023-12-01 16:12:59 32 4
gpt4 key购买 nike

我试图通过屏幕截图捕获带有突出显示的元素,但在屏幕截图中,该元素不带有突出显示。有谁知道解决办法吗?当我捕获文本时会出现突出显示:automatests@gmail.com

查看我的代码:

Utility.captureScreenShot(driver, "Verifica Nome_Email");
WebElement taketext1= driver.findElement(By.xpath("//*[@id=\"gb\"]/div[2]/div[3]/div[1]/div[2]/div/a/img"));
Helper.highLightElement(driver, taketext1);
driver.findElement(By.xpath("//*[@id=\"gb\"]/div[2]/div[3]/div[1]/div[2]/div/a/img")).click();
Thread.sleep(2000);
WebElement taketext= driver.findElement(By.className("gb_tb"));
Helper.highLightElement(driver, taketext);
Thread.sleep(2000);
Utility.captureScreenShot(driver, "Verifica Nome_Email");
String emailatual = taketext.getText();
assertTrue(emailatual.contains("automatests@gmail.com"));
driver.findElement(By.id("gb_71")).click();
driver.close();

方法公共(public)类助手{

public static void highLightElement(WebDriver driver, WebElement element)
{
JavascriptExecutor js=(JavascriptExecutor)driver;

//js.executeScript("arguments[0].setAttribute('style', 'background: transparent; border: 2px solid red;');", element);
js.executeScript("arguments[0].style.border='2px solid red'", element);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e) {

System.out.println(e.getMessage());
}

js.executeScript("arguments[0].setAttribute('style','border: solid 2px white');", element);

}

} enter image description here

最佳答案

我使用java.awt包中的Robot类来捕获屏幕截图。您可以使用以下代码替换 highLightElement 方法:

public static void highlightAndCapture(WebDriver driver, WebElement elem)
throws IOException, HeadlessException, AWTException {
if (driver instanceof JavascriptExecutor) {
// To highlight the element
((JavascriptExecutor) driver).executeScript("arguments[0].style.border='2px solid red'", elem);
// To capture screenshot
BufferedImage image = new Robot()
.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File("screenshot.png"));
// To remove border from highlighted element(Optional)
((JavascriptExecutor) driver).executeScript("arguments[0].style.border = \"none\";", elem);
}
}

关于javascript - 屏幕截图中没有出现突出显示元素?请,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62489370/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com