In my last post, I discussed how to find an element with Webdriver using the element id, class name, or name. In this post, I will discuss how to find an element using link text, partial link text, or tag name.
Return to www.google.com, and look at the links on the bottom of the page. Right-click on the link that says “Advertising” and select “Inspect Element”. You should see something like this:
<a class=”_Gs” href=”//www.google.com/intl/en/ads/?fg=1″>Advertising</a>
The link text is what is found between the >reverse brackets<. In this case, the link text is “Advertising”.
Return to myFirstTest in Eclipse and replace the findElement command in your test with:
driver.findElement(By.linkText(“Advertising”)).click();
This command tells Webdriver to:
1. Find the element with the link text “Advertising”
2. Click on the link
If you followed the instructions in the last post, you should still have a breakpoint set in your test. If not, right-click in the left margin of the “driver.quit();” command and select “Insert Breakpoint”. Then run the test by right-clicking on the test and selecting Debug As->JUnit Test. The test should run and will stop just after it clicks on the Advertising link. If the test has run correctly, you should now be on the Google Ads page. Click on the green arrow to finish the test.
Now we will find an element using a partial link text. Return to www.google.com, right-click on the link at the bottom of the page that says “Business”, and select “Inspect Element”. You should see something like this:
<a class=”_Gs” href=”//www.google.com/services/?fg=1″>Business</a>
The link text is “Business”. We will find this link using just a portion of the text: “ness”.
Return to myFirstTest in Eclipse and replace the findElement command in your test with:
driver.findElement(By.partialLinkText(“ness”)).click();
Run the test by right-clicking on the test and selecting Debug As->JUnit Test. The test should run and will stop just after it clicks on the Business link. If the test has run correctly, you should now be on the Google Business Solutions page. Click on the green arrow to finish the test.
Now we will find an element using a tag name. Go to www.pinterest.com, right-click on the button that says “Continue with Facebook” and select “Inspect Element”. You should see something like this:
<button class=”Button Module btn hasText large rounded unAuthFacebookConnect registerLoginButton unauthHomeRegisterButton multiStepRedesign” type=”button”>
Note that the first word that appears after the “<” is “button”. This is the tag name of the element.
Return to myFirstTest in Eclipse and replace the String URL line with:
String URL = ‘http://www.pinterest.com’
Next, replace the findElement command in your test with:
driver.findElement(By.tagName(“button”)).click();
This command tells Webdriver to:
1. Find the element with the tag name “button”
2. Click on the button
Run the test by right-clicking on the test and selecting Debug As->JUnit Test. The test should run and will stop just after clicking on the button. If the test has run correctly, you will see a popup prompting you to log in with Facebook. Click on the green arrow to finish the test.
You have successfully located a web element by using an link text, a partial link text, and a tag name! Before we proceed to finding a web element by css and xpath, it is first necessary to understand the DOM. This will be the topic of my next blog post.