Python: find_element_by_css_selector
To click on the Login button you can use either of the the following line of code :
LinkText :
driver.find_element_by_link_text("Login").click()
CssSelector :
driver.find_element_by_css_selector("a.login-btn > span.btn-text").click()
Getting more granular with the CssSelector you can also use the following line of code :
driver.find_element_by_css_selector("a.login-btn[data-bind='click:loginSection.loginClick'] > span.btn-text").click()
Update :
As you are seeing NoSuchElementException you can check this discussion
Python : How to use find_element_by_css_selector using Selenium
To crawl the text 72 you have to induce WebDriverWait for the visibility_of_element_located()
and you can use either of the following solutions:
Using
CSS_SELECTOR
:print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.u_cbox_chart_progress.u_cbox_chart_male>div.u_cbox_chart_per"))).text)
Using
XPATH
:print(WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='u_cbox_chart_progress u_cbox_chart_male']/div[@class='u_cbox_chart_per']"))).text)
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
Reference
You can find a couple of relevant discussions on NoSuchElementException in:
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
selenium: find element not working (should be quick, so plz help)
Use find_element
instead.
find_element_by_css_selector
doesn't exist anymore.
from selenium.webdriver.common.by import By
driver.find_element(By.CSS_SELECTOR, 'input[id*=onetrust-accept-btn-handler]')
selenium find_element_by_css_selector for the long class name
The CSS_SELECTOR
that you are using
.w-full.h-full.align-middle.object-cover.dark:brightness-80.dark:contrast-103.svelte-f3nlpp
does not really match any element in the HTML.
Instead, you should use this CSS_SELECTOR
:
div.w-full.h-full.align-middle img:not(.placeholder)
In code:
driver.maximize_window()
wait = WebDriverWait(driver, 30)
driver.get("https://raritysniper.com/nft-drops-calendar")
#time.sleep(1)
first_element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.w-full.h-full.align-middle img:not(.placeholder)")))
print(first_element.get_attribute('alt'))
print(first_element.get_attribute('src'))
Import:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Output:
NEON PLEXUS
https://media.raritysniper.com/featured/neon-plexus_1648840196045_3.webp
Process finished with exit code 0
selenium webdriver can't click on button using driver.find_element_by_css_selector
Please check in the dev tools
(Google chrome) if we have unique entry in HTML-DOM
or not.
xpath that you should check :
//a[contains(.,'View Cart')]
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL + F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
If it's a unique match then click it like below:
Code trial 1:
time.sleep(5)
driver.find_element(By.XPATH, "//a[contains(.,'View Cart')]").click()
Code trial 2:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'View Cart')]"))).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
Code trial 2 is recommended.
Selenium find_element_by_class_name and find_element_by_css_selector not working
You're using find_element_by_css_selector
incorrectly. Try
store_tag.find_element_by_css_selector('h3.cr__container-name-title.cr__text--subtitle.cr__textColor--colorDark300')
Note the dots instead of spaces.
Related Topics
How to Move Pandas Data from Index to Column After Multiple Groupby
What's the Difference Between _Builtin_ and _Builtins_
Matplotlib: How to Plot Images Instead of Points
How to Improve the Label Placement in Scatter Plot
What's a Faster Operation, Re.Match/Search or Str.Find
Remove Namespace and Prefix from Xml in Python Using Lxml
Can Existing Virtualenv Be Upgraded Gracefully
Pip - Fatal Error in Launcher: Unable to Create Process Using '"'
Use and Meaning of "In" in an If Statement
Python How to Write to a Binary File
Executing Multiple Statements with Postgresql via SQLalchemy Does Not Persist Changes
How to Get Around Declaring an Unused Variable in a for Loop
Run Child Processes as Different User from a Long Running Python Process
Python Read JSON File and Modify
Strange Behavior of Lists in Python