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

Rip*_*sim 0 java caption selenium-webdriver

我有"保存"按钮的以下HTML代码:

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

我想检索按钮的标题.我使用以下代码来做到这一点:

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

我也使用绝对xpath如下:

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

但不幸的是,没有检索到任何文字.找到空白/空白文本.有谁能够帮我?

小智 10

getAttribute 方法可用于检索属性值.

在这种情况下,以下将返回标题:

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