1.打开网页
driver.get("https://www.baidu.com");
2.浏览器后退
driver.navigate().back();
3.浏览器前进
driver.navigate().forward();
4.刷新
driver.navigate().refresh();
5.浏览器最大化
driver.manage().window().maximize();
6.获取当前页面URL
driver.getCurrentUrl();
7.获取当前页面Title
driver.getTitle();
8.设置浏览器大小
Dimension dimension =new Dimension(800,900);
driver.manage().window().setSize(dimension);
eg:
import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; public class getTest { WebDriver driver; @BeforeMethod public void openbroser() { System.setProperty("webdriver.chrome.driver","D:\Program Files\Java\Webautomation\drivers\chromedriver.exe"); driver = new ChromeDriver(); } /*浏览器打开百度首页并打开新浪5s后后退3s前进之后关闭*/ @Test public void getTest() throws InterruptedException { //打开百度首页 driver.get("https://www.baidu.com"); driver.navigate().to("https://www.sina.com"); //等待5s Thread.sleep(5000); //浏览器后退 driver.navigate().back(); //等待3s Thread.sleep(3000); //浏览器前进 driver.navigate().forward(); //刷新 driver.navigate().refresh(); //浏览器最大化 driver.manage().window().maximize(); Thread.sleep(3000); //设置浏览器大小 Dimension dimension =new Dimension(100,100); driver.manage().window().setSize(dimension); Thread.sleep(3000); //获取当前页面URL String url = driver.getCurrentUrl(); System.out.println("获取到的值是"+url); Assert.assertEquals(url,"https://www.sina.com.cn/"); //获取当前页面Title driver.getTitle(); } @AfterMethod public void closebroser(){ //关闭浏览器-如果这个是写在程序中,程序一旦有问题则不关闭浏览器会有很多问题 driver.quit(); } }