0
<button class="btn">Enable</button>

This is my button element. I want to retrieve "Enable" back to my program.

if(driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/div[2]/div/div/div/div/div/table/tbody/tr[16]/td[4]/div/button")).getText.matches("Enable"))


{
    System.out.println("Test Pass");
    //return true;
}
else
{System.out.println("Test Failed");
    //return false;
} 
  • use getAttribute("value"); – Shailendra Rathore Jun 14 '17 at 9:00
  • What are you trying to this? Do you want to find text on that button element only or you hv to do something new? your test case will fail always as currently, you hv used " .matches("Enable"). – Bharat Mane Jun 14 '17 at 9:21
  • Let me know whether you are looking for button's enabled property or you wanted to verify button's text? – NarendraC Jun 15 '17 at 3:57
4
<button name="btnK" id="gbqfba" aria-label="Google Search" class="gbqfba"><span id="gbqfsa">Google Search</span></button>

Button tag has multiple attributes 'name', 'id', 'class' and 'aria-label' and has values for each attribute. To get the attribute value using selenium WebDriver, we can use element.getAttribute(attributeName)

In your case:

WebElement button = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/div[2]/div/div/div/div/div/table/tbody/tr[16]/td[4]/div/button");

    if(button.getAttribute("name").matches("Enable"))
    {
        System.out.println("Test Pass");
        //return true;
    }
    else{
       System.out.println("Test Failed");
        //return false;
    } 
0

There is a mistake. The function is .getText(). The brackets are missing in my code.

0

We can simply get the text from the button.

    WebElement buttonText = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/div[2]/div/div/div/div/div/table/tbody/tr[16]/td[4]/div/button"));

    if(buttonText.getText().equals("Enable")) {
        System.out.println("Test Pass");
        //return true;
    } else{
        System.out.println("Test Failed");
        //return false;
    }

Or

WebElement buttonText = driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/div[2]/div/div/div/div/div/table/tbody/tr[16]/td[4]/div/button"));
Assert.assertEquals("Buton Nmae", buttonText.getText());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.