LuCash
LuCash

Reputation: 23

Click on specific Button (Selenium Python)

here I have 2 buttons:

Clicked Button:

<button type="button" data-testid="favorite-button" class="styles__button--1wSNn styles__neutral--17MuV styles__elevation--2fhDh styles__elevationMedium--2eus4 styles__circle--3zgIv styles__small--127Kw"><span class="styles__iconAfter--3xNI0"><div class="FavoriteIcon__icon--2fuH8 FavoriteIcon__small--2hXns FavoriteIcon__favorited--zicAG"><img class="styles__image--2CwxX" src="/boom/client/f0605f03fa478593f75f791e8eea8889.svg" data-testid="heartFilled" alt="Favorited"></div></span></button>

Unclicked Button:

<button type="button" data-testid="favorite-button" class="styles__button--1wSNn styles__neutral--17MuV styles__elevation--2fhDh styles__elevationMedium--2eus4 styles__circle--3zgIv styles__small--127Kw"><span class="styles__iconAfter--3xNI0"><div class="FavoriteIcon__icon--2fuH8 FavoriteIcon__small--2hXns"><img class="styles__image--2CwxX" src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg" data-testid="heartOutline" alt="Favorite"></div></span></button>

How can I make it click only on the unclicked buttons?

I've already tried that: driver.find_element_by_xpath("//input[@type="image"][@src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg"]).click()

But it doesn't seem to work.

Thanks.

Upvotes: 1

Views: 346

Answers (1)

KunduK
KunduK

Reputation: 33384

To Click unclicked button Use the below Xpath to click.

driver.find_element_by_xpath("//button[.//img[data-testid='heartOutline']]").click()

To avoid synronization Issue Use WebDriverWait() and wait for element_to_be_clickable()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[.//img[data-testid='heartOutline']]"))).click()

you need to import below libraries.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 1

Related Questions