java selenium demo

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 Testing {

 

public static WebDriver driver = new ChromeDriver(); //初始化一个chrome驱动实例,保存到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)).click();

}

else if (type == 2){

driver.findElement(By.name(data1)).click();

}

else if (type == 3){

driver.findElement(By.linkText(data1)).click();

}

else if (type == 4){

driver.findElement(By.partialLinkText(data1)).click();

}

else if (type == 5){

driver.findElement(By.cssSelector(data1)).click();

}

else if (type == 6){

driver.findElement(By.xpath(data1)).click();

}

else if (type == 7){

driver.findElement(By.className(data1)).click();

}

else if (type == 8){

 

driver.findElement(By.tagName(data1));

 

}

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);

 

driver.manage().window().maximize(); //最大化窗口

driver.manage().window().maximize(); //最大化窗口

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

driver.get(web_url);

Thread.sleep(4000);

test.operation(4, "kw","selenium" );

Thread.sleep(4000);

test.location(1,"su");

 

}

 

}

posted @ 2023-01-30 18:39  先定一个小目标  阅读(192)  评论(0编辑  收藏  举报