【发布时间】:2019-07-23 07:47:42
【问题描述】:
我使用了以下代码:
driver.getPageSource().contains("My value in text box");
这会让我知道,元素是否存在于 dom 中。
现在,我需要知道,包含文本框“我在文本框中的值”的值。这个文本框的id是什么。
【问题讨论】:
标签: java selenium selenium-webdriver webdriver getattribute
我使用了以下代码:
driver.getPageSource().contains("My value in text box");
这会让我知道,元素是否存在于 dom 中。
现在,我需要知道,包含文本框“我在文本框中的值”的值。这个文本框的id是什么。
【问题讨论】:
标签: java selenium selenium-webdriver webdriver getattribute
要提取dom 元素 的id,您无需调用getPageSource()。您可以简单地使用getAttribute() 方法,如下所示:
String elementID = driver.findElement(By.xpath("//*[contains(text(),'My value in text box')]")).getAttribute("id");
【讨论】: