click( ) is a predefined method of Selenium's 'WebDriver' Class which is used for clicking Links and Buttons.
In this post, lets use click( ) predefined method for clicking a Link.
Lets Implement This:
Pre-requisites:
1. Create a new Java Project say 'WebDriver-Project2' in Eclipse IDE
2. Configure the Project to work with Selenium WebDriver
3. Create a package say 'package2' under the newly created project.
4. Create a Java Class file say 'Class2' under the newly created package as shown below:
Actual Steps:
1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:
2. The above code will create a Selenium WebDriver object, Launch a new Firefox Browser session, opens the http://book.theautomatedtester.com in the Browser window and finally it maximizes the Browser window as shown below:
3. Now lets write the Selenium WebDriver Automation code using the Selenium WebDriver Command that clicks the 'Chapter1' link in the above screen. But in order to click the 'Chapter1' link, we have to first find the locator of the 'Chapter1' link. How to find the locators for the UI elements is explained in our previous posts. Please go through the below previous posts before going to the next step:
Post#19 What are Locators?
Post#20 Different Types of Locators to identify the UI elements
Post#21 Tools in Selenium to find out the Locators.
Post#22 Locate UI elements by ID
Post#23 Locate UI elements by Name
Post#24 Locate UI elements by Link
Post#25 Locate UI elements by XPath
Post#26 Locate UI elements by CSS
Post#27 Finding Locators for UI elements using the Selenium IDE's recording feature
4. Using FireBug options, Inspect the 'Chapter1' link and identify the locator after observing the below code in the below screenshot:
5. After seeing the above html code in the screenshot, its very clear that we've to identify the 'Chapter1' link using link locator using the syntax link=LinkName i.e. link=Chapter1 in this case.
6. Also we've to use the above link locator in the Selenium WebDriver Command which is used to click the specified link. _driver.findElement(By.linkText("Locator")).click( ); is the syntax we've to use for clicking the specified locator.
6. Also we've to use the above link locator in the Selenium WebDriver Command which is used to click the specified link. _driver.findElement(By.linkText("Locator")).click( ); is the syntax we've to use for clicking the specified locator.
Lets understand the _driver.findElement(By.linkText("Locator")).click( ); syntax by breaking it as below:
- _driver - is the WebDriver object
- findElement( ) - is used for locating the elements on which we have to perform the operations
- By.id("ID LOCATOR") - By.id informs the findElement( ) to find the elements using ID LOCATOR
- By.name("NAME LOCATOR") - By.name informs the findElement( ) to find the elements using NAME LOCATOR
- By.linkText("LINK LOCATOR") - By.linText informs the findElement( ) to find the elements using LINK LOCATOR
- By.xpath("XPath LOCATOR") - By.xpath inform the findElement( ) to find the elements using XPath LOCATOR
- By.cssSelector("CSS LOCATOR") - By.cssSelector inform the findElement( ) to find the elements using CSS LOCATOR
- Locator - is the actual locator we want to find (i.e. Chapter1 in this case )
- click( ) - is the WebDriver command for clicking the element for which the locator is specified.
7. So after understanding the things in step5 and step6, its very clear that, in order to click the Chapter1 link, we've to use the statement _driver.findElement(By.linkText("Chapter1")).click( ); as explained in the next steps.
8. Inside the @Test specified method, Type _driver object of Selenium 'WebDriver' Class and press dot '.' on your keyboard as shown below and ensure that the list of selenium WebDriver commands are displayed for selection:
9. Type 'find' text after the dot to filter the displayed Selenium WebDriver predefined methods and select the 'findElement(By arg0)' as shown below:
10. Ensure that the 'findElement( )' method got added as shown below:
11. Inside findElement( ) parenthesis, Type 'By' and press dot '.' on your keyboard as shown below and ensure that the list of selenium WebDriver commands are displayed for selection:
12. As we have to locate the Chapter1 link element using the link locator, Type 'link' text after the dot to filter the displayed Selenium WebDriver predefined methods and select the 'linkText( )' method option as shown below:
13. Ensure that the 'linkText( )' method got added as shown below:
14. Specify the link locator Chapter1 into the linkText( ) method as shown below:
15. Place the cursor at the end of the statement and press dot '.' on your keyboard as shown below and ensure that the Selenium WebDriver commands are displayed for selection as shown below:
15. Place the cursor at the end of the statement and press dot '.' on your keyboard as shown below and ensure that the Selenium WebDriver commands are displayed for selection as shown below:
16. Type 'click' text after the dot to filter the displayed Selenium WebDriver predefined methods and select the 'click( )' method option as shown below:
17. Ensure that the 'click( )' method got added as shown below:
18. Add semicolon to the end of the statement as shown below and Save:
19. Run the Test using JUnit Test as shown below:
20. Ensure that the 'Chapter1' link is clicked and as a result 'Chapter1' page is displayed as shown below:
Watch the below video:
Click here to watch the video.
Download this Project:
Click here to download this project and import into Eclipse IDE on your machine.
Please comment below to feedback or ask questions.
Using click( ) method for clicking a Button will be explained in the next post.
17. Ensure that the 'click( )' method got added as shown below:
18. Add semicolon to the end of the statement as shown below and Save:
19. Run the Test using JUnit Test as shown below:
20. Ensure that the 'Chapter1' link is clicked and as a result 'Chapter1' page is displayed as shown below:
Watch the below video:
Click here to watch the video.
Download this Project:
Click here to download this project and import into Eclipse IDE on your machine.
Please comment below to feedback or ask questions.