getAttribute("value") 返回空值,为什么?

Suj*_*it 5 selenium-webdriver

我的代码是

String OTP = driver.findElement(By.xpath("//span[@id='otp']")).getAttribute("value");
System.out.println(OTP);
Run Code Online (Sandbox Code Playgroud)

Gra*_*per 2

假设您正在尝试获取跨度中的文本。getAttribute("value")仅适用于文本框、文本区域。供 span div 和其他使用getText()

driver.findElement(By.xpath("//span[@id='otp']")).getText();
Run Code Online (Sandbox Code Playgroud)

  • @Sujit然后你将不得不等待文本在跨度中发生变化,或者就像黑客添加睡眠来检查一样。如何将相关的 html 源添加到您的问题中? (2认同)