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

+1 vote

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,820 points
13,546 views

1 answer to this question.

+1 vote

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,460 points
Thanks it helps
You're welcome. Glad it worked for you. Please feel free to ask any other questions if you have.

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, 2019 in Selenium by Abha
• 28,140 points
4,798 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, 2019 in Selenium by Abha
• 28,140 points
7,255 views
0 votes
1 answer
0 votes
2 answers

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,500 points
13,773 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,188 views
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,910 points
7,263 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,902 views
0 votes
8 answers

How to open a link in new tab of chrome browser using Selenium WebDriver?

This below code works for me in ...READ MORE

answered Dec 14, 2020 in Selenium by Gitika
• 65,770 points
103,150 views
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,460 points
3,509 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP