import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
//import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.JavascriptExecutor;
public class Testine {
private WebDriver driver;
public void setUp(String url){
System.setProperty("webdriver.chrome.driver",url); //设置chrome驱动程序的路径
System.out.println(System.getProperty("webdriver.chrome.driver"));
}
public void location(int type,String data1){
if (type == 1){
driver.findElement(By.id(data1));
}
else if (type == 2){
driver.findElement(By.name(data1));
}
else if (type == 3){
driver.findElement(By.linkText(data1));
}
else if (type == 4){
driver.findElement(By.partialLinkText(data1));
}
else if (type == 5){
driver.findElement(By.cssSelector(data1));
}
else if (type == 6){
driver.findElement(By.xpath(data1));
}
else if (type == 7){
driver.findElement(By.className(data1));
}
else if (type == 8){
WebElement element = driver.findElement(By.tagName(data1));
element.getTagName();
}
else if (type == 9){
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript(data1);
}
else {
System.out.println("this type is not support");
}
}
public void operation(int type,String data1,String data2){
if (type == 1){
driver.quit();
}else if(type == 2){
driver.navigate().to("http://www.baidu.com");
}else if(type == 3){
driver.getTitle();
}else if(type == 4){
driver.findElement(By.id(data1)).sendKeys(data2);
}
}
public static void main(String[] args) throws InterruptedException {
String url = "F:\\chromedriver.exe";
String web_url = "https://www.baidu.com";
Testine test = new Testine();
test.setUp(url);
WebDriver driver = new ChromeDriver(); //初始化一个chrome驱动实例,保存到driver中
driver.manage().window().maximize(); //最大化窗口
driver.manage().window().maximize(); //最大化窗口
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(web_url);
Thread.sleep(5000);
test.operation(4, "wb","selenium" );
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
test.location(1,"su");
}
标签:java,selenium,data1,driver,else,demo,import,type From: https://www.cnblogs.com/lgqboke/p/17076970.html