This is the HTML

<button type="button" class="d-block p-3 mx-auto btn btn-danger btn-lg xh-highlight">
<i class="fa fa-close mr-1">

And I'm trying with:

driver.find_element(By.XPATH, "//button[@class = 'd-block p-3 mx-auto btn btn-danger btn-lg']").click()


print driver.find_element(By.XPATH, "//button[contains(text(), 'Close')]").click()

driver.find_element(By.XPATH, "//button[@type = 'button' and contains(text(), 'Close')]").click()

This is the error I'm getting

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class = 'd-block p-3 mx-auto btn btn-danger btn-lg']"}
  (Session info: chrome=70.0.3538.110)
  (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17134 x86_64)
  • driver.find_element(By.CLASS_NAME,"d-block").click() is not correct, since this is only a part of the class. Try by css selector (we want to find button with class that starts with dblock): driver.find_element(By.CSS_SELECTOR,"button[class^='dblock]").click()
    – return
    Dec 18, 2018 at 0:00

1 Answer 1


Your code is failing at the first line of the snippet, trying to locate the button by the specified classes. It's a good idea to make the locators as simple as possible.

If there is only one button with btn-danger present on the page, you could use XPath //button[contains(@class, 'btn-danger')] Add as many classes as you need so it matches only the element you want. Also, don't forget to make sure the element is fully loaded before you try to locate it.

As a note, I have found odd that XPath //button[contains(text(), 'Close')] doesn't work.

  • Yes you are right I have added a wait and now it is working
    – Shoval
    Dec 19, 2018 at 4:30
  • @Shoval If this answer was what you were looking for, mark it as accepted by using the 'tick' icon next to it. :) Dec 27, 2018 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.