gpt4 book ai didi

selenium - Selenium 中是否可以通过部分 id 匹配来定位元素

转载 作者:行者123 更新时间:2023-12-01 21:23:19 26 4
gpt4 key购买 nike

我正在尝试使用生成的 id 来查找元素,其中 ID 的某些部分是已知的;例如:

id="page_x002e_x0023_default-create-firstname"

其中最后 3 个单词 (_default-create-firstname) 已知,但前面的任何内容都可能发生变化。这可能吗?

最佳答案

您可以申请ends-with CSS selector :

By.cssSelector("[id$=default-create-firstname]")

更新

自从发布答案以来已经过去了一段时间了。以下是链接的 Mozilla 开发者页面的一些更新和下面的评论:

新使用By.css代替By.cssSelector

By.css("[id$=default-create-firstname]")

另请参阅四种可能性

  • 开头
  • 里面的任何地方
  • 内部任意位置,无论大小写
  • 结束于
<小时/>
/* 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;
}

关于selenium - Selenium 中是否可以通过部分 id 匹配来定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31248804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com