0

I have the following button.

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

I want to find if the button contains "Buy Now". I used something like this, but it didn't work.

driver.find_element_by_xpath('//button[text()="Buy Now"]').click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Paul Vio
  • 57
  • 1
  • 7
  • What does _but it didn't work_ mean? Have you tried reading the documentation for what I presume is Selenium (since your question doesn't contain that information, I can only guess) – AMC Dec 25 '19 at 01:39
  • `driver.find_element_by_xpath('//button[text()="Buy Now"]').click()` this one didn't work:) – Paul Vio Dec 25 '19 at 10:51

1 Answers1

3

The complete innerText is Buy Now for 3k. So to identify and click on the element through partial innerText i.e. Buy Now you have to induce WebDriverWait with expected_conditions set for the desired element_to_be_clickable() and can use either of the following based Locator Strategies:

  • Using xpath and contains()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(., 'Buy Now')]"))).click()
    
  • Using xpath and starts-with()

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(., 'Buy Now')]"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I get the following error: `StaleElementReferenceException: Message: The element reference of – Paul Vio Dec 24 '19 at 22:07
  • I forgot to mention, the 'buy button' appears after a click of a button. ex: I press click on a button and after 1 second appear "Buy button' – Paul Vio Dec 24 '19 at 22:07
  • @PaulVio Checkout the updated answer and let me know the status. – undetected Selenium Dec 24 '19 at 22:10
  • 1
    Yes, it's working man. Thank you. But can you explain to me what means webdriverwait and that "EC" in 2-3 words? – Paul Vio Dec 24 '19 at 22:20
  • @PaulVio Updated the answer with all the required references. Let me know if you need further help. – undetected Selenium Dec 24 '19 at 22:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204817/discussion-between-paul-vio-and-debanjanb). – Paul Vio Dec 24 '19 at 22:29