Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium非确定性循环列表<;WebElement>;_Java_Selenium_Google Chrome - Fatal编程技术网

Java Selenium非确定性循环列表<;WebElement>;

Java Selenium非确定性循环列表<;WebElement>;,java,selenium,google-chrome,Java,Selenium,Google Chrome,我正在尝试做一些快速测试来学习selenium,以用于抓取网页。我正试着浏览塔可钟网站的菜单项。我感到困惑的是,列表的第一个元素不是第一次或第二次单击所选择的元素。实际选择通常是第二或第三个元素。它是不确定的。我做错了什么 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chr

我正在尝试做一些快速测试来学习selenium,以用于抓取网页。我正试着浏览塔可钟网站的菜单项。我感到困惑的是,列表的第一个元素不是第一次或第二次单击所选择的元素。实际选择通常是第二或第三个元素。它是不确定的。我做错了什么

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class Main {

    static WebDriver driver;

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "/Applications/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.tacobell.com/food");
        List<WebElement> listOfMenuCategories = driver.findElements(By.cssSelector(".cls-category-card-item-card"));
        for(WebElement webElement : listOfMenuCategories){
            scanTacoBellMenuCategory(webElement);
        }
        System.out.println("1: "+listOfMenuCategories.size());

        driver.quit();
    }

    public static void scanTacoBellMenuCategory(WebElement webElement){
        webElement.click();
        List<WebElement> listOfSubMenuCategories = driver.findElements(By.cssSelector(".product-item"));
        for(WebElement submenuCategory : listOfSubMenuCategories){
            scanTacoBellSubMenuCategory(submenuCategory);
        }
    }

    public static void scanTacoBellSubMenuCategory(WebElement webElement){
        webElement.click();
    }
}
import org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入java.util.List;
公共班机{
静态网络驱动程序;
公共静态void main(字符串[]args)引发InterruptedException{
System.setProperty(“webdriver.chrome.driver”,“/Applications/chromedriver”);
驱动程序=新的ChromeDriver();
驱动程序。获取(“https://www.tacobell.com/food");
List listOfMenuCategories=driver.findElements(By.cssSelector(“.cls类别卡项目卡”);
for(WebElement WebElement:listOfMenuCategories){
scanTacoBellMenuCategory(webElement);
}
System.out.println(“1:+listOfMenuCategories.size());
driver.quit();
}
公共静态无效扫描目录(WebElement WebElement){
webElement.click();
ListListofSubMenuCategories=driver.findElements(By.cssSelector(“.product item”);
对于(WebElement子菜单类别:子菜单类别列表){
Scantacobell子菜单类别(子菜单类别);
}
}
公共静态无效扫描目录子菜单类别(WebElement WebElement){
webElement.click();
}
}
谢谢

更新-------------------------------

我现在意识到我的例子过于复杂,意图也不明显。下面是一个更直接的例子:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;

public class MainTwo {

    static WebDriver driver;

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "/Applications/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.tacobell.com/food");
        List<WebElement> listOfMenuCategories = driver.findElements(By.cssSelector(".cls-category-card-item-card"));
        for(WebElement webElement : listOfMenuCategories){
            webElement.click();
            break;
        }
        driver.quit();
    }
}
import org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.chrome.ChromeDriver;
导入java.util.List;
公共二级{
静态网络驱动程序;
公共静态void main(字符串[]args)引发InterruptedException{
System.setProperty(“webdriver.chrome.driver”,“/Applications/chromedriver”);
驱动程序=新的ChromeDriver();
驱动程序。获取(“https://www.tacobell.com/food");
List listOfMenuCategories=driver.findElements(By.cssSelector(“.cls类别卡项目卡”);
for(WebElement WebElement:listOfMenuCategories){
webElement.click();
打破
}
driver.quit();
}
}
tacobell菜单()按以下顺序分为16类:新菜单、最爱菜单、套餐、特色菜、玉米卷、墨西哥煎饼、墨西哥玉米片、价值菜单、糖果、配菜、饮料、电源菜单、派对、套餐、素食、早餐

当我循环这些项目时,我“单击”forEach循环中的第一个项目。我希望这是“新”的类别。我还希望结果可以重复。然而,这两种说法都不正确。它通常打开“收藏夹”、“组合”或“特色”菜单之一。然而,它可以真正打开几乎任何东西

似乎Webdriver在某种程度上是非阻塞的。特别是,webElement.click()事件似乎并没有停止forEach循环的执行。这几乎就像webElement在另一个线程中一样


当我运行上述代码时,为什么没有显示“New”(新建)菜单?为什么这不是确定性的?

您要寻找的预期行为是什么?foreach循环应该从第一个WebElement开始。相反,它是从第二个或第三个WebElement开始的。不幸的是,在工作中,我无法打开taco bell网站,在浏览器的f12 wilnow中,当你检查列表时,“新”菜单是否显示为第一个元素?例如,