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");
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通