C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Locating Strategies- (By ID)In this section, you will learn how to locate a particular web element using the value of its id attribute. Let us consider a test case in which we will automate the following scenarios:
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. Give your Class name as "Locator_One" and click on "Finish" button. Step3. Let's get to the coding ground.
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" );
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);
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 Locator_One { 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 by using the value of its id attribute. 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.
The Java Syntax for locating a web element using its id attribute is written as: driver.findElement(By.id (<element ID>)) Therefore, for locating the text box on the sample web page we will use the value of its id attribute as: driver.findElement(By.id (<"fname">)) Similarly, for locating the Submit button on the sample web page we will use the value of its id attribute as: driver.findElement(By.id (<"idOfButton">)) Step5. To automate our third, fourth and fifth test scenario, we need to write the code which will click on the Text box, type the desired values on the text box and click on the submit button. Here is the sample code to click on the Text box and type values as "JavaTpoint". // Click on the textbox and send value driver.findElement(By.id("fname")).sendKeys("JavaTpoint"); And the following code snippet will click on the Submit button: // Click on the Submit button using click() command driver.findElement(By.id("idOfButton")).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 Locator_One { 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); // Click on the textbox and send value driver.findElement(By.id("fname")).sendKeys("JavaTpoint"); // Click on the Submit button using click() command driver.findElement(By.id("idOfButton")).click(); } } The following screenshot shows the Eclipse window for our test script. Step6. Right click on the Eclipse code and select Run As > Java Application. Upon execution, the above test script will launch the Firefox browser and automate all the test scenarios.
Next TopicWebDriver Locating Strategies
|