Yes, the Locator Strategy below:
submit_button = driver.find_element_by_css_selector("input[type='submit']")
is syntactically correct. But as per the copy css selector it should have been:
submit_button = driver.find_element_by_css_selector("div > button[type='submit']")
Note:
find_element_by_*commands are deprecated. Please use find_element() instead
Accordingly you can also use:
submit_button = driver.find_element(By.CSS_SELECTOR, "input[type='submit']")
As per copy css selector:
submit_button = driver.find_element(By.CSS_SELECTOR, "div > button[type='submit']")
Answer from undetected Selenium on Stack OverflowCss Selector button click with selenium (python) - Stack Overflow
Selenium | css-selector not found
Please help: selenium can’t find the css selector for an element
finding items with selenium by CSS selector using multiple attributes
Why Use CSS Selectors in Selenium?
Why are CSS Selectors generally faster than XPath in Selenium?
How do CSS Selectors improve test script maintainability in Selenium?
Videos
As you've mentioned
The class name has spaces in it, which lead me to use the css_selector
this is right approach, however you should also make sure that one
- One should remove the space and put a
. .represent class in CSS.
So the below code should work:
driver.find_element(By.CSS_SELECTOR, ".btn.btn-alt.see-full-list-btn")
or you can even use it with the tag a
driver.find_element(By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn")
or the recommended solution would be to use with explicit waits:
see_full_list_button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn")))
see_full_list_button.click()
Imports:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
There is no necessity to focus on the element HTML after the click is already invoked.
As per the HTML
<a href="#" class="btn btn-alt see-full-list-btn">See Full List</a>
you can use either of the following locator strategies:
Using link_text:
driver.find_element(By.LINK_TEXT, "See Full List").click()Using css_selector:
driver.find_element(By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn").click()Using xpath:
driver.find_element(By.XPATH, "//a[@class='btn btn-alt see-full-list-btn' and text()='See Full List']").click()
Ideally to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "See Full List"))).click()Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.btn.btn-alt.see-full-list-btn"))).click()Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='btn btn-alt see-full-list-btn' and text()='See Full List']"))).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