使用标签中的硒访问元素

access the element using selenium from label

我有以下html代码

<label class="error" id="field">Invalid format </label>

我正在尝试使用以下命令获取值 无效格式 :

System.out.println(driver.findElement(By.id("field")).getText());

但它 returns 没什么。

请告诉我获取值的代码使用 selenium

的格式无效

我假设您的标签已经可见,请使用以下代码获取文本:

 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 System.out.println(driver.findElement(By.xpath("//*[@id='field']")).getText());