有没有人能帮我解决这个代码。当我使用streams时,我得到了NoSuchElement异常。foreach()替代方案(评论)运行良好。我想知道为什么它会抛出异常,如果使用流实现的话。下面是供您参考的代码:
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();
}
}发布于 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公斤
https://stackoverflow.com/questions/65732142
复制相似问题