提问人:Артём Орёл 提问时间:11/6/2023 更新时间:11/6/2023 访问量:42
如何从 Web 元素列表中处理和提取正确的信息
How to process and extract the right information from a list of web elements
问:
如何处理 Web 元素列表并提取所需信息?
网页上有一定数量的元素具有所需的信息。我在页面上找到了元素,做了一个列表:
package org.example.EmailCollectors.Pages;
import org.example.EmailCollectors.BasePages.BaseSeleniumPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import java.util.ArrayList;
import java.util.List;
public class AllVacanciesList extends BaseSeleniumPage {
private List<String> emails = new ArrayList<>();
@FindBy(xpath = "//*[@id=\"vacContainer\"]/div")
private List<WebElement> listOfVacanciesElements;
@FindBy(xpath = "//*[@id=\"vacContainer\"]/div[1]/div[1]/div/a")
private WebElement rabotaTitle;
public AllVacanciesList() {
PageFactory.initElements(driver, this);
}
public AnnaunsmentElem openAnaunsment() {
for (int i = 1; i < listOfVacanciesElements.size(); i++) {
WebElement rabotaTitle = driver.findElement(By.xpath("//*[@id=\"vacContainer\"]/div[" + i + "]/div[1]/div/a"));
rabotaTitle.click();
}
return new AnnaunsmentElem();
}
}
我尝试使用数组,但出现错误。您必须返回上一页才能处理另一个元素,这就是问题所在。
在上面的代码中,我找到了页面上的所有元素并将它们保存在 .每个元素都有一个指向另一个页面的链接 ()。listOfVacanciesElements
rabotaTitle
使用以下依赖项:pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Selenium1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.typesafe/config -->
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.4.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha0</version>
</dependency>
<!-- For standard jdk1.4 logging :-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>2.0.0-alpha0</version>
<scope>runtime</scope>
</dependency>
<!-- For slf4j-simple logging :-->
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.0-alpha0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
</project>
答: 暂无答案
评论