【问题标题】:Clicking every button that contains a specific class name with ChromeDriver in C#在 C# 中使用 ChromeDriver 单击每个包含特定类名的按钮
【发布时间】:2020-10-26 22:23:51
【问题描述】:

我正在使用 ChromeDriver,我希望它点击页面上包含特定类名的每个按钮。

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
options.AddArgument("ignore-certificate-errors");
ChromeDriver driver = new ChromeDriver(chromeDriverService, options);

driver.Url = "https://www.some-url.com";
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(2);

我在想一些类似的事情:

driver.FindElement(By.ClassName("some-class-name")).Click();

但是,这只点击一个按钮。我怎样才能做到这一点来点击网页上的每个按钮?

【问题讨论】:

    标签: c# selenium selenium-webdriver webdriver selenium-chromedriver


    【解决方案1】:

    尝试使用FindElements 而不是FindElement

    IList<IWebElement> list= driver.FindELements(By.ClassName("some-class-name");
    foreach (IWebElement element in list)
    {
        element.Click();
    }
    

    【讨论】:

      【解决方案2】:
      List<WebElement> elementName = driver.findElementsBy.ClassName("some-class-name"));
      

      这将返回一个 WebElements 列表,您可以在 foreach 语句中对其进行迭代,对每个语句执行 .click()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-24
        相关资源
        最近更新 更多