Alex
Alex

Reputation: 1934

Click many buttons with same name from a website- Selenium

I am trying to navigate through a page and start adding to cart all products from the list. By doing that i want to click whatever it has the button "Agree".

Here is my code

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("url.com");
for (int i = 0; i < 10; i++)
  {
     driver.FindElement(By.LinkText("Agree")).Click();                 
  }

Here is a screenshot with my buttons and their details in "Inspect" enter image description here

Upvotes: 0

Views: 94

Answers (1)

Barry O&#39;Kane
Barry O&#39;Kane

Reputation: 1187

As long as you're happy that you just click each of the found elements up to 10, then this will work.

var buttons = driver.FindElements(By.LinkText("Buy")).ToArray();

for (int i = 0; i < 10; i++)
{
    buttons[i].Click();                 
}

Alternatively if you want to click each button once use a foreach

foreach(button in buttons)
{
   button.Click();
}

Upvotes: 1

Related Questions