英文:
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>
内,您需要执行以下操作:
- 使用
scrollIntoView()
将所需的 iframe 滚动至可见。 - 引入 WebDriverWait 来等待所需的 frameToBeAvailableAndSwitchToIt。
- 引入 WebDriverWait 来等待要执行鼠标悬停的元素的 visibilityOfElementLocated()。
- 引入 WebDriverWait 来等待要检索工具提示 的元素的 visibilityOfElementLocated():
- 您可以使用以下基于 [tag:xpath] 的 定位策略:
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 <iframe>
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("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());
-
Console Output:
We ask for your age only for statistical purposes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论