Как обработать и извлечь нужную информацию из списка веб-элементов
Как обработать список веб-элементов и извлечь нужную информацию?
На вебстранице есть некое количество элементов, в которых есть нужная информация. Я нашел элементы на странице, сделал список:
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>