Dar*_*ar1 36 selenium selenium-webdriver
我正在尝试使用生成的ID定位元素,其中ID的某些部分是已知的; 例如:
id="page_x002e_x0023_default-create-firstname"
Run Code Online (Sandbox Code Playgroud)
其中最后3个单词(_default-create-firstname)已知但前面的任何内容都可能发生变化.这可能吗?
ale*_*cxe 51
By.cssSelector("[id$=default-create-firstname]")
Run Code Online (Sandbox Code Playgroud)
更新
自答案公布以来,有一段时间过去了.这里有一些来自链接的mozilla开发者页面和评论的更新:
新用By.css而不是By.cssSelector
By.css("[id$=default-create-firstname]")
Run Code Online (Sandbox Code Playgroud)
另见四种可能性
/* Internal links, beginning with "#" */
a[href^="#"] {
background-color: gold;
}
/* Links with "example" anywhere in the URL */
a[href*="example"] {
background-color: silver;
}
/* Links with "insensitive" anywhere in the URL,
regardless of capitalization */
a[href*="insensitive" i] {
color: cyan;
}
/* Links that end in ".org" */
a[href$=".org"] {
color: red;
}
Run Code Online (Sandbox Code Playgroud)
Cat*_*hal 22
如果你想沿着xpath路线走,那么你可以使用contains(),如下所示:
//*[contains(@id,'_default-create-firstname')]
Run Code Online (Sandbox Code Playgroud)
这将在整个页面中搜索包含文本"_default-create-firstname"的id.它可以很容易地变得更具体
小智 18
你可以在Java-Selenium中尝试这个:你可以使用ends-with()或者contains().我认为两者都应该奏效.
driver.findElement(By.xpath("//*[ends-with(@id,'_default-create-firstname')]"));
driver.findElement(By.xpath("//*[contains(@id, '_default-create-firstname')]"));
Run Code Online (Sandbox Code Playgroud)
这适用于selenium vba
.FindElementsByXPath( "//*[contains(@class, 'unique-string')]" )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57990 次 |
| 最近记录: |