我们应该在 hover-over 使用操作 class 之前还是在 hover-over 之后获取工具提示文本?

Should we get the tooltip text before we hover-over using the Actions class or after we hover-over?

如果我重复这个问题,我深表歉意。我已经在这个网站上找到了很多答案,但我仍然没有收到 tool-tip 文本。 如果我不使用 Actions class 到 hover-over 并显示 tool-tip,我可以阅读标题。 但是,一旦我使用 Actions class 来显示 tool-tip,那么标题总是空的。我不想在我之前收到短信 hover-over,阅读显示的 tool-tip 文本不是整个想法吗?

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);

Tooltips are extracted only after 执行。

要打印 将鼠标悬停在字段上以查看工具提示。 因为所需元素在 <iframe> 中,您需要:

  • scrollIntoView()想要的

  • 为所需的 frameToBeAvailableAndSwitchToIt.

    引入
  • 诱导 for visibilityOfElementLocated() of the element you need to .

  • 诱导WebDriverWait for visibilityOfElementLocated() for the element from where you need to retrieve the :

  • 您可以使用以下 based :

    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.