【问题标题】:size() option is missing using Selenium webdriver through Java通过 Java 使用 Selenium webdriver 缺少 size() 选项
【发布时间】:2020-12-31 08:09:23
【问题描述】:

一直在学习一些课程来提高我使用 Selenium Webdrive 的自动化技能。在尝试计算页面内链接的数量时,我没有 size() 方法作为选项。

我错过了一些罐子吗?导入库?

java public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "/Users/Follo/Dropbox/Chrome Drivers/chromedriver");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");
        // options.addArguments("--headless");
        WebDriver driver = new ChromeDriver(options);
        driver.get("URL");
        WebElement link = driver.findElement(By.tagName("a"));
        link.size()
        // .size do not appear as an option in the dropdown menu
        System.out.println(link.size());
        driver.quit();
    }
}

【问题讨论】:

    标签: java eclipse selenium selenium-webdriver size


    【解决方案1】:

    使用“findElements”代替“findElement”。它返回元素列表,以便您可以遍历它们。

    区别在于findElement返回第一个匹配的元素,findElements返回所有匹配元素的列表

    【讨论】:

      【解决方案2】:

      尺寸()

      Java中List接口的size()方法用于获取该列表中的元素个数。也就是说,此方法返回此列表容器中存在的元素的计数。

      所以本质上,WebElement 类型的变量 link 将不支持 size() 方法。因此,您必须将变量 link 的类型更改为 List 并使用 findElements() 方法填充 List ,您可以使用以下解决方案:

      List<WebElement> link = driver.findElements(By.tagName("a"));
      System.out.println(link.size());
      

      【讨论】:

        【解决方案3】:
            ArrayList<WebElement> firstLinkurl = (ArrayList<WebElement>) 
        
            driver.findElements(By.xpath("write your xpath here"));
        
            System.out.println(link.size());
        
            firstLinkurl.get(0).click();//also with this you can also click any link on the page just by providing the index number.
        

        【讨论】:

        • 另外,欢迎在您的帖子中添加任何解释。
        猜你喜欢
        • 2017-03-10
        • 1970-01-01
        • 2012-09-25
        • 1970-01-01
        • 2015-08-19
        • 1970-01-01
        • 2012-08-09
        • 2018-04-25
        相关资源
        最近更新 更多