【问题标题】:Click on specific Button (Selenium Python)单击特定按钮(Selenium Python)
【发布时间】:2021-03-31 14:03:35
【问题描述】:

这里有 2 个按钮:

点击按钮:

<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>

未点击的按钮:

<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>

如何让它只点击未点击的按钮?

我已经尝试过: driver.find_element_by_xpath("//input[@type="image"][@src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg"]).click()

但它似乎不起作用。

谢谢。

【问题讨论】:

  • 尝试使用 css 选择器。 driver.find_element_by_css_selector([src="/boom/client/fe5b59d42e7d54796992f8f9914d3e45.svg"]).click()

标签: python python-3.x selenium xpath webdriverwait


【解决方案1】:

点击unclicked按钮使用下面的Xpath点击。

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

为避免同步问题使用WebDriverWait() 并等待element_to_be_clickable()

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

您需要导入以下库。

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

【讨论】:

    猜你喜欢
    • 2020-01-10
    • 1970-01-01
    • 2021-04-07
    • 2021-03-18
    • 1970-01-01
    • 2013-11-12
    • 2018-08-07
    • 2020-12-16
    • 2020-07-28
    相关资源
    最近更新 更多