I have the following HTML code for "Save" button:
<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:
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.
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. If text comes out, then there is a problem with xpath, you can analyze the exact run time xpath by using plugin of Firefox.
这应该工作-
String actualButtonCaption = driver.findElement(By.name("Submit")).getAttribute("value");
I have got the solution by using 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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.