getText()在sendKeys()之后返回空字符串

Chandrachud Nanduri

我有一个带有以下公共实例变量的类

@FindBy(id="titleInput")
public WebElement titleInputBox;

然后我在构造函数中使用页面工厂在每次使用时对其进行初始化

PageFactory.initElements(driver, this);

在此页面的测试用例中,我使用以下代码测试发送的文本是否确实在字段中设置了...

subtitleInputBox.sendKeys("Test");
subtitleInputBox.getText();

我得到空字符串

任何想法为什么会发生这种情况...我认为如果driver.findElement()直接使用不带@FindByPageFactory

索拉布·高尔

当您需要输入框元素值属性文本时,实际上WebElement.getText()返回此元素(包括子元素)的可见(即未被CSS隐藏)innerText,而没有任何前导或尾随空格。

FYI输入框元素将您尝试使用的文本存储在WebElement.sendKeys()其属性名称中,value而不是内部文本中。

因此,您应该尝试使用WebElement.getAttribute()后者来获取元素的给定属性的值。

在这里,您还需要执行以下步骤WebDriverWait来确定元素值是否已成功设置ExpectedConditions.textToBePresentInElementValue

subtitleInputBox.sendKeys("Test");

//Now wait until value has been set into element 
new WebDriverWait(driver, 10).until(ExpectedConditions.textToBePresentInElementValue(subtitleInputBox, "Test"));

//Now get the element value
subtitleInputBox.getAttribute("value");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章