在Java Selenium中单击NEXT时无法获取数据



这是我的代码:

package working;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
private static WebDriver driver;
public static void main(String[] args) throws Exception {
openBrowser();
System.out.println(scrape());
closeBrowser();
}
private static String table = "(//table[contains(@class,'datatable')])[1]";
private static String tr = table + "/tbody/tr";
private final static String next = table + "/preceding::a[text()='Next'][1]";
private final static String yesGotIt = "//body/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-banner ")]/div/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-button-wrapper ")]/button";
private static String scrape() throws Exception {
WebElement table_element = driver.findElement(By.xpath(table));
List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));
int count = 1;
String rows = "";
driver.findElement(By.xpath(yesGotIt)).click();
while(count <= 3) {
for (WebElement trElement : tr_collection) {
List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
for (WebElement tdElement : td_collection) {
rows += tdElement.getText() + "t";
}
rows = rows + "n";
}
System.out.println(rows);
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));
driver.findElement(By.xpath(next)).click();
count += driver.findElements(By.xpath(next)).size();
System.out.println(count);
}
return rows;
}
private static void openBrowser() {
System.setProperty("webdriver.chrome.driver",
"E:\eclipse-workspace\src\working\selenium\driver\chromedriver.exe");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.get("https://www.iata.org/en/publications/directories/code-search/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
private static void closeBrowser() {
driver.quit();
}
}

没有while循环和next,代码运行良好,但当我运行上述代码时,它重复打印第一页表数据3次,并抛出以下错误:

2021年3月20日上午11:21:47 org.openqa.selenium.remote.Protocol握手createSession INFO:检测到的方言:OSS";Fly Armenia Airways";亚美尼亚有限责任公司VF 914〃;NOVAIR";有限责任亚美尼亚NG 206 21航空有限责任公司美国2I*68140-Mile Air,有限公司美国Q5*748 Air服务肯尼亚H4*874 874

2〃;Fly Armenia Airways";亚美尼亚有限责任公司VF 914
"NOVAIR";亚美尼亚NG 206 21航空有限责任公司美利坚合众国2I*681 40-Mile Air,有限公司美利坚合众国AMERICA Q5*748 Air Services Kenya H4*874 874";飞亚美尼亚Airways";亚美尼亚有限责任公司VF 914〃;NOVAIR";有限的责任公司亚美尼亚NG 206 21航空有限责任公司AMERICA 2I*681 40-Mile Air,有限公司美国Q5*
748 Air Services Kenya H4*874 874

3线程中的异常";主";org.openqa.selenium.StaleElementReferenceException:陈旧元素引用:元素未附加到页面文档(会话信息:chrome=89.0.4389.90)(驾驶员信息:chromedriver=2.42.591088(7b2b2dca23cca0862f674758c9a393e685c27d5),平台=Windows NT10.0.18363 x86_64)(警告:服务器未提供任何堆栈竞争信息)命令持续时间或超时:0毫秒对于有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/stale_element_reference.html内部版本信息:版本:"3.141.59",修订版:"e82be7d358",时间:'2018-11-14T08:25:53'系统信息:主机:'DESKTOP-832OS5B',ip:'10.122.250.5',操作系统名称:'Windows10',操作系统架构:'amd64',操作程序版本:"10.0",java.version:"15.0.2"驱动程序信息:org.openqa.selenium.chrome.ChromeDriver功能{acceptInsecureCerts:false,acceptSslCerts:false,applicationCacheEnabled:false,browserConnectionEnabled:false,浏览器名称:chrome,chrome:{chromedriverVersion:2.42.591088(7b2b2dca23cca0…,userDataDir:C:\Users\Windows\AppData\Lo…},cssSelectorsEnabled:true,databaseEnabled:false,goog:chromeOptions:{debuggerAddress:localhost:59261},handlesAlerts:true,hasTouchScreen:false,javascriptEnabled:true,locationContextEnabled:true,mobileEmulationEnabled:false,nativeEvents:true,networkConnectionEnabled:false,pageLoadStrategy:正常,平台:XP,平台名称:XP,可旋转:false,setWindowRect:true,takesHeapSnapshot:true,takes屏幕截图:true,意外的AlertBehavior:,unhandledPromptBehavior:,版本:89.0.4389.90,webStorageEnabled:true}会话ID:5064d7cddc475396a421aa6d1639fb5c,位于java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native方法)java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessor Impl.java:64)在java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessor Impl.java:45)在java.base/java.lang.reflect.CConstructor.newInstanceWithCaller(Constructor.java:500)在java.base/java.lang.reflect.CConstructor.newInstance(Constructor.java:481)在org.openqa.selene.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)在org.openqa.selene.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)在org.openqa.selene.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponse Codec.java:40)在org.openqa.selene.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpRescenseCodec.java:80)在org.openqa.selene.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpRescenseCodec.java:44)在org.openqa.selene.remote.HttpCommandExecution.execute(HttpCommandExecutior.java:158)在org.openqa.selene.remote.service.DriverCommandExecution.execute(DriverCommandExecutior.java:83)在org.openqa.selene.remote.RemoteWebDriver.exexecute(RemoteWebDriver.java:552)在org.openqa.selene.remote.RemoteWebElement.exexecute(RemoteWebElement.java:285)在org.openqa.selene.remote.RemoteWebElement.getText(RemoteWebElement.java:166)在工作中。测试.刮擦(测试.java:41)工作Test.main(Test.java:21)

如何通过单击NEXT按钮打印每个页面表数据?

向致以最良好的问候

  • 每次单击"下一步"按钮后刷新页面
  • 在while内部声明table_element、tr_collection
  • 用void return声明scrape(),只调用scrape)而不打印
  • 还对代码进行了一些其他更改
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test 
{
private static WebDriver driver;
public static void main(String[] args) throws Exception 
{
openBrowser();
//System.out.println(scrape());
scrape();
closeBrowser();
}
private static String table = "(//table[contains(@class,'datatable')])[1]";
private static String tr = table + "/tbody/tr";
private final static String next = table + "/preceding::a[text()='Next'][1]";
private final static String yesGotIt = "//body/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-banner ")]/div/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-button-wrapper ")]/button";
private static void scrape() throws Exception 
{
// WebElement table_element = driver.findElement(By.xpath(table));
// List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));
int count = 1;
// String rows = "";
driver.findElement(By.xpath(yesGotIt)).click();
while (count <= 3) {
//Declare rows here - previous page's data will not repeatedly print
String rows = "";
System.out.println(count);
// Declare WebElement, List here
WebElement table_element = driver.findElement(By.xpath(table));
List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));
for (WebElement trElement : tr_collection) 
{
List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
for (WebElement tdElement : td_collection) 
{
rows += tdElement.getText() + "t";
}
rows = rows + "n";
}
System.out.println(rows);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));
driver.findElement(By.xpath(next)).click();
count += driver.findElements(By.xpath(next)).size();
//System.out.println(count);
//refresh page to ignore StaleElementReferenceException
driver.navigate().refresh();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));
}
}
private static void openBrowser() 
{
System.setProperty("webdriver.chrome.driver",
"E:\eclipse-workspace\src\working\selenium\driver\chromedriver.exe");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.get("https://www.iata.org/en/publications/directories/code-search/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
private static void closeBrowser() 
{
driver.quit();
}
}

希望在更改表变量后一切都能正常工作-看看控制台打印。由于可见性集中在第一个表上,所以无法观察到最后一个表的页面变化。为了关注实际表的下一个按钮,使用了JavascriptExecutor并更新了整个代码。

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test 
{
private static WebDriver driver;
public static void main(String[] args) throws Exception 
{
openBrowser();
//System.out.println(scrape());
scrape();
//closeBrowser();
}
//private static String table = "(//table[contains(@class,'datatable')])[1]";
private static String table = "(//table[contains(@class,'datatable')])[last()]";
private static String tr = table + "/tbody/tr";
private final static String next = table + "/preceding::a[text()='Next'][1]";
private final static String yesGotIt = "//body/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-banner ")]/div/div[contains(concat(" ",normalize-space(@class)," ")," gdpr-button-wrapper ")]/button";
private static void scrape() throws Exception 
{
// WebElement table_element = driver.findElement(By.xpath(table));
// List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));
int count = 1;
// String rows = "";
driver.findElement(By.xpath(yesGotIt)).click();
while (count <= 3) {
//Declare rows here - previous page's data will not repeatedly print
String rows = "";
System.out.println(count);
// Declare WebElement, List here
WebElement table_element = driver.findElement(By.xpath(table));
List<WebElement> tr_collection = table_element.findElements(By.xpath(tr));
for (WebElement trElement : tr_collection) 
{
List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
for (WebElement tdElement : td_collection) 
{
rows += tdElement.getText() + "t";
}
rows = rows + "n";
}
System.out.println(rows);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));

//scroll to next button
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement we = driver.findElement(By.xpath(next));
je.executeScript("arguments[0].scrollIntoView(true);", we);
we.click();

driver.findElement(By.xpath(next)).click();
count += driver.findElements(By.xpath(next)).size();
//System.out.println(count);
//refresh page to ignore StaleElementReferenceException
driver.navigate().refresh();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(next)));
}
}
private static void openBrowser() 
{
System.setProperty("webdriver.chrome.driver",
"E:\eclipse-workspace\src\working\selenium\driver\chromedriver.exe");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver();
driver.get("https://www.iata.org/en/publications/directories/code-search/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
private static void closeBrowser() 
{
driver.quit();
}
}

相关内容

  • 没有找到相关文章

最新更新