首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium Streams - NoSuchElementException

Selenium Streams - NoSuchElementException
EN

Stack Overflow用户
提问于 2021-01-15 15:35:39
回答 1查看 26关注 0票数 0

有没有人能帮我解决这个代码。当我使用streams时,我得到了NoSuchElement异常。foreach()替代方案(评论)运行良好。我想知道为什么它会抛出异常,如果使用流实现的话。下面是供您参考的代码:

代码语言:javascript
复制
public class Test {
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "E:\\Sajidh\\WebDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");
WebElement product = driver.findElements(By.cssSelector("div.product")).stream().filter(p->p.findElement(By.cssSelector("h4.product-name")).getText().contains("Potato")).collect(Collectors.toList()).get(0);
// WebElement product = null;
// for(WebElement p : driver.findElements(By.cssSelector("div.product")))
// {
// if(p.findElement(By.cssSelector("h4.product-name")).getText().contains("Potato"))
// {
// product = p;
// break;
// }
// }
System.out.println(product.findElement(By.cssSelector("h4.product-name")).getText());
System.out.println(product.findElement(By.cssSelector("input.quantity")).getText());
System.out.println(product.findElements(By.cssSelector("div.product-action button:nth-child(1)")).size());
product.findElement(By.cssSelector("div.product-action button:nth-child(1)")).click();
}
}
EN

回答 1

Stack Overflow用户

发布于 2021-01-16 05:17:47

要打印所有元素的textContent,您需要导出visibilityOfAllElementsLocatedBy()WebDriverWait,您可以使用Java8、stream()map(),也可以使用以下Locator Strategies之一

  • cssSelector

import org.openqa.selenium.support.ui.ExpectedConditions;;import java.util.stream.Collectors;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;System.setProperty("webdriver.chrome.driver","C:\WebDrivers\chromedriver.exe");ChromeOptions options = new ChromeOptions();options.addArguments("--start-maximized");WebDriver driver = new ChromeDriver(options);driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");System.out.println(新驱动程序(驱动程序,WebDriverWaitSystem.out.println(新的WebDriverWait(驱动程序、20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div@class='product'//h4@class='product-name'"))).stream().map(element->element.getText()).collect(Collectors.toList()));driver.quit();

  • 控制台输出:

西葫芦-1公斤,花椰菜-1公斤,黄瓜-1公斤,甜菜-1公斤,胡萝卜-1公斤,番茄-1公斤,豆子-1公斤,灯笼-1公斤,辣椒,香菇-1公斤,土豆-1公斤,南瓜-1公斤,玉米-1公斤,洋葱-1公斤,苹果-1公斤,香蕉-1公斤,葡萄-1公斤,芒果-1公斤,麝香瓜-1公斤,橙子-1公斤,梨-1公斤,石榴子-1公斤,覆盆子- 1/4公斤,草莓- 1/4公斤,水-1公斤杏仁- 1/4公斤,手枪- 1/4公斤,坚果混合物-1公斤,腰果-1公斤,核桃- 1/4公斤

  • xpath

import org.openqa.selenium.support.ui.ExpectedConditions;;import java.util.stream.Collectors;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;System.setProperty("webdriver.chrome.driver","C:\WebDrivers\chromedriver.exe");ChromeOptions options = new ChromeOptions();options.addArguments("--start-maximized");WebDriver driver = new ChromeDriver(options);driver.get("https://rahulshettyacademy.com/seleniumPractise/#/");System.out.println(新驱动程序(驱动程序,WebDriverWaitSystem.out.println(新的WebDriverWait(驱动程序、20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//div@class='product'//h4@class='product-name'"))).stream().map(element->element.getText()).collect(Collectors.toList()));driver.quit();

  • 控制台输出:

西葫芦-1公斤,花椰菜-1公斤,黄瓜-1公斤,甜菜-1公斤,胡萝卜-1公斤,番茄-1公斤,豆子-1公斤,灯笼-1公斤,辣椒,香菇-1公斤,土豆-1公斤,南瓜-1公斤,玉米-1公斤,洋葱-1公斤,苹果-1公斤,香蕉-1公斤,葡萄-1公斤,芒果-1公斤,麝香瓜-1公斤,橙子-1公斤,梨-1公斤,石榴子-1公斤,覆盆子- 1/4公斤,草莓- 1/4公斤,水-1公斤杏仁- 1/4公斤,手枪- 1/4公斤,坚果混合物-1公斤,腰果-1公斤,核桃- 1/4公斤

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65732142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档