使用 Selenium Web 驱动程序,无法截取带有工具提示文本的网页截图 [英] Using Selenium web driver, not able to take Screenshot of web page with tool-tip text displayed on it

查看:29
本文介绍了使用 Selenium Web 驱动程序,无法截取带有工具提示文本的网页截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试截取显示工具提示文本的网页屏幕截图,但我只能看到鼠标悬停后突出显示的 WebElement 文本,但在脚本执行期间工具提示甚至没有显示在网页上.

这是我正在尝试执行的一段代码.需要帮助才能弄清楚我错过了什么..

 public static void main(String[] args) 抛出 InterruptedException、IOException、AWTException{String URL = "https://www.rediff.com";System.setProperty("webdriver.chrome.driver", "C:\\Users\\bhara\\eclipse-workspace\\Drivers\\chromedriver.exe");WebDriver 驱动程序 = new ChromeDriver();driver.get(URL);driver.manage().window().maximize();driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);String WebElementXpath = "///*[@class='mailicon']";字符串 toolTipText ;WebElement ToolTipElement = driver.findElement(By.xpath(WebElementXpath));Actions act1 = new Actions(driver);act1.moveToElement(ToolTipElement).build().perform();机器人机器人 = 新机器人();点点;点 = ToolTipElement.getLocation();int x = point.getX();int y = point.getY();机器人.mouseMove(x,y);文件 scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(scrFile, new File("screenshot.png"));System.out.println( ToolTipElement.getAttribute("title"));}

解决方案

我在您的代码块中没有看到任何此类问题.但是,如果您观察 WebElement 的

文本Lightning fast free emailtitle 属性,但不是工具提示.因此,通过使用 Actions 类在元素上鼠标悬停不会弹出弹出窗口.但是,我做了一些小的更改并执行了您的代码以获得如下相同的结果:

  • 代码块:

    ChromeOptions options = new ChromeOptions();options.addArguments("开始最大化");options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));options.setExperimentalOption("useAutomationExtension", false);WebDriver driver = new ChromeDriver(options);driver.get("https://www.rediff.com/");new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='mailicon']")))).build().perform();文件 scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Rediffmail.png"));System.out.println(driver.findElement(By.xpath("//a[@class='mailicon']")).getAttribute("title"));驱动程序退出();

  • 控制台输出:

    闪电般的快速免费电子邮件

  • 截图:

I am trying to take screen shot of the web page showing the tool tip text, but I can only see WebElement text being highlighted after mouse hover, but tool tip is not even shown up on the web page during script execution.

Here is the piece of code, that I am trying to execute. Need help to figure out what am I missing..

 public static void main(String[] args) throws InterruptedException, IOException, AWTException  
   {
    String URL = "https://www.rediff.com";
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\bhara\\eclipse-workspace\\Drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get(URL);
    driver.manage().window().maximize();
    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

    String WebElementXpath = "//*[@class='mailicon']";

    String toolTipText ;
    WebElement ToolTipElement = driver.findElement(By.xpath(WebElementXpath));

    Actions act1 = new Actions(driver);


    act1.moveToElement(ToolTipElement).build().perform();

     Robot robot = new Robot();

     Point point;
     point = ToolTipElement.getLocation();
     int x = point.getX(); 
     int y = point.getY(); 
     robot.mouseMove(x,y);

     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
     FileUtils.copyFile(scrFile, new File("screenshot.png"));

    System.out.println( ToolTipElement.getAttribute("title"));

}

解决方案

I don't see any such issues in your code block as such. However if you observe the HTML DOM of the WebElement:

The text Lightning fast free email is the title attribute but not the tooltip as such. Hence through using Actions class to Mouse Hover over the element won't bring up the popup. However, I made some small changes and executed you code to get the same result as follows:

  • Code Block:

    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.rediff.com/");
    new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@class='mailicon']")))).build().perform();
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Rediffmail.png"));
    System.out.println(driver.findElement(By.xpath("//a[@class='mailicon']")).getAttribute("title"));
    driver.quit();
    

  • Console Output:

    Lightning fast free email
    

  • Screenshot:

这篇关于使用 Selenium Web 驱动程序,无法截取带有工具提示文本的网页截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆