我想获得一条工具提示消息,该消息会在鼠标悬停在图标上时出现。我想在 Selenium gettext()
方法中获取消息并将其分配给 String
。
下面是我的代码:
JavascriptExecutor jse = (JavascriptExecutor)driver;
String script = "return driver.findElement(By.xpath(ObjectRepository.tooltipMsg)).getText();";
String message = ((JavascriptExecutor) driver).executeScript(script).toString();
Thread.sleep(3000);
System.out.println("message "+message);
这不起作用,我收到错误消息 driver is not defined
最佳答案
错误 driver is not defined
由于以下语句而被抛出。
return driver.findElement(By.xpath(ObjectRepository.tooltipMsg)).getText();
这实际上不是Javascript,而是用于查找webelement 文本的Java 代码。因此,在创建 javascript 时不能直接使用在 java 中定义的驱动程序。
使用 Javascript:
String script = "return document.getElementById("your-id").innerHTML;";
String message = ((JavascriptExecutor) driver).executeScript(script).toString();
使用 Java:
String message = driver.findElement(By.xpath("your XPath")).getText();
您需要在上面提供正确的 XPath 或 Id 才能找到该元素。如果您有任何疑问,请告诉我。
关于javascript - 如何在 Selenium 中获取 javascript 工具提示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32731435/