-1

Hi i am new to Automation testing and got stuck with the clicking the button with same class name.I want to click the specific button when all the classes and id are same for all the section.

<div class="race-category-block">
<div class="info-block race-detail-block">
<a href="organizer-view-race?race=38"><div class="about-race">
<p class="race-name">Vsd</p>
<div class="date-location has-v-line-group">
<span class="has-v-line">28-01-2020 </span>
<span class="has-v-line no-v-line">Chennai</span>
<span style="display:none" id="raceDirectorName">Raajesh</span>
</div>
</div>
</a>
<div class="race-detail-status"></div>
<div class="race-detail-button-block">
<button class="btn btn-outline add-race" data-value="38">Add a race</button>
</div>
</div>
</div>

The above section is repeated for number of times

`Here I want to click the button in the button tag "Add a race" using the text in the Vsd

. because the text will only be the unique to choose, other class and ids are used many times in the same page.
JeffC
  • 22,180
  • 5
  • 32
  • 55
karthi
  • 3
  • 3

6 Answers6

0

You can make an array of all the elements with the same ID.

IList<IWebElement> Buttons = driver.FindElements(By.CssSelector("button.btn.btn-outline.add-race"));

And after you can click any of those Buttons, you can write in the console how many buttons are called, and you can later click again.

Buttons[1].Click();
Buttons[2].Click();
0

I think you could use the following xpath to find based on text in Vsd element and click corresponding "Add a race" button.

Xpath that could be used:

"//p[@class='race-name' and text()='Vsd']/parent::div/parent::a/following-sibling::div/button"
Gleeson
  • 231
  • 2
  • 5
0

As the desired element is having the unique text Add a race so to locate and click() on the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using Java and xpath with innerText:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-outline add-race' and text()='Add a race']"))).click();
    
  • Using Python and CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.race-detail-button-block>button.btn.btn-outline.add-race"))).click()
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can use the XPath locator to click the desired button

//p[text()='Vsd']//following::button
JeffC
  • 22,180
  • 5
  • 32
  • 55
0

Actually this worked for me

Event_name=driver.find_element_by_xpath("//p[text()='Vsd']")
Event_name.find_element_by_xpath("//button[@class='btn btn-outline add-race']").click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
karthi
  • 3
  • 3
-1

you can use index Xpath:

this.driver.findElement(By.xpath("/html/body/div/div/div[2]/button")).click();

please cross verify the XPath value, you can use chrome to identify the XPath by: 1. right-click on the button 2. select inspect 3. on the right element page you need to right-click on the element and copy -> full XPath

please note that using XPath is not recommended because the automation will be broken if any change will be done on the HTML structure.

Yaman Jain
  • 1,254
  • 11
  • 16