Search code examples
javaselenium-webdrivergetattribute

Getting empty value when using "getAttribute()"


I'm trying to get the text "Lior Pelet", but i'm getting empty string

This is the site code:

<div class="crm-entity-stream-content-detail"><span>Lior Pelet</span></div>

And this is my code:

@FindBy(css=".crm-entity-stream-content-detail > span")
public WebElement txtFullName;  //full name 

String sContactName=txtFullName.getAttribute("span");

Solution

  • You can fetch by using getText() method instead of using the getAttribute() method.
    You can do it like:

    @FindBy(css=".crm-entity-stream-content-detail > span")
    public WebElement txtFullName;  //full name 
    
    String sContactName=txtFullName.getText();
    

    OR

    You can get the desired output by using getAttribute("value") .
    You can do it like:

    @FindBy(css=".crm-entity-stream-content-detail > span")
    public WebElement txtFullName;  //full name 
    
    String sContactName=txtFullName.getAttribute("value");