【问题标题】:Getting empty value when using "getAttribute()"使用“getAttribute()”时获取空值
【发布时间】:2020-02-19 16:02:48
【问题描述】:

我正在尝试获取文本“Lior Pelet”,但我得到的是空字符串

这是网站代码:

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

这是我的代码:

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

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

【问题讨论】:

标签: java selenium-webdriver getattribute


【解决方案1】:

您可以使用getText() 方法而不是getAttribute() 方法来获取。
你可以这样做:

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

String sContactName=txtFullName.getText();

您可以使用getAttribute("value") 获得所需的输出。
你可以这样做:

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

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

【讨论】:

    【解决方案2】:
    css=".crm-entity-stream-content-detail > span
    

    表示你的“txtFullName”已经是一个 span 元素,所以请调用 getText() 方法。

    【讨论】: