如何使用 Selenium 在 Angular 7 应用程序中提取工具提示文本

编程入门 行业动态 更新时间:2024-10-22 23:15:01
本文介绍了如何使用 Selenium 在 Angular 7 应用程序中提取工具提示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试通过 selenium 读取 angular7 应用程序中的文本工具提示.但是获取文本返回空白,而 javascript 执行程序返回空.

使用 Selenium 你必须:

诱导 WebDriverWait 使所需的元素可见鼠标悬停元素.诱导 WebDriverWait 使所需的工具提示可见然后提取工具提示文本

您可以使用以下解决方案:

代码块:

import org.openqa.selenium.By;导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.chrome.ChromeDriver;导入 org.openqa.selenium.chrome.ChromeOptions;导入 org.openqa.selenium.interactions.Actions;导入 org.openqa.selenium.support.ui.ExpectedConditions;导入 org.openqa.selenium.support.ui.WebDriverWait;公共类 Angular_ToolTip {公共静态无效主(字符串 [] args){System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");ChromeOptions options = new ChromeOptions();options.addArguments("开始最大化");//options.addArguments("disable-infobars");options.addArguments("--disable-extensions");WebDriver driver = new ChromeDriver(options);driver.get("https://vmware.github.io/clarity/documentation/v0.12/tooltips");new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h6[text()='Small']//following::div[1]/a[@class='tooltip tooltip-sm']")))).build().perform();System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h6[text()='Small']//following::div[1]/a[@class='tooltip tooltip-sm']//following::span[1]"))).getAttribute("innerHTML"));}}

控制台输出:

Lorem ipsum 坐

浏览器快照:

I'm trying to read text tooltip in angular7 application via selenium. But get text is returning blank and javascript executor is returning null.

Link to the image of the DOM for which I'm not able to find the xpath

But get text is returning blank and javascript executor is returning null.

This is returning blank

driver().get("https://vmware.github.io/clarity/documentation/v0.12/tooltips");
Wait(3000);
System.out.println(driver().findElement(By.xpath("(//span[@class='tooltip-content'])[2]")).getText());

This is returning null

System.out.println(driver().findElement(By.xpath("(//span[@class='tooltip-content'])[2]")).getAttribute("value"));

String theTextIWant = ((JavascriptExecutor) driver()).executeScript("return arguments[0].innerHTML;",driver().findElement(By.xpath("(//span[@class='tooltip-content'])[2]")));

解决方案

To extract the tooltip text Lorem ipsum sit within the Angular7 Application using Selenium you have to:

Induce WebDriverWait for the desired element to be visible Mouse Hover the element. Induce WebDriverWait for the desired tooltip to be visible Then extract the Tool Tip Text

You can use the following solution:

Code Block:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Angular_ToolTip {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        //options.addArguments("disable-infobars");
        options.addArguments("--disable-extensions");
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://vmware.github.io/clarity/documentation/v0.12/tooltips");
        new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h6[text()='Small']//following::div[1]/a[@class='tooltip tooltip-sm']")))).build().perform();
        System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h6[text()='Small']//following::div[1]/a[@class='tooltip tooltip-sm']//following::span[1]"))).getAttribute("innerHTML"));
    }
}

Console Output:

Lorem ipsum sit

Browser Snapshot:

这篇关于如何使用 Selenium 在 Angular 7 应用程序中提取工具提示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-22 16:15:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027019.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   应用程序   文本   提示   工具

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!