如何在Selenium中单击其href具有某个子字符串的链接?

Bur*_*ras 49 java selenium

以下是一组链接,<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>

...................
Run Code Online (Sandbox Code Playgroud)

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

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

dda*_*son 97

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

With the beauty of CSS selectors.

your statement would be...

driver.findElement(By.cssSelector("a[href*='long']")).click();
Run Code Online (Sandbox Code Playgroud)

This means, in english,

Find me any 'a' elements, that have the href attribute, and that attribute 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


rt2*_*800 6

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

  • 谢谢你的回答.但是,这只有在我在链接名称中查找子字符串时才有效.如果要在`href`中查找子字符串,这不起作用 (6认同)