1.阿里云库:
<repositories> <repository> <id>aliyun</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-plugin</id> <url>https://maven.aliyun.com/repository/public</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories>
2.一般的依赖,tess4j为识别验证码的依赖:
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.8.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> </dependency> <!--用这个依赖或者把chromedriver复制到chrome安装目录的driver目录下,并加到path里面。--> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>5.3.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.30</version> </dependency> <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.12</version> </dependency> <dependency> <groupId>net.sourceforge.tess4j</groupId> <artifactId>tess4j</artifactId> <version>5.5.0</version> </dependency>
3.样例测试脚本:
public class SeleTest { @Before public void setup() { WebDriverManager.chromedriver().setup(); } WebDriver driver; @Test public void eightComponents() { driver = new ChromeDriver(); driver.get("https://www.selenium.dev/selenium/web/web-form.html"); String title = driver.getTitle(); assertEquals("Web form", title); driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500)); WebElement textBox = driver.findElement(By.name("my-text")); WebElement submitButton = driver.findElement(By.cssSelector("button")); textBox.sendKeys("Selenium"); submitButton.click(); WebElement message = driver.findElement(By.id("message")); String value = message.getText(); assertEquals("Received!", value); driver.quit(); } }
4.如果要在springboot项目中使用selenium ,需要使用和springboot本身默认一致的版本号:如:springboot 2.4.2,要用3.141.59,否则会有错误。
5.Docker 部署Selenium环境:
Dockerfile:
FROM selenium/standalone-chrome # 暴露 Selenium 服务器的端口 EXPOSE 4444
docker build -t seleserver .
docker run -d -p 4444:4444 --name seleweb --restart always seleserver
打开端口后即可用:
self.driver = webdriver.Remote(command_executor='http://111.222.33.44:4444/wd/hub', options=options)
就说方不方便吧。
6.css 选择器:#heightToTop > div.info > div.btns.clearfix > button.btn-large-yellow
与 #heightToTop div.info div.btns.clearfix button.btn-large-yellow
区别 : > 严格限定父子关系 ,而 空格 " " 不限定逐级父子,可跨级。