Reputation: 13
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.
Upvotes: 0
Views: 7591
Reputation: 33384
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
Upvotes: 4
Reputation: 1769
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()
Upvotes: 0