我们是在使用Actions类悬停之前获取工具提示文本还是在悬停之后获取呢?

huangapple go评论93阅读模式
英文:

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

问题

我抱歉,如果我重复了这个问题。我在这个网站上查看了很多答案,但仍然无法获取工具提示文本。
如果我不使用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();  // (我也尝试了clickAndHold方法)
WebElement toolTip = driver.findElement(By.xpath("//*[@id='age']"));

// 获取工具提示文本并进行断言
String toolTipText = toolTip.getAttribute("title");
System.out.println("toolTipText-->" + toolTipText);
英文:

I apologize if I'm repeating this question. I've been through a lot of answers on this site but I'm still not getting the tool-tip text.
I can read the title if I don't use the Actions class to hover-over and display the tool-tip.
However once I use the Actions class to display the tool-tip then title is always empty. I don't want to get the text before I
hover-over, isn't the whole idea to read the tool-tip text that is displayed?

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

答案1

得分: 1

工具提示 仅在执行鼠标悬停之后提取。

要打印出 [tag:tooltip] 悬停在字段上以查看工具提示。 作为所需元素位于 <iframe> 内,您需要执行以下操作:

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());
  • 控制台输出:
我们只为统计目的询问您的年龄
英文:

Tooltips are extracted only after mouse over is performed.

To print the [tag:tooltip] Hover the field to see the tooltip. as the desired element is within a &lt;iframe&gt; you need to:

  • scrollIntoView() the desired iframe

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for visibilityOfElementLocated() of the element you need to Mouse Hover.

  • Induce WebDriverWait for visibilityOfElementLocated() for the element from where you need to retrieve the tooltip:

  • You can use the following [tag:xpath] based Locator Strategies:

    System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;C:\\WebDrivers\\chromedriver.exe&quot;);
    ChromeOptions options = new ChromeOptions();
    options.addArguments(&quot;--start-maximized&quot;);
    options.setExperimentalOption(&quot;excludeSwitches&quot;, Collections.singletonList(&quot;enable-automation&quot;));
    options.setExperimentalOption(&quot;useAutomationExtension&quot;, false);
    WebDriver driver =  new ChromeDriver(options);
    driver.get(&quot;https://jqueryui.com/tooltip/&quot;);
    ((JavascriptExecutor)driver).executeScript(&quot;return arguments[0].scrollIntoView(true);&quot;, new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//h1[@class=&#39;entry-title&#39;]&quot;))));
    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(&quot;//iframe[@src=&#39;/resources/demos/tooltip/default.html&#39;]&quot;)));
    new Actions(driver).moveToElement(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//input[@id=&#39;age&#39;]&quot;)))).build().perform();
    System.out.println(new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;//input[@id=&#39;age&#39;]//following::div[text()]&quot;))).getText());
    
  • Console Output:

    We ask for your age only for statistical purposes.
    

huangapple
  • 本文由 发表于 2020年7月22日 04:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63022422.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定