问题描述
我有以下需要查找的HTML元素.现在,将这个"id"添加到名称以int"0"表示的方式是动态的.最后会改变,但我知道会是什么."0--0"中的第一个int也会发生变化,但这无关紧要.
I have the below HTML element that I need to find. Now, this "id" name is dynamic in the way that the int "0" at the end will change, but I know what it will be. The first int in "0-0" will also change, but it doesn't matter what it will be.
<div id="ui-select-choices-row-0-0">
我尝试了以下代码,查找以#ui-select-choices-row-"开头的元素.并以所需的"int"输入结尾,但未按预期找到它.关于我在这里做错什么的任何建议吗?
I've tried the below code that looks for an element that starts with "#ui-select-choices-row-" and ends with the desired input of "int", but it's not finding it as expected. Any suggestions on what I'm doing wrong here?
尝试1:
driver.findElement(By.cssSelector("div[id^='#ui-select-choices-row-'] and div[id$='"+int+" div']"));
尝试2:
driver.findElement(By.cssSelector("div[id^='ui-select-choices-row-'] and div[id$='"+int+"']"));
推荐答案
您已经足够亲密.您需要从 id
的开头删除 #
,因为 #
本身表示 id
属性.
You were close enough. You need to remove the #
from the beginning of the id
as #
itself denotes the id
attribute.
要仅考虑 href
和 data-drupal-link-system-path
属性的静态部分,可以在 css-selectors :
To consider only the static part of href
and data-drupal-link-system-path
attributes you can use the following wildcards in css-selectors:
-
$ :表示属性值结尾
^ :表示属性值起始于
因此,最精细的css_selector将是:
So the most granular css_selector would be :
div[id^='ui-select-choices-row-'][id$='" +count+ "']
解决方案
您的有效代码行将是:
Solution
Your effective line of code will be:
driver.findElement(By.cssSelector("div[id^='ui-select-choices-row-'][id$='" +count+ "']"));
注意: int
是关键字,使用其他名称作为变量名称.
Note: int
is a keyword, use other names as the variable name.
您可以在以下位置找到几个相关的详细讨论:
You can find a couple of relevant detailed discussions in:
这篇关于Java Selenium Webdriver表达式查找以ccs开头和结尾的ccs动态元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!