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

查看:410
本文介绍了是否可以通过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?

推荐答案

您可以应用 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天全站免登陆