1

There are two buttons with same class, text etc. I have to find and click each.

enter image description here

<a class="jsx-1291462554"><button style="padding: 12px 15px 10px; border-radius: 4px; font: 500 15px / 16px hind; color: white; background-color: rgb(18, 83, 181); justify-content: center; text-align: center; cursor: pointer; text-decoration: none; outline: none; border: none; width: 162px;"><span class="jsx-1291462554 magzter__buttonText">Choose</span></button></a>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
deepika
  • 11
  • 1
  • 3

6 Answers6

0

//section[contains(@class,'chooseplanTitle')]//button[1] //section[contains(@class,'chooseplanTitle')]//button[2]

eamf
  • 41
  • 5
0

If is possible, put something to differentiate the buttons's mapping. But if it isn't possible, these mappings will solve

(//section[contains(@class, 'twocol')]/a)[1]

(//section[contains(@class, 'twocol')]/a)[2]
Vladimir Stanciu
  • 1,468
  • 1
  • 7
  • 24
0

to enable the automatic clicking of a button, we use the XPath function, which is in the class By.


1.Version

driver.FindElement(By.XPath("//button[@class ='CLASS NAME']")).Click();
                                                          

2.Version

driver.FindElement(By.XPath("//div[normalize-space(.)='NAME']/button")).Click();

To enable an automatic input into the text field we can also use the function CssSelector which is located in the class By.


        IWebElement usernameInput = driver.FindElement(By.CssSelector("input[name='username']"));
        IWebElement passwordInput = driver.FindElement(By.CssSelector("input[name='password']"));

        usernameInput.SendKeys("YourUsername");
        passwordInput.SendKeys("YourPassword");
Mikail B.
  • 225
  • 5
  • 11
0

find first and second element that matches the below locator :

(//a[@class="jsx-1291462554"]/button)[1]
(//a[@class="jsx-1291462554"]/button)[2]

find all elements with tag button and is a first child of 'a' with class specified:

//a[@class="jsx-1291462554"]/button[1]

css equalent of first xpath :

a.jsx-1291462554>button:nth-of-type(1)
a.jsx-1291462554>button:nth-of-type(2)

css equalent of second xpath :

a.jsx-1291462554>button:nth-child(1)
PDHide
  • 18,113
  • 2
  • 31
  • 46
0

Both the elements are a dynamic elements. So to click() on the first Choose you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • xpath:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//p[starts-with(., 'Choose the plan that')]//following::a[1]/button/span[text()='Choose']")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0
List<WebElement> links = driver.findElements(By.xpath("//a[@class='jsx-1291462554']"));

        links.get(0).click();
        //Wait here for some action to happen
        links.get(1).click();
                

The xpath varies depending on what you actually want to click, if the button element replace with:

//a[@class='jsx-1291462554']/button

If the button's click action changes the DOM you might encounter a StaleElementReference, in which case you may need to refetch (findElements) again before selecting the 2nd one.

DMart
  • 2,401
  • 1
  • 14
  • 19