【问题标题】:Should we get the tooltip text before we hover-over using the Actions class or after we hover-over?我们应该在使用 Actions 类悬停之前或悬停之后获取工具提示文本吗?
【发布时间】:2020-11-11 07:12:40
【问题描述】:

如果我重复这个问题,我深表歉意。我在这个网站上得到了很多答案,但我仍然没有得到工具提示文本。 如果我不使用 Actions 类悬停并显示工具提示,我可以阅读标题。 但是,一旦我使用 Actions 类来显示工具提示,那么标题始终为空。我不想在我之前得到文本 悬停,不就是阅读显示的工具提示文本的全部想法吗?

driver.get("https://jqueryui.com/tooltip/");


WebElement frame = driver.findElement(By.xpath("//iframe[@src='/resources/demos/tooltip/default.html']"));
driver.switchTo().frame(frame);

WebElement element = driver.findElement(By.id("age"));
Actions actions = new Actions(driver);
actions.moveToElement(element).perform();  (I've also tried clickAndHold method)
WebElement toolTip = driver.findElement(By.xpath("//*[@id='age']"));

// To get the tool tip text and assert
String toolTipText = toolTip.getAttribute("title");
System.out.println("toolTipText-->"+toolTipText);

【问题讨论】:

    标签: java selenium-webdriver tooltip mouseover webdriverwait


    【解决方案1】:

    Tooltips 仅在执行mouse over 后提取。

    要打印 悬停该字段以查看工具提示。因为所需元素位于<iframe> 内,您需要:

    • scrollIntoView() 所需的iframe

    • 为所需的frameToBeAvailableAndSwitchToIt诱导WebDriverWait

    • 将你需要的元素的 visibilityOfElementLocated() 引入WebDriverWaitMouse Hover

    • visibilityOfElementLocated() 诱导 WebDriverWait 用于您需要从中检索 tooltip 的元素:

    • 您可以使用以下基于Locator Strategies

      System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
      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://jqueryui.com/tooltip/");
      ((JavascriptExecutor)driver).executeScript("return arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h1[@class='entry-title']"))));
      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@src='/resources/demos/tooltip/default.html']")));
      new Actions(driver).moveToElement(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='age']")))).build().perform();
      System.out.println(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='age']//following::div[text()]"))).getText());
      
    • 控制台输出:

      We ask for your age only for statistical purposes.
      

    【讨论】:

    • 感谢您的快速回复。但是,这仍然是不正确的文本。“悬停该字段以查看工具提示。”不是工具提示文本,工具提示文本是“我们仅出于统计目的询问您的年龄。
    • @zara 那是我的错,修复它。重新测试并让我知道状态。
    • 谢谢,是的,这行得通。最后一个问题:我看到工具提示文本“我们仅出于统计目的而询问您的年龄”。在 DOM 中的多个位置。你怎么知道从 By.xpath("//input[@id='age']//following::div[text()]" 获取文本。我一直在尝试使用驱动程序获取文本.findElement(By.xpath("//input[@id='age']")).getAttribute("title"); (显然我错了)
      我们要求您的年龄仅用于统计目的。
    • @zara 工具提示文本在下面的<div> 中,只有当您将鼠标悬停在触发事件的文本框上时才会显示。
    • 谢谢,有道理
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多