在 Selenium Webdriver 中存储 WebElement 的属性值
Store the attribute's value of a WebElement in Selenium Webdriver
我有一个动态填充的 UL,每次单击 "Action" 按钮时也会动态生成 ID;有没有办法读取
的标签
<div class="class">
<ul id="dynamically generated">
<li class="li_class">
<li class="li_class">
<li class="li_class">
<li class="li_class">
<li class="li_class">
</ul>
</div>
既然你没有提到任何语言,我就用 JAVA syntax:
// String object 'field_value' is created
String fieldValue;
// The field value is retrieved by the getAttribute("id") Selenium WebDriver predefined method and assigned to the String object.
fieldValue = _driver.findElement(By.ById("dynamically generated")).getAttribute("id")
它将获取元素的给定属性的值。将 return 当前值,即使这在页面加载后已被修改。
我知道你已经接受了一个答案,但如果 ID 的某些部分是稳定的,你可以搜索部分匹配。例如,假设您有一个元素
<span id="ctl00_SomeStableTextGoesHere_12345" ...>
其中 12345
是动态生成的。您可以使用 CSS 选择器搜索 ID 的开头。
driver.findElement(By.cssSelector("span[id^='ctl00_SomeStableTextGoesHere_']"));
还有其他选项:
^
开头为
$
结尾为
*
包含
我有一个动态填充的 UL,每次单击 "Action" 按钮时也会动态生成 ID;有没有办法读取
的标签<div class="class">
<ul id="dynamically generated">
<li class="li_class">
<li class="li_class">
<li class="li_class">
<li class="li_class">
<li class="li_class">
</ul>
</div>
既然你没有提到任何语言,我就用 JAVA syntax:
// String object 'field_value' is created
String fieldValue;
// The field value is retrieved by the getAttribute("id") Selenium WebDriver predefined method and assigned to the String object.
fieldValue = _driver.findElement(By.ById("dynamically generated")).getAttribute("id")
它将获取元素的给定属性的值。将 return 当前值,即使这在页面加载后已被修改。
我知道你已经接受了一个答案,但如果 ID 的某些部分是稳定的,你可以搜索部分匹配。例如,假设您有一个元素
<span id="ctl00_SomeStableTextGoesHere_12345" ...>
其中 12345
是动态生成的。您可以使用 CSS 选择器搜索 ID 的开头。
driver.findElement(By.cssSelector("span[id^='ctl00_SomeStableTextGoesHere_']"));
还有其他选项:
^
开头为$
结尾为*
包含