简体   繁体   English

如何获得与通配符CSS选择器匹配的第二个元素?

[英]How do I get the second element which matches a wildcard css selector?

I have a page with two buttons share the similar id in the pattern of button A id be "ABCNN_xxx" and button B id be "ABC(NN+1)_xxx" which NN is a number that being different on other pages with the same logic. 我有一个页面,其中两个按钮在按钮A模式为“ ABCNN_xxx”和按钮B id为“ ABC(NN + 1)_xxx”的模式下共享相似的ID,其中NN是一个数字,该数字在其他具有相同按钮的页面上是不同的逻辑。 My script needs to get those button universally on different pages so I use a wildcard css selector. 我的脚本需要在不同页面上通用获取这些按钮,因此我使用通配符css选择器。 However, since the two buttons share the same pattern, the wildcard selector would catch only the first button (A, with smaller NN number) it sees but cannot locate the second one. 但是,由于两个按钮共享相同的模式,所以通配符选择器将仅捕获它看到的第一个按钮(A,NN号较小),但找不到第二个按钮。

I tried to put [2] behind the selector but seems it doesn't work like that. 我试图将[2]放在选择器后面,但似乎无法正常工作。 I am using the below css selector. 我正在使用下面的CSS选择器。 It will get me the first match but not the remaining match. 这将使我获得第一场比赛,但不会获得其余比赛。

driver.findElement(By.cssSelector("img[id^='ABC'][id$='_xxx']")).click();

Besides getting me ABC36_xxx for the first time, I would like to have ABC37_xxx for the second call instead of returning the first result. 除了第一次让我获得ABC36_xxx之外,我还希望第二次通话具有ABC37_xxx,而不是返回第一个结果。 How should I achieve my objective? 我应该如何实现我的目标?

you could select all into an array and loop over them using findElement s : driver.findElements(By.cssSelector("img[id^='ABC'][id$='_xxx']")).click(); 您可以选择将所有使用findElement 的阵列和环比他们: driver.findElements(By.cssSelector("img[id^='ABC'][id$='_xxx']")).click(); Or you could add a flag class after clicking it: 或者,您可以在单击它后添加标志类:

  • after the click add a "seleniumClicked" class to the button 单击后,将“ seleniumClicked”类添加到按钮
  • then change your selector to driver.findElements(By.cssSelector("img[id^='ABC'][id$='_xxx']:not(.seleniumClicked)")).click(); 然后将选择器更改为driver.findElements(By.cssSelector("img[id^='ABC'][id$='_xxx']:not(.seleniumClicked)")).click();

If you sure with the locator : By.cssSelector("img[id^='ABC'][id$='_xxx']") and it more than one, then collect them by : 如果您确定使用定位符: By.cssSelector("img[id^='ABC'][id$='_xxx']")并且不止一个,请通过以下方式收集它们:

List<WebElement> elmnts = driver.findElements(By.cssSelector("img[id^='ABC'][id$='_xxx']"));

And this to get second element : 这是第二个要素:

elmnts.get(1).click();

Import this : 导入此:

import java.util.List;
import org.openqa.selenium.WebElement;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Jsoup获取元素的CSS选择器? - How do I get the CSS selector of an element with Jsoup? 我如何从 dropdownmenu 中获取 Id、className 或 CSS Selector,它是 java 中 selenium 的 div - How do I get Id, className, or CSS Selector from dropdownmenu that is div for selenium in java 如何在正则表达式中获得通配符的匹配部分? - How do I get the matched part of a wildcard in a regular expression? 给定一个bySelector,我如何获得该元素的孩子的选择器? - given a bySelector how can i get the selector of this element's child? Selenium CSS 选择器只获取第一个元素。 如何检索所有元素 - Selenium CSS Selector get only first element. How to retrieve all the elements 如何获得集合的第二个元素? - How to get a second element of a collection? 如何使用JAVA将所有CSS属性应用到网页上的任何元素? - How do I get all CSS properties applied on any element in a webpage using JAVA? 如何通过 JavaFX WebEngine 通过 CSS 类名获取 HTML 元素? - How do I get an HTML Element By CSS class name via a JavaFX WebEngine? 在java中,哪个HTML解析器可以使用HTML选择器获取Element? - in java,which HTML parser can use HTML selector to get Element? 如何使用Selenium WebDriver获取CSS选择器? - How to get a CSS selector using Selenium WebDriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM