how to find element by partial href in selenium
Continuous Test Orchestration And Execution Platform Online
Perform automatic and live-interactive testing on 3000+ existent desktop and mobile devices online.
Find Elements With Link Text & Partial Link Text In Selenium
Sadhvi Singh
Posted On: June 17, 2019
192270 Views
6 Min Read
This article is a office of our Content Hub. For more than in-depth resources, check out our content hub on Selenium Locators Tutorial.
CSS locators in Selenium are a fundamental concept that every tester who aims to perform automation testing with Selenium, should be aware of. Proficient employ of CSS locators in Selenium can help you perform testing in a more efficient & thorough fashion. I accept been in the automation testing industry from 7 years now, & I often find that testers when performing Selenium, tend to forget the purpose of each CSS locator. Freshers have a tough fourth dimension understanding them & experienced tester oftentimes sticks to a couple of selectors for locating elements on a webpage as they execute automation testing with Selenium.
There accept been times where I meet an experienced automation tester taking a longer route for finding an chemical element equally they have a addiction of sticking to their favorite locator. It is why I thought I would come up with a tutorial series for CSS Selenium locators to assistance budding automation testers come up with strategic implementation of these locators. And to those of us who are experienced, information technology would be a quick and adept recap.
This commodity will represent the applied implementation of link text & partial link text, as we perform automation testing with Selenium. The links on any web application can aid in locating an element with either exact friction match of the text or through a partial match. Using link text & fractional link text in Selenium, nosotros will be able to locate both of these matches. This is the terminal article of my tutorial serial on CSS Locator in Selenium.
Yous tin can check out other manufactures around different CSS locator in Selenium that helps in locating elements through various ways:
- ID locator in Selenium
- Name locator in Selenium
- Class Name locator in Selenium
- Tagname locator in Selenium
- CSS Selector in Selenium
- XPath in Selenium
If y'all are an advanced or medium Selenium practitioner, then you can chuck on dedicated articles mentioned above. And go for our complete guide to aid you illustrate the applied demonstration of CSS locator in Selenium.
Cheque Out My Consummate Guide For Every CSS Locator In Selenium WebDriver With Examples
With that said, let's observe out how to leverage link text & partial link text in Selenium to locate elements on a web page.
This certification is for anyone who wants to stay alee among professionals who are growing their career in Selenium automation testing.
Here's a short glimpse of the Selenium 101 certification from LambdaTest:
Using Link Text In Selenium To Locate An Element
In social club to admission link using link text in Selenium, the below-referenced lawmaking is used:
driver.findElement(Past.linkText("this is a link text"));
Note: In a scenario where, multiple links having similar text exists, information technology volition automatically select the first one.
Let's refer the code snippet below to understand the utilise instance. In this example, we are taking Airbnb example, where nosotros are clicking on any one stay of Goa through the link match.
Referenced screenshot of the div element with the link text:
one two iii 4 v vi 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java . util . concurrent . TimeUnit ; import org . openqa . selenium . By ; import org . openqa . selenium . WebDriver ; import org . openqa . selenium . chrome . ChromeDriver ; import org . testng . Assert ; public grade LinkText { public static void main ( String [ ] args ) { // TODO Auto-generated method stub Organization . setProperty ( "webdriver.chrome.driver" , ".\\ChromeDriver\\chromedriver.exe" ) ; WebDriver driver = new ChromeDriver ( ) ; driver . manage ( ) . timeouts ( ) . implicitlyWait ( 20 , TimeUnit . SECONDS ) ; driver . manage ( ) . window ( ) . maximize ( ) ; //Opening the air bnb Goa homestays page driver . go ( "https://www.airbnb.co.in/s/Goa/all" ) ; //locating an element via link text in Selenium now, and clicking on that stay commuter . findElement ( By . linkText ( "Standard 1 Bedroom Suite with Pool & Jacuzzi" ) ) . click ( ) ; driver . quit ( ) ; } } |
We tin can locate the same chemical element using partial link text in Selenium too. Allow's check how!
Using Partial Link Text In Selenium To Locate An Element
Partial link text in Selenium is another way of locating an element via a link. The only deviation from the link text in Selenium to partial link text is that it does non wait into the exact match of the cord value but piece of work on a partial match. And then, in case you lot are looking for a link with a bigger text length, y'all can get away from using only the partial link text rather than the whole link text in Selenium.
Syntax of locating element by fractional link text.
driver.findElement(Past.partialLinkText ("link text"));
Referencing the above scenario, below is the code snippet for partial link text of the aforementioned stay of Airbnb:
1 two 3 4 v vi seven 8 nine 10 xi 12 13 14 xv xvi 17 eighteen 19 xx 21 22 23 24 25 26 27 28 | import java . util . concurrent . TimeUnit ; import org . openqa . selenium . By ; import org . openqa . selenium . WebDriver ; import org . openqa . selenium . chrome . ChromeDriver ; import org . testng . Assert ; public class PartialLinkText { public static void chief ( Cord [ ] args ) { // TODO Auto-generated method stub System . setProperty ( "webdriver.chrome.driver" , ".\\ChromeDriver\\chromedriver.exe" ) ; WebDriver driver = new ChromeDriver ( ) ; driver . manage ( ) . timeouts ( ) . implicitlyWait ( 20 , TimeUnit . SECONDS ) ; driver . manage ( ) . window ( ) . maximize ( ) ; //Opening the air bnb Goa homestays folio driver . become ( "https://www.airbnb.co.in/s/Goa/all" ) ; //locating an element via link text now and clicking on that stay driver . findElement ( By . partialLinkText ( "Pool & Jacuzzi" ) ) . click ( ) ; driver . quit ( ) ; } } |
How To Select The Right Link Text When You Have Multiple Match Results?
The only thing to think and to be cautious of while using fractional link text in Selenium is when partial link text ends upwardly matching with multiple link text on the page. In this case, make certain, you are clicking on the desired one.
Let's consider another scenario as we perform automation testing with Selenium, where y'all finish upward matching multiple link text and wish to target only the designated one. For the aforementioned homestays of Goa, I am trying to locate the chemical element with partial text as 'pool' in it. So, the strategy would exist to find out domicile many stays have a puddle in them and click on the required stay, post that. The reference lawmaking snippet beneath represents how we can pick the right target out of multiple match results using the fractional link text in Selenium.
i 2 iii iv v 6 seven eight 9 ten 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 forty 41 42 43 44 45 | import coffee . util . Listing ; import java . util . concurrent . TimeUnit ; import org . openqa . selenium . By ; import org . openqa . selenium . WebDriver ; import org . openqa . selenium . WebElement ; import org . openqa . selenium . chrome . ChromeDriver ; import org . testng . Affirm ; public class LinkText { public static void main ( String [ ] args ) { // TODO Auto-generated method stub System . setProperty ( "webdriver.chrome.driver" , ".\\ChromeDriver\\chromedriver.exe" ) ; WebDriver driver = new ChromeDriver ( ) ; driver . manage ( ) . timeouts ( ) . implicitlyWait ( twenty , TimeUnit . SECONDS ) ; commuter . manage ( ) . window ( ) . maximize ( ) ; //Opening the air bnb Goa homestays page commuter . go ( "https://www.airbnb.co.in/s/Goa/all" ) ; //locating an element via link text at present and clicking on that stay List & lt ; WebElement & gt ; poolNumber = commuter . findElements ( By . partialLinkText ( "Pool" ) ) ; //find the number of links with the text as pool int numberOfStaysWithPool = poolNumber . size ( ) ; Organization . out . println ( numberOfStaysWithPool ) ; for ( int k = 0 ; k & lt ; numberOfStaysWithPool ; thousand ++ ) { //press all those links System . out . println ( poolNumber . become ( k ) . getText ( ) ) ; } //select the luxury three sleeping room apartment link poolNumber . get ( 2 ) . click ( ) ; commuter . quit ( ) ; } } |
In the to a higher place code snippet, I accept used findElements since I am supposed to receive multiple web elements with partial text as 'pool'. At present, using an index, I take navigated to one of the links, which I wish to click. Elementary, isn't it?
Notation: Both, link text & fractional link text are cases sensitive every bit CSS locator in Selenium.
For example, Presume that a link 'Register' is nowadays on a home page and a similar link 'REGISTER' is present on the footer of the homepage. In this case, if you are locating with link text 'Annals', it volition automatically select the link in the footer and not the other one.
Link text and partial link text locator in Selenium works just on links of a given web application. If you lot intend to locate elements other than links you cannot utilize link text or partial link text locator in Selenium. If y'all are dealing with links in your application, then this is perhaps the best locator to become with. Happy testing! 🙂
Got Questions? Drop them on LambdaTest Community. Visit now
Examination your websites, web-apps or mobile apps seamlessly with LambdaTest.
- Selenium, Cypress, Playwright & Puppeteer Testing
- Existent Devices Deject
- Native App Testing
- Appium Testing
- Live Interactive Testing
- Smart Visual UI Testing
Book a Demo
Source: https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/
Posted by: benoithoughle.blogspot.com
0 Response to "how to find element by partial href in selenium"
Post a Comment