【问题标题】:How to extract the tooltip text within an Angular 7 application using Selenium如何使用 Selenium 在 Angular 7 应用程序中提取工具提示文本
【发布时间】:2019-04-02 23:05:46
【问题描述】:

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

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

但获取文本返回空白,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());

这是返回 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]")));

【问题讨论】:

  • 不应该是("(//span[@class='tooltip-content'])[2]"),你在索引2周围少了[]
  • 为什么你在 xpath 的一侧给出了圆括号
  • @LeyonGudinho ("(//span[@class='tooltip-content'])[2]) 在获取正确的文本
  • 我认为您误解了,我的意思是说您的代码在您的问题中显示 [2] 为 2。
  • .getAttribute("innerHTML") 而不是 getText() 就可以了。 @DebanjanB 在答案中也提到了。请接受。

标签: angular selenium selenium-webdriver angular7 webdriverwait


【解决方案1】:

要使用 SeleniumAngular7 Application 中提取工具提示文本 Lorem ipsum sat,您必须:

  • 诱导 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"));
          }
      }
      
  • 控制台输出:

    Lorem ipsum sit
    
  • 浏览器快照:

【讨论】:

    猜你喜欢
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 2023-03-26
    • 1970-01-01
    • 2018-07-31
    相关资源
    最近更新 更多