【问题标题】:Get list of an elements using Selenide使用 Selenide 获取元素列表
【发布时间】:2021-06-14 11:06:40
【问题描述】:

我正在尝试在网站上找到一个元素并获取所有元素。为此,我使用硒化物/硒。我正在寻找的元素不能被一个类定位,因此我试图找到一个具有两个类(父类和子类)的元素。仅使用 Selenide,我可以使用以下方法轻松定位元素:

SelenideElement basketIcon = $(By.className("buttons")).$(byClassName("btn-cart"))

这肯定会给我一个元素。但是我怎样才能找到一个元素数组呢? List<SelenideElement> productName1 = WebDriverRunner.getWebDriver().findElements(By.className("")); 但它不起作用,我也尝试过:List<WebElement> productName = WebDriverRunner.getWebDriver().findElements(By.className("buttons")); 但在这里我如何添加另一个类来定位元素? $(byClassName("btn-cart"))?还是有其他方法可以做到这一点?

我也尝试过使用ElementsCollection elements = WebDriverRunner.getWebDriver().findElements(By.className(""));,但它不起作用...

编辑: 我有一个名为“DriverFactory”的类,用于打开浏览器:

public static void openBrowser(String browserName, String url) throws Exception {
    
        WebDriverManager.chromedriver().browserVersion(chromeversion).setup();
        Configuration.browser = "Chrome";
        Configuration.startMaximized = true;
        driver.get(url);
    }

另一个类,我在其中定义定位器:

public class EE_MonitorCategoryElements extends DriverFactory {


protected static List<WebElement> productName = getWebDriver().findElements(By.id("asddsa"));
}

还有一个测试类:

@Test
public void ads() throws Exception {
    DriverFactory.openBrowser("Chrome", "https://google.ge");
}

当我使用“getWebDriver()”添加最后一个元素定位器并尝试运行测试时,它给了我一个错误:没有测试被绑定,并且我删除/注释 getWebDriver 行,它正在工作

【问题讨论】:

    标签: java selenium intellij-idea css-selectors selenide


    【解决方案1】:

    只需使用$$("&lt;cssSelector&gt;") 即可获取 Selenide 中元素的列表(集合)。

    【讨论】:

      【解决方案2】:

      您必须使用className 吗?为什么不xpathcssSelector

      类似这样的:

      List<WebElement> productName = WebDriverRunner.getWebDriver().findElements(By.xpath("//button"));
      

      并打印size of productName list 以查看您是否拥有所有可用的按钮。

      【讨论】:

      • 嗯,有没有办法像这样使用 ElementsCollection?或者我可以不使用 xpath 吗?
      • 是的,您可以使用 ID、linText、NAME、CSS_SELECTOR 或 XPATH,这取决于您。
      • 伙计,我现在出去几个小时了。请在此处或聊天中分享详细信息。一旦我回来,我一定会看看并回复你。谢谢
      • 获取每个循环中放入 if 块的确切元素,如下所示: for (WebElement element : elements) { if(element.getText().contains("Your desired text")){ System.out.println(element.getText()); //在这里做一些事情}}
      • 完全没有必要使用这么长的结构,有 $$() 供您使用。
      猜你喜欢
      • 2022-01-18
      • 2021-05-19
      • 2019-12-31
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 2022-10-23
      • 2012-04-04
      • 2013-08-21
      相关资源
      最近更新 更多