How to click a link whose href has a substring using Selenium webdriver?

0 votes

The following is a bunch of links <a >element. Only one of them has a substring "long" as a value for the attribute href.

<a class="g1" href= "very_lpng string" > name1 </a>
<a class="g2" href= "verylong string" > name2 </a>   // I need this one
<a class="g3" href= "very ling string" > name3 </a>
<a class="g4" href= "very ng string" > name4 </a>

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

driver.findElement(By.partialLinkText("long")).click(); // because it chooses by the name

Jul 11, 2018 in Selenium by QueenBee
• 1,810 points
4,122 views

1 answer to this question.

0 votes

It can be done by using the CSS selectors.​

The code would be:

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

Hope it helps!!

answered Jul 11, 2018 by Meci Matt
• 9,420 points

Related Questions In Selenium

0 votes
1 answer

How to Click a Link by href value in Selenium WebDriver?

Hi Ujjwal, you can use following code ...READ MORE

answered Aug 14 in Selenium by Abha
• 22,870 points
47 views
+2 votes
1 answer
0 votes
1 answer

How to click on a hyperlink using Selenium WebDriver?

Hi Jonathan, you can use click() method in ...READ MORE

answered May 29 in Selenium by Abha
• 22,870 points
205 views
0 votes
1 answer

How to open a browser window in full screen using Selenium WebDriver with C#

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,130 points
3,308 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
547 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
881 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
546 views
0 votes
5 answers
0 votes
1 answer

How to take screenshot of a frame using Selenium WebDriver?

you can use the below code: import java.awt.image.BufferedImage; import ...READ MORE

answered Jun 20, 2018 in Selenium by Meci Matt
• 9,420 points
661 views