Java 如何单击在 Selenium 中其 href 具有特定子字符串的链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19058637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:45:39  来源:igfitidea点击:

How to click a link whose href has a certain substring in Selenium?

javaselenium

提问by Buras

The following is a bunch of links <a elements.ONLY oneof them has a substring "long" as a value for the attribute href

以下是一堆链接,<a elements.只有其中一个链接有一个子字符串“long”作为属性 href 的值

<a class="c1" href= "very_lpng string" > name1 </a>
<a class="g2" href= "verylong string" > name2 </a>   // The one that I need
<a class="g4" href= "very ling string" > name3 </a>
<a class="g5g" href= "very ng string" > name4 </a>

...................

I need to click the link whose hrefhas substring "long" in it. How can I do this?

我需要单击其中href包含子字符串“long”的链接。我怎样才能做到这一点?

PS: driver.findElement(By.partialLinkText("long")).click();// b/c it chooses by the name

PS: driver.findElement(By.partialLinkText("long")).click();// b/c 它按名称选择

采纳答案by ddavison

I need to click the link who's href has substring "long" in it. How can I do this?

我需要单击其 href 中包含子字符串“long”的链接。我怎样才能做到这一点?

With the beauty of CSS selectors.

具有 CSS 选择器的美。

your statement would be...

你的陈述将是...

driver.findElement(By.cssSelector("a[href*='long']")).click();

This means, in english,

这意味着,在英语中,

Find me any 'a' elements, that have the hrefattribute, and that attribute contains'long'

为我找到任何具有href属性的'a' 元素,并且该属性为contains'long'

You can find a useful article about formulating your own selectors for automation effectively, as well as a list of all the other equality operators. contains, starts with, etc... You can find that at: http://ddavison.io/css/2014/02/18/effective-css-selectors.html

您可以找到有关如何有效地制定自己的选择器以实现自动化的有用文章,以及所有其他等式运算符的列表。contains, starts with, 等等...您可以在以下位置找到:http: //ddavison.io/css/2014/02/18/effective-css-selectors.html

回答by rt2800

use driver.findElement(By.partialLinkText("long")).click();

driver.findElement(By.partialLinkText("long")).click();

回答by Praveen

You can do this:

你可以这样做:

//first get all the <a> elements
List<WebElement> linkList=driver.findElements(By.tagName("a"));

//now traverse over the list and check
for(int i=0 ; i<linkList.size() ; i++)
{
    if(linkList.get(i).getAttribute("href").contains("long"))
    {
        linkList.get(i).click();
        break;
    }
}

in this what we r doing is first we are finding all the <a>tags and storing them in a list.Afterthat we are iterating the list one by one to find <a>tag whose href attribute contains long string. And then we click on that particular <a>tag and comes out of the loop.

在这里,我们首先要做的是找到所有<a>标签并将它们存储在list.After我们正在逐个迭代列表以找到<a>其 href 属性包含长字符串的标签。然后我们点击那个特定的<a>标签并退出循环。

回答by Nitish Kumar

With the help of xpath locator also, you can achieve the same.

在 xpath 定位器的帮助下,您也可以实现相同的目标。

Your statement would be:

你的陈述是:

driver.findElement(By.xpath(".//a[contains(@href,'long')]")).click();

And for clicking all the links contains long in the URL, you can use:-

要单击 URL 中包含很长的所有链接,您可以使用:-

List<WebElement> linksList = driver.findElements(By.xpath(".//a[contains(@href,'long')]"));

for (WebElement webElement : linksList){
         webElement.click();
}