Working With Element - Selenium Automation


Text box
sendKeys used to enter value in text box.






Link

Click command is used to click on any link.




Button

Click command is used to click on any button





WebElement button=driver.findElement(By.xpath("//*[@name='submit']"));

button.click();
 
 


Image, image link or image button








public void clickImage() {
WebElement pictureI= driver.findElement(By.xpath("//img"));
pictureI.click();
}



Checkbox










public class MultipleElement {
WebDriver driver= null;
@BeforeClass
public void browser()
{
System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
driver=new ChromeDriver();
driver.get("D:/Selenium/Website/first.html");
}
@Test
public void checkBox()
{
List<WebElement>check=driver.findElements(By.name("cert"));
check.get(1).click();
}


Output-





Radio button-









WebDriver driver= null;


@BeforeClass
public void browser()
{
System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
driver=new ChromeDriver();
driver.get("D:/Selenium/Website/first.html");
}
@Test
public void radio()
{
List<WebElement>radio=driver.findElements(By.name("nation"));
radio.get(1).click();
}



Output-








Drop-down list







public class MultipleElement {

WebDriver driver= null;
@BeforeClass
public void browser()
{
System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
driver=new ChromeDriver();
driver.get("D:/Selenium/Website/first.html");
}
@Test
public void DropDown()
{
Select drpDwn= new Select(driver.findElement(By.name("passName")));
drpDwn.selectByVisibleText("Priti");
}
}




Output-




Pop up/Alert

Handling during launching browser




ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver_78.exe");
driver=new ChromeDriver(options);

InputBox alert




JFrame f=new JFrame(); 
WebDriver driver;
@BeforeClass
public void launchBroser()
{
System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
driver=new ChromeDriver();
driver.get("D:/Selenium/Website/first.html");
}
@Test
public void enterPromptText()
{
String entercapthe=JOptionPane.showInputDialog(f,"New Pop-UP Text");
WebElement enterText= driver.findElement(By.xpath("//input[@name='t1']"));
enterText.clear();
enterText.sendKeys(entercapthe);
}










Pup up accept and demise








public class Alert {

JFrame f=new JFrame(); 
WebDriver driver;
@BeforeClass
public void launchBroser()
{
System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
driver=new ChromeDriver();
driver.get("D:/Selenium/Website/first.html");
}
@Test
public void ClosePoupu()
{
WebElement pupUp =driver.findElement(By.xpath("//*[text()=' Pup-uP ']"));
pupUp.click();
driver.switchTo().alert().dismiss();
}
@Test
public void AcceptPoupu()
{
WebElement pupUp1 =driver.findElement(By.xpath("//*[text()=' Pup-uP ']"));
pupUp1.click();
driver.switchTo().alert().accept();
}


Multiple Window




public class MultipleWindow {
WebDriver driver;
@BeforeClass
public void launchBroser()
{
System.setProperty("webdriver.chrome.driver", "C:/ChromeDriver/chromedriver_2.41.exe");
driver=new ChromeDriver();
driver.get("D:/Selenium/Website/second.html");
}
@Test
public void openWindow()
{
WebElement windowLink= driver.findElement(By.linkText("window"));
windowLink.click();
Set<String> windowList=driver.getWindowHandles();
String conWindow=windowList.toString();
System.out.println(conWindow);
String [] splitWin=conWindow.split(",");
String secondWin=splitWin[1].replace("]","").trim();
System.out.println(secondWin);
driver.switchTo().window(secondWin);
WebElement enterName=driver.findElement(By.name("t1"));
enterName.clear();
enterName.sendKeys("Yogndrasingh");
}
}



outcome-






Second window








No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...