在按钮上查找特定文本

Find a specific text on a button

我有以下按钮。

<button class="btn-standard buyButton currency-coins">Buy Now for 3k <button>

我想查找按钮是否包含 "Buy Now"。 我用过类似的东西,但是没有用。

driver.find_element_by_xpath('//button[text()="Buy Now"]').click()

完整的 innerText 是 立即购买 3k。因此,要通过部分 innerText 识别并单击元素,即 立即购买,您必须诱导 WebDriverWait with expected_conditions set for the desired element_to_be_clickable() and can use either of the following based :

  • 使用 xpathcontains()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
    
  • 使用 xpathstarts-with()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(., 'Buy Now')]"))).click()
    
  • 注意:您必须添加以下导入:

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