Sorting out common functions of Java+Selenium (I. browser operation)

Summary: browser actions
			 - Navigation
			 -Windows and tabs
			 - Frames and IFrames
			 -window management
			 -Settings window

I. navigation

1) Page navigation to

driver.get("https://www.baidu.com")

driver.navigate.to("https://www.baidu.com")

Difference: get is a logical jump, while navigate.to is a direct jump to the page without any logic
Reference: https://blog.csdn.net/gaokao2011/article/details/17169969

2) Get current page URL

String url = driver.getCurrentUrl();   //String returned
System.out.println(url);     //https://www.baidu.com

3) Return to the previous page

driver.navigate.back();

4) Go to next page

driver.navigate.forward();

5) Refresh current page

driver.navigate.refresh();

6) Get the title of the current page

String title = driver.getTitle();  //Return string
System.out.println(title);   //Baidu once, you will know

2, Windows and Tabs

When the driver opens a new window, if you want to operate the elements on the new window, you need to switch the driver to the new window

1) Get current window handle

String currentWindowHandle = driver.getWindowHandle();   //Return string

2) Get all window handles

Set<String> allWindowHandles = driver.getWindowHandles();  //Return string collection

3) Switch to a new window

driver.switchTo().window(newHandle);  //newHandle is the handle of the new window to switch

Example:
1. Open Baidu page
2. Enter [selenium] to search
3. Click [Baidu Encyclopedia]
4. Switch to the newly opened page, hover over [classification], and click [art] below
5. Switch to the newly opened page and click the link of "through 800 years"

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Demo5 {
	public static void main(String[] args) throws InterruptedException {
		// TODO Auto-generated method stub
		WebDriver driver = new ChromeDriver();
		//1. Open browser and enter Baidu page
		driver.get("https://www.baidu.com/");
		//2. Enter selenium in the input box
		driver.findElement(By.id("kw")).sendKeys("selenium");
		//3. Click Baidu
		driver.findElement(By.id("su")).click();
		//4. Click Baidu Encyclopedia
		Thread.sleep(5000);
		driver.findElement(By.xpath("//a[contains(text(),'baidu encyclopedia ')]“)).click();
		Thread.sleep(3000);
		//5. Get a handle to the new window and switch to the new window
			// 1) First save the handle of the first window
		String firstpage = driver.getWindowHandle();
			// 2) Get handle to all windows
		Set<String> handles = driver.getWindowHandles();
			// 3) Loop through handles to find the handle for the new window
		String newHandle = "";
		
		for(String handle : handles){
			
			if(handle!=firstpage){
				
				newHandle = handle;
			}
		}
			// 4) Switch to a new window
		driver.switchTo().window(newHandle);
		
		Thread.sleep(2000);
		//6. Mouse over classification
			// 1) Actions class
		Actions action = new Actions(driver);
			// 2) Move the mouse over classification
		action.moveToElement(driver.findElement(By.linkText("classification"))).perform();
		
		Thread.sleep(5000);
		//7. Click science
		driver.findElement(By.linkText("science")).click();
		
		//8. Mouse over classification
		action.moveToElement(driver.findElement(By.linkText("classification"))).perform();	
		
		Thread.sleep(5000);
		//9. Click Art
		driver.findElement(By.linkText("Art")).click();
		
		//10. Switch handle to new open window
		Set<String> allhandles = driver.getWindowHandles();
		
		for(String handle : allhandles){
			
			driver.switchTo().window(handle);
			if(driver.getTitle().contains("Art Encyclopedia_Baidu Encyclopedia")){
				
				break;
			}
			
		}
		//11. Click [through 800 years]
		driver.findElement(By.partialLinkText("Through 800 years")).click();
	}
}

4) Close window or tabs

driver.close();

5) Exit browser at end of session

driver.quit();

3, Frames and IFrames

When there are iframe and frame elements on the page, if you want to operate the html elements under iframe and frame, you need to switch the driver

1) Switch to the specified iframe or frame

WebElement element_iframe = driver.findElement(By.id("iframe1"));
driver.switchTo().frame(element_iframe);

2) Switch back to the parent page

driver.switchTo().defaultContent();

4, window management

1) Get window size

int width = driver.manage().window().getSize().getWidth();
int height = driver.manage().window().getSize().getHeight();
Dimension size = driver.manage().window().getSize();
int width = size.getWidth();
int height = size.getHeight();

2) Set window size

driver.manage().window().setSize(new Dimension(1024,768));

3) Get window location

int x = driver.manage().window().getPosition().getX();
int y = driver.manage().window().getPosition().getY();
Point position = driver.manage().window().getPosition();
int x = position.getX();
int y = position.getY();

5, Settings window

1) Set window position

driver.manage().window().setPositon(new Point(0,0));

2) Maximize window

driver.manage().window().maximize();

3) Full screen window

driver.manage().window().fullscreen();
Published 7 original articles, won praise 1, visited 1467
Private letter follow

Tags: Selenium Windows Java Session

Posted on Tue, 14 Jan 2020 20:40:23 -0800 by gfrank