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
2,763 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,400 points

Related Questions In Selenium

+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
• 9,340 points
28 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
2,227 views
0 votes
1 answer

How to click a span with given text using Selenium Python

If ALL_USA is subject to change, then ...READ MORE

answered Jul 26, 2018 in Selenium by Samarpit
• 5,130 points
1,326 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,050 points
145 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
540 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
138 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,400 points
503 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.