0

Hi I am new at using selenium and I am automating a certain process for my work. I have successfully been able to fill in forms and click on several buttons but when verifying my MFA, the button to verify doesn't work.

HTML for button:

<button class = "btn blue trustdevice trustbtn" onclick="updateTrustDevice(true)"> 
<span class ="loadwithbtn" style="display: none;"></span> 
<span class "waittext">Trust</span> 
</button>

My code:

browser.find_element_by_class_name("btn blue trustdevice trustbtn").click() 

I receive this error selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn blue trustdevice trustbtn"}

I also tried

elements = browser.find_element_by_class_name("btn blue trustdevice trustbtn")
for e in elements:
e.click()

but received the same error. Please let me know if any more info is required!

EDIT:

button = browser.find_element_by_class_name("btn blue trustdevice trustbtn")

gives me the same error message as well.

2
  • Is it possible that there's a delay before the button HTML is rendered that way? If so, you can use an explicit wait to wait until the element shows up. Commented Jul 27, 2020 at 18:13
  • @shynjax287 I don't believe so. I was able to immediately click it with my mouse. Commented Jul 27, 2020 at 18:21

2 Answers 2

4

find_element_by_class_name() only accept single classname. Instead use css selector.

Induce WebDriverWait() and wait for element_to_be_clickable()

You can use either of locator.

Css Selector:

WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,".btn.blue.trustdevice.trustbtn"))).click()

OR

Xpath:

WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//button[./span[text()='Trust']]"))).click()

You need to import below libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Sign up to request clarification or add additional context in comments.

Comments

0

It's a common issue that people tend to forget that you have to add some time.sleep() between your clicks to let the page load. So i would suggest to add:

import time

# action

time.sleep(5) #wait 5 seconds

# next action

Also you can use seleniums waits:

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

1 Comment

When I try button = browser.find_element_by_class_name("btn blue trustdevice trustbtn") it also gives me the same error. I don't believe it is due to the page loading.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.