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 Overflowfinding items with selenium by CSS selector using multiple attributes
Please help: selenium canโt find the css selector for an element
When using Selenium (with Python) is it bad practice to find elements by xpath?
Selenium can't find the element I'm searching for
Videos
Hi all. I'm trying to use Selenium to find an element by CSS selector and although I've tried using the documentation, I think I'm misunderstanding something.
This is what I'm trying:
driver.find_element(By.CSS_SELECTOR,f"img [src='images/icons/new.gif'][alt='Add new item to list']").click()
This is the element I want to find and click:
<img src="images/icons/new.gif" alt="Add new item to list">
The error I get is as follows:
Exception has occurred: NoSuchElementException
Message: no such element: Unable to locate element: {"method":"css selector","selector":"img [src='images/icons/new.gif'][alt='Add new item to list']"}I can see it's on the page, however, so I'm not sure what I'm missing. I can find other items using XPATH and ID so selenium is up and running, just to rule that out. Can anybody suggest anything?
(I'm using selenium 4.8.2 and python 3.11.1, if that helps)
Finding elements by CSS selector is the only way Al Sweigart has taught me. I've tried finding by xpath and by class name and still got an exception although idk if I did those right
here's my code, dont laugh at the website:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.barstoolsports.com/shows/call-her-daddy/ask')
elem_for_first_name = browser.find_element_by_css_selector('.quantumWizTextinputPaperinputInput')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/fakename/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/home/fakename/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/home/fakename/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/fakename/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .quantumWizTextinputPaperinputInputI get similar exceptions when trying xpath and class name.
There are a total of 2 (maybe 3) elements I need from this webpage:
-
element for entering your first name
-
element for asking your question
-
(maybe) the submit button
Number 3 might not be that hard since selenium can automatically submit a form after you send the keys. I just haven't gotten to this point yet because I cant find the first 2 elements to begin with.
Please, good pythoners of reddit, help a girl out