1.下载驱动
1.1查看Google浏览器版本
1.2访问https://chromedriver.storage.googleapis.com/index.html 进入对应目录
1.3 点击 chromedriver_win32.zip 进行下载,下载完成解压
2. 编写java selenium代码
2.1 新建maven项目,在pom文件中加入如下依赖
<!– selenium-java begin–>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<!– selenium-java end–>
2.2编写java代码
package selenium.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
/**
* @author cmf
* @date 2022/12/20
*/
public class SeleniumTest {
public static void main(String[] args) throws InterruptedException, IOException {
//设置1.中下载的驱动路径
System.setProperty("webdriver.chrome.driver", "D:\\code\\selenium-test\\src\\main\\resources\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//窗口最大化
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//输入网址
driver.get("http://www.baidu.com/");
WebElement input = driver.findElement(By.xpath("//*[@id=\"kw\"]"));
input.sendKeys("www.fogsvc.com"); //切换城市,打开默认是本地
driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
Thread.sleep(30000);
driver.close();//关闭浏览器
}
}
2.3测试
运行项目,结果如下