package com.main;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Main {
public static void main(String[] args) {
new Main().fireFox();
/* new Main().chrome(); */
}
public void chrome() {
System.setProperty("webdriver.chrome.driver", "D:\\BS\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.findElement(By.cssSelector("#kw")).sendKeys("海瑞");
driver.findElement(By.xpath(".//*[@id='su']")).click();
System.out.println(driver.getTitle());
System.out.println(driver.manage().window().getSize());
driver.manage().window().fullscreen();
Thread thread = new Thread();
try {
thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.manage().window().setSize(new Dimension(500, 500));
driver.close();
}
public void fireFox() {
// System.setProperty("webdriver.gecko.driver",
// "D:\\BS\\geckodriver.exe");
// 低于48版本的用这个
System.setProperty("webdriver.firefox.marionette", ".\\Tools\\geckodriver.exe");
Thread thread = new Thread();
try {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com");
driver.findElement(By.cssSelector("#kw")).sendKeys("海瑞");
driver.findElement(By.xpath(".//*[@id='su']")).click();
System.out.println(driver.getTitle());
System.out.println(driver.manage().window().getSize());
Thread.sleep(1000);
driver.findElement(By.xpath("/html/body/div/div[2]/div/a[1]")).click();
Thread.sleep(1000);
//后退
driver.navigate().back();
Thread.sleep(1000);
driver.navigate().forward();
// 全屏操作挂了
/* driver.manage().window().fullscreen(); */
driver.manage().window().maximize();
thread.sleep(2000);
// 设置大小
driver.manage().window().setSize(new Dimension(500, 500));
// 获取坐标点
System.out.println(driver.manage().window().getPosition());
/* Thread thread = new Thread(); */
try {
thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 设置定位点
driver.manage().window().setPosition(new Point(30, 30));
Actions actionOpenLinkInNewTab = new Actions(driver);
// 这个是利用键盘快捷键c+t 新建标签页
actionOpenLinkInNewTab.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).perform();
thread.sleep(2000);
// 利用快捷键关闭当前页
actionOpenLinkInNewTab.keyDown(Keys.CONTROL).sendKeys("w").keyUp(Keys.CONTROL).perform();
// 理论上是关闭当前页
// driver.close();
// 关闭所有
driver.close();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}