Search On Google

Saturday, February 16, 2019

Selenium Web elements

What is forms in selenium?

Forms are webelement which have multiple elements like textbox, email, password, checkbox, radio button etc.

How to get access to any webelement for do some task on it?

We use locators for get access as below.
Suppose we have a text box with id name_1 then we use following code:-

Webelement element1= driver.findElement(By.id("name_1"))


How to set data in element  ?

element1.sendKeys("sandeep yadav");


How to reset data in field?

element1.clear()


Suppose we have button element then how to click on that element?

For clicking on element by selenium we use following code.
element1.click();

If we have  checkbox then how to chech uncheck?

element1.click(); //for check
element1.click(); //for uncheck

we use same code again because in second click if not checked then will be checked.
We can check element1.isSelected() for check checkbox selected or not.

if we have radio button then how to toogle?

We use click() method on web element object as below.
element1.click();


CHECKBOX and radiobutton Example java code:-


"package pkg1;

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; 
import org.openqa.selenium.*; 

public class MyClass5_webelements_radiobutton_checkbox {   
    public static void main(String[] args) {       
     
        System.setProperty(""webdriver.chrome.driver"",""C:\\Users\\Desk-105\\Documents\\SELENIUM\\chromedriver.exe"");   
        WebDriver driver = new ChromeDriver();   

        driver.get(""https://www.onlinetutorial.info/p/selenium-checkbox-and-radiobutton.html"");   
        WebElement radio1 = driver.findElement(By.id(""radio_1""));     
        WebElement radio2 = driver.findElement(By.id(""radio_2""));     
        WebElement radio3 = driver.findElement(By.id(""radio_3""));           

        radio3.click(); 
        System.out.println(""Radio Button 3rd will be selected"");
       
       
        WebElement opt1 = driver.findElement(By.id(""checkbox_1""));     
        opt1.click(); 
        if (opt1.isSelected()) {   
            System.out.println(""Checkbox is Toggled On i.e selected"");   
        } else { 
            System.out.println(""Checkbox is Toggled Off i.e not selected"");   
        } 
  //driver.close(); 
         
    } 
}

No comments:

Post a Comment

About Me

My photo
Mumbai, Maharashtra, India