Miqro
Miqro

Reputation: 477

Python Selenium: 2 buttons with same class?

I am trying to click on button with class btn3, but there is another button on same page with same class.

Button I want to click:

<div class="btn3">Follow</div>

Button I DON'T want to click:

<div class="btn3">Add Site/Page</div>

My code:

driver.find_element_by_class_name('btn3').click()

Upvotes: 0

Views: 2683

Answers (2)

kaaposc
kaaposc

Reputation: 174

Assuming these buttons are inside some other elements and not attached directly to body, you can change your code to

driver.find_element_by_css_selector('.first_div .btn3').click()

(change class name accordingly)

Upvotes: 0

Vikas Ojha
Vikas Ojha

Reputation: 6950

There are two ways to do this -

driver.find_elements_by_class_name('btn3')[0].click()

Or,

driver.find_element_by_link_text('Follow').click()

Upvotes: 3

Related Questions