简体   繁体   English

如何使用Java在WebDriver中获取按钮的标题/文本

[英]How to get caption/text of button in WebDriver using Java

I have the following HTML code for "Save" button: 我的“保存”按钮具有以下HTML代码:

<input type="submit" onclick="return confirm('Sure to change global settings?')" value="Save" name="submit">

I want to retrieve the caption of button. 我想检索按钮的标题。 I used the following code to do that: 我使用以下代码来做到这一点:

String actualButtonCaption = driver.findElement(By.xpath("//input[@value='Save']")).getText();

I also used the absolute xpath as below: 我还使用了绝对的xpath,如下所示:

String actualButtonCaption = driver.findElement(By.xpath("//html/body/form/div[3]/div[2]/input")).getText();

But unfortunately, no text was retrieved. 但是很遗憾,没有找到任何文本。 Blank/empty text was found. 找到空白/空文本。 Can anybody help me? 有谁能够帮助我?

getAttribute method could be used to retrieve the attribute values. getAttribute方法可用于检索属性值。

In this case following would return the caption: 在这种情况下,以下将返回标题:

driver.findElement(By.XPath("//input[@name='submit']")).getAttribute("value");

try associating an ID with input and then find element by ID. 尝试将ID与输入相关联,然后按ID查找元素。 If text comes out, then there is a problem with xpath, you can analyze the exact run time xpath by using plugin of Firefox. 如果出现文本,则说明xpath存在问题,您可以使用Firefox插件分析确切的运行时xpath。

这应该工作-

String actualButtonCaption = driver.findElement(By.name("Submit")).getAttribute("value");

I have got the solution by using JavaScript. 我已经通过使用JavaScript获得了解决方案。 The code is as below: 代码如下:

WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor) driver;
String ss = (String)jse.executeScript("var x=document.getElementsByName('submit')[0].value; return x");
System.out.println("Caption of Save button: " + ss);

It works fine. 工作正常。 The caption of button is printed as "Save" 按钮的标题打印为“保存”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java从文本框Webdriver获取类值 - how to get a class value from text box Webdriver using Java 如何通过与Java一起使用Selenium WebDriver从此HTML代码中获取文本 - How to get Text from this HTML code By using Selenium WebDriver with Java 带有 Java 的 WebDriver - 无法使用 Webdriver 获取所有文本 - WebDriver with Java - Not able to get all the text using Webdriver 如何使用Java和WebDriver来获取textArea? - How get textArea using Java with WebDriver? 如何使用Java在WebDriver中获取当前月份 - How to get current month in webdriver using java 使用Java中的Selenium Webdriver获取文本 - Get Text with Selenium Webdriver in Java 如何使用带有Java的Selenium Webdriver从包围在多个span标签中的span元素中获取实际文本 - How to get the actual text from a span element enclosed within multiple span tags, using Selenium Webdriver with Java 如何使用带有Java的Selenium Webdriver从登录页面上的图像徽标获取文本 - how to get text from image logo on Login page using Selenium Webdriver with java 是否可以使用Java使用Selenium WebDriver从验证码图像中获取文本? - Is it possible to get text from captcha image with Selenium WebDriver using Java? 如何仅从子元素中获取文本 - Webdriver - Java - How to get the text only from the child element - Webdriver - Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM