Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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 如何从列表中选择元素,单击它,返回列表并使用SeleniumWebDriver选择下一个元素_Java_Selenium_Junit - Fatal编程技术网

Java 如何从列表中选择元素,单击它,返回列表并使用SeleniumWebDriver选择下一个元素

Java 如何从列表中选择元素,单击它,返回列表并使用SeleniumWebDriver选择下一个元素,java,selenium,junit,Java,Selenium,Junit,我正在尝试编写一个小型应用程序robot,它将使用webdriver登录站点,在搜索字段中搜索一些数据,然后从列表中选择每个元素,方法是单击其中一个,返回列表,然后选择下一个具有相同类名但不同父级的元素。 我想我知道如何获得具有相同类名的所有元素: List<WebElement> incognito_user = driver.findElements(By.xpath("//*[@id='results']/li[2]/div/h3/a")) 但是我需要一些关于for循环的帮助

我正在尝试编写一个小型应用程序robot,它将使用webdriver登录站点,在搜索字段中搜索一些数据,然后从列表中选择每个元素,方法是单击其中一个,返回列表,然后选择下一个具有相同类名但不同父级的元素。 我想我知道如何获得具有相同类名的所有元素:

List<WebElement> incognito_user = driver.findElements(By.xpath("//*[@id='results']/li[2]/div/h3/a"))

但是我需要一些关于for循环的帮助,这些循环将索引所有元素并转到下一页。

如果您需要处理元素列表,请为selenium创建一个包装器

点击


只需使用clickButton,在代码中传递标识符和列表索引

我以一种稍微不同的方式解决了这个问题:

    boolean inLoop = true;
    int maxPages = 2;

    while(inLoop && (maxPages-- > 0)) {
        List<WebElement> incognito_user = driver.findElements(By.xpath("xpath"));
        String openInNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
        for (WebElement element : incognito_user) {
            element.sendKeys(openInNewTab);
            driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);
            waitSecs(randomNumber(5, 10));
            driver.switchTo().window(new ArrayList<String>(driver.getWindowHandles()).get(1));
            driver.close();
            driver.switchTo().window(new ArrayList<String>(driver.getWindowHandles()).get(0));
            waitSecs(randomNumber(5,10));
        }

        List<WebElement> nextElements = driver.findElements(By.xpath("xpath"));
        if (nextElements.size() > 0) {
            nextElements.get(0).click();
            waitSecs(randomNumber(5,10));
        }
        else {
            inLoop = false;
        }
    }
谢谢你的帮助。
亲切问候

您能为您的最佳尝试发布代码吗?谢谢。实际上我想看看你的页面。也许吧。jsp。但是我认为你需要让你的列表有唯一的id,并找到使用这个id的for循环。通过两个xpath表达式强制查找,但命名xpath参数…标识符。对WebElement的无效转换。一些未登记的财产魔法。始终返回相同的字符串,而不是使方法无效。声明无理由抛出IOException。未声明的日志记录成员。
    boolean inLoop = true;
    int maxPages = 2;

    while(inLoop && (maxPages-- > 0)) {
        List<WebElement> incognito_user = driver.findElements(By.xpath("xpath"));
        String openInNewTab = Keys.chord(Keys.CONTROL, Keys.RETURN);
        for (WebElement element : incognito_user) {
            element.sendKeys(openInNewTab);
            driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);
            waitSecs(randomNumber(5, 10));
            driver.switchTo().window(new ArrayList<String>(driver.getWindowHandles()).get(1));
            driver.close();
            driver.switchTo().window(new ArrayList<String>(driver.getWindowHandles()).get(0));
            waitSecs(randomNumber(5,10));
        }

        List<WebElement> nextElements = driver.findElements(By.xpath("xpath"));
        if (nextElements.size() > 0) {
            nextElements.get(0).click();
            waitSecs(randomNumber(5,10));
        }
        else {
            inLoop = false;
        }
    }