合并两个 WebElements 列表

无法打印合并列表中的值,因为它向我抛出空值!


List<WebElement> DateTime2 = driver.findElements(By.xpath(""));

driver.findElement(By.xpath("")).click();

List<WebElement> DateTime3 = driver.findElements(By.xpath(""));

List<WebElement> DateTime = new ArrayList<>(DateTime2);

DateTime.addAll(DateTime3);

Thread.sleep(2000);

System.out.println("This is for testing the list " + 

DateTime.get(2).getText());

System.out.println("This is for testing the list " + 

DateTime.get(30).getText());

我希望输出打印日期和时间


长风秋雁
浏览 139回答 1
1回答

萧十郎

这是我使用的一些代码的示例(尽管我为此示例稍微更改了它。这通常是扩展另一个类的一部分。)int sanitycount= 0;int ec_Timeout = 10; //seconds to wait for list...&nbsp;&nbsp; &nbsp; public void RunAction(WebDriver driver, String in_xpath)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; wait = new WebDriverWait(driver, ec_Timeout);&nbsp; &nbsp; &nbsp; &nbsp; List<WebElement> found_elements = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; found_elements = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(in_xpath)));&nbsp; &nbsp; &nbsp; &nbsp; if (!found_elements.isEmpty())&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // store this array, return an array from function&nbsp; &nbsp; &nbsp; &nbsp; // or loop through array and add items to more global array..&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp;if (e.getClass().getCanonicalName().equals("org.openqa.selenium.StaleElementReferenceException"))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//need to do it again, not finished loading&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("*****************Stale caught-redoing");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sanitycount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (sanitycount<ec_Timeout * 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RunAction(driver, in_xpath);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; System.out.println (e.toString());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; System.out.println (e.toString());&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }RunAction(your_driver, your_xpath);&nbsp;//set sanitycount back to zero if you run again...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java