what is the solution for this the bug:

Question:

This is the code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service

s=Service("C:selenium driverchromedriver.exe")
driver = webdriver.Chrome(service=s)

driver.get("https://www.selenium.dev/selenium/web/web-form.html")

driver.implicitly_wait(5)

submit_button = driver.find_element(By.CLASS_NAME, "btn btn-outline-primary mt-3")
submit_button.click()

This is the error:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn btn-outline-primary mt-3"}
  (Session info: chrome=109.0.5414.120)
Asked By: Zeyad Magdy

||

Answers:

btn btn-outline-primary mt-3 — Because there are blank spaces between the words of class name, it is not able to locate the element.

Try by XPath instead:

submit_button = driver.find_element(By.XPATH, "//*[@class='btn btn-outline-primary mt-3']")
Answered By: Shawn

You are trying to locate an element by class name,

(By.CLASS_NAME, "btn btn-outline-primary mt-3")

but in CSS selector syntax, there are actually 3 class names provided: "btn", "btn-outline-primary", and "mt-3".

To locate this element, switch this to a CSS selector. Each class in a CSS selector is prefaced with a ".", e.g. .btn

driver.find_element(By.CSS_SELECTOR, ".btn.btn-outline-primary.mt-3").click()

You can also test your locators in the dev console. Press F12 to open it and type $$(locator) for a CSS selector or $x(locator) for an XPath. The CSS selector above would be $$(".btn.btn-outline-primary.mt-3") and it returns a single element. If you expand the return and hover over it, it will highlight the element on the page that corresponds to that locator. It’s the right button so we know it works and it’s finding the element we’re looking for.

Answered By: JeffC