Skip to content

Instantly share code, notes, and snippets.

@monishamanivasagam
Last active January 26, 2024 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monishamanivasagam/ced42c11db7eae2fc7f4cc38f7d28139 to your computer and use it in GitHub Desktop.
Save monishamanivasagam/ced42c11db7eae2fc7f4cc38f7d28139 to your computer and use it in GitHub Desktop.
Selenium Notes

Print number of rows and columns

		WebDriver driver = null;
	    CallChromeWebDriver cd = new CallChromeWebDriver(driver,"Chrome");
	    driver = cd.getDriver();
	    driver.get("https://rahulshettyacademy.com/AutomationPractice/");
	    // reach the table 
	    driver.findElement(By.xpath("//fieldset/table[@id='product']"));
	    //print number of rows
	  List<WebElement> rows =  driver.findElements(By.xpath("//fieldset/table[@id='product']/tbody/tr"));
	   System.out.println("Number of rows: " +rows.size());
	   // print number of columns
	   System.out.println("Number of columns: "+ driver.findElements(By.xpath("//fieldset/table[@id='product']/tbody/tr/th")).size());
	  System.out.println("Contents of third column: " + driver.findElement(By.xpath("//fieldset/table[@id='product']/tbody/tr[3]")).getText()); 
	   cd.closeDriver();1 1

Realtive Locators

import org.openqa.selenium.By;



import org.openqa.selenium.WebDriver;



import org.openqa.selenium.WebElement;



import org.openqa.selenium.chrome.ChromeDriver;

import static org.openqa.selenium.support.locators.RelativeLocator.*;

public class RelativeLoc {
public static void main(String[] args) {

System.setProperty("webdriver.chrome.driver", "/Users/rahulshetty/Documents/chromedriver");
WebDriver driver=new ChromeDriver();

driver.get("https://rahulshettyacademy.com/angularpractice/");

WebElement nameEditBox =driver.findElement(By.cssSelector("[name='name']"));

System.out.println(driver.findElement(with(By.tagName("label")).above(nameEditBox)).getText());

WebElement dateofBirth = driver.findElement(By.cssSelector("[for='dateofBirth']"));

driver.findElement(with(By.tagName("input")).below(dateofBirth)).click();

WebElement iceCreamLabel =driver.findElement(By.xpath("//label[text()='Check me out if you Love IceCreams!']"));

driver.findElement(with(By.tagName("input")).toLeftOf(iceCreamLabel)).click();

WebElement rdb = driver.findElement(By.id("inlineRadio1"));
System.out.println(driver.findElement(with(By.tagName("label")).toRightOf(rdb)).getText());

}
}

Invoking multiple tabs/windows parallely - partial screenshot - saving the file to local - getting dimemsions of a webelement

import java.io.File;

import java.io.IOException;

import java.util.Iterator;

import java.util.Set;



import org.apache.commons.io.FileUtils;

import org.openqa.selenium.By;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.WindowType;

import org.openqa.selenium.chrome.ChromeDriver;

public class NewWindow {

public static void main(String[] args) throws IOException {

System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");

WebDriver driver=new ChromeDriver();

driver.get("https://rahulshettyacademy.com/angularpractice/");

//Switching Window

driver.switchTo().newWindow(WindowType.WINDOW);

Set<String> handles=driver.getWindowHandles();

Iterator<String> it=handles.iterator();

String parentWindowId = it.next();

String childWindow =it.next();

driver.switchTo().window(childWindow);

driver.get("https://rahulshettyacademy.com/");

String courseName = driver.findElements(By.cssSelector("a[href*='https://courses.rahulshettyacademy.com/p']"))

.get(1).getText();

driver.switchTo().window(parentWindowId);

WebElement name=driver.findElement(By.cssSelector("[name='name']"));

name.sendKeys(courseName);

//Screenshot

File file=name.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(file, new File("logo.png"));

//driver.quit();

//GEt Height & Width

System.out.println(name.getRect().getDimension().getHeight());

System.out.println(name.getRect().getDimension().getWidth());
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment