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

ani*_*tri 1 selenium selenium-webdriver angular webdriverwait angular7

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

链接到我无法找到xpath的DOM图像

但是获取文本返回空白,而javascript执行程序返回null。

这是空白

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());
Run Code Online (Sandbox Code Playgroud)

这将返回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]")));
Run Code Online (Sandbox Code Playgroud)

Deb*_*anB 5

要使用SeleniumAngular7应用程序中提取工具提示文本Lorem ipsum,您必须:

  • 诱导WebDriverWait用于所需元件是可见的
  • 将鼠标悬停在元素上。
  • 诱导WebDriverWait所希望的工具提示可见
  • 然后提取工具提示文本
  • 您可以使用以下解决方案:

    • 代码块:

      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"));
          }
      }
      
      Run Code Online (Sandbox Code Playgroud)
  • 控制台输出:

    Lorem ipsum sit
    
    Run Code Online (Sandbox Code Playgroud)
  • 浏览器快照:

工具提示