<a class="LinkDetail" href="/settings/carsettings?xyz=L_11:1:*:2&carid=199&carnumber=4294967295" target="_top" tabindex="23"/>在上面的链接中,我需要使用/settings/carsettings和carid=199来定位元素
使用CSS定位器。有人能让我知道同样的语法吗?还可以共享XPath的语法。
发布于 2014-02-10 08:00:59
请给我们看看你试过的东西,这样我们就可以说出你没有成功的地方。如果下面的CSS Selector/XPath不能工作,那么发布您的堆栈跟踪和更多的HTML代码,以找到最佳的定位器。
CSS选择器
a[href*='settings/carsettings'][href*='carid=199']XPath
.//a[contains(@href, 'settings/carsettings') and contains(@href, 'carid=199')]发布于 2014-02-10 08:00:51
您所需的可以使用以下代码实现:
//get all <a> tags in the webpage to a list
List<WebElements> aTags = driver.findElements(By.tagName("a"));
int index = 0;
//iterate through list of <a> tags
for (WebElement aTag: aTags) {
//get the href attribute of each <a> tag
String href = aTag.getAttribute("href");
//see if the href contains /settings/carsettings and carid=199
if (href.contains("/settings/carsettings")&&href.contains("carid=199")) {
//if it contains break out of for loop. This esssentially gives the index
break;
}
index++;
}
//get the required <a> tag using the index
WebElement required = aTags.get(index);如果这对你有帮助,请告诉我。
https://stackoverflow.com/questions/21671451
复制相似问题