是否可以通过 Selenium 中的部分 id 匹配来定位元素 [英] Is it possible to locate element by partial id match in Selenium

查看:36
本文介绍了是否可以通过 Selenium 中的部分 id 匹配来定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to locate elements with generated ids wherein some part of the ID is known; for example:

id="page_x002e_x0023_default-create-firstname"

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

in which the last 3 words (_default-create-firstname) are known but anything preceding that is subject to change. Is this possible?

推荐答案

您可以申请 ends-with CSS 选择器:

You can apply an ends-with CSS selector:

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

更新

自从发布答案以来已经过了一段时间.这里有一些来自链接的 mozilla 开发者页面的更新和下面的评论:

Some time went by since the answer was posted. Here some update from the linked mozilla developer page and comments below:

新使用 By.css 而不是 By.cssSelector

New use By.css instead of 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 中的部分 id 匹配来定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆