TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Selenium Webdriver Locating Strategies By Link Text

Selenium Webdriver Locating Strategies By Link Text with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to SELENIUM

Locating Strategies- (By Link Text)

In this section, you will learn how to locate a particular web element through its Link Text.

Let us consider a test case in which we will automate the following scenarios:

  • Invoke Firefox browser
  • Open URL: https://www.testandquiz.com/selenium/testing.html
  • Click on the link text "This is a Link" on the sample web page
  • We will create our test case step by step in order to give you a complete understanding of how to use Locators to identify and locate a particular web element.

    Step1. Launch Eclipse IDE and open the existing test suite "Demo_Test" which we have created in earlier sessions of this tutorial.

    Step2. Right click on the "src" folder and create a new Class File from New > Class.

    Selenium Webdriver Locating Strategies By Link Text

    Give your Class name as "Link_Test" and click on "Finish" button.

    Selenium Webdriver Locating Strategies By Link Text

    Step3. Let's get to the coding ground.

    • To invoke Firefox browser, we need to download Gecko driver and set the system property for Gecko driver. We have already discussed this in earlier sessions of this tutorial. You can refer "Running Test on Firefox Browser" to learn how to download and set System property for Firefox driver.

    Here is the sample code to set system property for Gecko driver:

     // System Property for Gecko Driver 
    	System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
    
    • After that we have to initialize Gecko Driver using Desired Capabilities Class.

    Here is the sample code to initialize gecko driver using DesiredCapabilities class.

    // Initialize Gecko Driver using Desired Capabilities Class
    		DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    		capabilities.setCapability("marionette",true);
    	WebDriver driver= new FirefoxDriver(capabilities);
    

    Combining both of the above code blocks, we will get the code snippet to launch Firefox browser.

      // System Property for Gecko Driver 
    	System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
    		
    	 // Initialize Gecko Driver using Desired Capabilities Class
    	DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    	capabilities.setCapability("marionette",true);
    	WebDriver driver= new FirefoxDriver(capabilities);
    
    • After that we need to write the code which will automate our second test scenario (navigate to the desired URL)

    Here is the sample code to navigate to the desired URL:

    // Launch Website
    driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
    

    The complete code till now will look something like this:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class Link_Test {
    
    	public static void main(String[] args) {
    		
    	// System Property for Gecko Driver 
    		System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
    			
    	// Initialize Gecko Driver using Desired Capabilities Class
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    			capabilities.setCapability("marionette",true);
    			WebDriver driver= new FirefoxDriver(capabilities);
    		
    
          // Launch Website
    driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
    	
    	}
    
    }
    

    Step4. Now, we will try to locate the desired web element through its Link text. In Selenium, locating a particular web element involves inspection of its HTML codes.

    Follow the steps given below to locate the Text box on the sample web page.

    Selenium Webdriver Locating Strategies By Link Text
    • It will launch a window containing all the specific codes involved in the development of the Link Text.
    Selenium Webdriver Locating Strategies By Link Text
    • Pick the value of the Link Text i.e. "This is a link".
    Selenium Webdriver Locating Strategies By Link Text

    The Java Syntax for locating a web element through its Link Text is written as:

    driver.findElement(By.linkText (<linktext>)
    

    Therefore, for locating the Link Text on the sample web page we will use the value of its Link Text:

    driver.findElement(By.linkText (<"This is a Link">))
    

    Step5. To automate our third test scenario, we need to write the code which will click on the Link Text.

    Here is the sample code to click on the Link Text.

    // Click on the Link Text using click() command
     driver.findElement(By.linkText("This is a Link")).click();
    

    Thus, our final test script will look something like this:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.remote.DesiredCapabilities;
    
    public class Link_Test {
    
    	public static void main(String[] args) {
    		
    		  // System Property for Gecko Driver 
    		System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" );
    			
    		 // Initialize Gecko Driver using Desired Capabilities Class
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    			capabilities.setCapability("marionette",true);
    			WebDriver driver= new FirefoxDriver(capabilities);
    			
    	
    			  
    // Launch Website
    driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); 
    
    // Click on the Link Text using click() command
     	driver.findElement(By.linkText("This is a Link")).click();
    	 
    	}
    }
    

    The following screenshot shows the Eclipse window for our test script.

    Selenium Webdriver Locating Strategies By Link Text

    Step6. Right click on the Eclipse code and select Run As > Java Application.

    Selenium Webdriver Locating Strategies By Link Text

    Upon execution, the above test script will launch the Firefox browser and automate all the test scenarios.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf