1

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.

1
  • Try using css selector. driver.find_element_by_css_selector([src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg"]).click() Commented Dec 21, 2020 at 19:54

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.