In this Python Selenium lesson, we want to learn How to Use CSS Selectors in Python Selenium, CSS selectors are powerful way to target and manipulate elements on web pages. when you want to automate web pages with Python Selenium, then CSS selectors are one of the easiest way that you can target specific elements in the web page, in this lesson we will talk about the CSS selector technique for locating web elements in Selenium.
What are CSS Selectors?
CSS selectors are patterns used to select and style HTML elements based on their attributes, IDs, classes, or hierarchy inside the document structure. CSS selectors allows you to target elements with flexibility, and this will be popular choice for web developers and automation engineers.
This is the complete code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from selenium import webdriver # Launch Chrome browser driver = webdriver.Chrome() # Navigate to the login page driver.get("https://demoqa.com/login") # Find the username and password input fields and enter credentials username_input = driver.find_element("css selector", "#userName") password_input = driver.find_element("css selector", "#password") username_input.send_keys("geekscoders") password_input.send_keys("Geekscoders@123") # Submit the form login_button = driver.find_element("xpath", "//*[@id='login']") login_button.click() # Close the browser driver.quit() |
Now let’s describe above code, first we have imported the necessary modules.
1 |
from selenium import webdriver |
In here we have created an instance of the Chrome web driver using webdriver.Chrome(). Make sure you have the Chrome driver executable in your system PATH or provide the path to it explicitly, or you can directly add that in your working directory where your Python code exists.
1 |
driver = webdriver.Chrome() |
You can download the drivers from here.
The get() method is used to navigate the web driver to the specified URL, in this case, the login page of the website https://demoqa.com/login, we already have an account with username and password in this website, because we will need that in Python Selenium code.
1 |
driver.get("https://demoqa.com/login") |
These lines uses find_element() method to locate the username and password input fields using CSS selectors (“css selector”). The CSS selector #userName selects the element with the ID “userName”, and #password selects the element with the ID “password”. send_keys() method is then used to enter the provided credentials.
1 2 3 4 5 |
username_input = driver.find_element("css selector", "#userName") password_input = driver.find_element("css selector", "#password") username_input.send_keys("geekscoders") password_input.send_keys("Geekscoders@123") |
In here, we have used find_element() method with an XPath selector (“xpath”) to locate the login button with the ID “login” (//*[@id=’login’]). click() method is then used to simulate a click on the button, submitting the login form.
1 2 |
login_button = driver.find_element("xpath", "//*[@id='login']") login_button.click() |
And at the end we use the quit() method to close the browser window and terminate the web driver session.
1 |
driver.quit() |
FAQs about Using CSS Selectors in Python Selenium:
Q: What are CSS selectors, and why are they important in Selenium?
A: CSS selectors are patterns used to select and style HTML elements based on their attributes, IDs, classes or hierarchy inside the document structure. In Python Selenium, CSS selectors are one of the important selector for locating elements on web pages.
Q: How do I find elements using CSS selectors in Selenium?
A: You can use find_element_by_css_selector() method in Python Selenium to locate elements based on CSS selector expressions. This method returns the first matching element found on the page.
Q: Can I use CSS selectors to locate multiple elements in Python Selenium?
A: Yes, you can use find_elements_by_css_selector() method to locate multiple elements. This method returns a list of all matching elements found on the page.
Q: What types of CSS selectors are available in Python Selenium?
A: Python Selenium supports different types of CSS selectors, including element selectors, class selectors, ID selectors, attribute selectors, descendant selectors, child selectors, pseudo-classes, and more.
Q: How do I write efficient CSS selectors in Python Selenium?
A: To write efficient CSS selectors, be specific and concise, leveraging class and ID selectors whenever possible. Validate CSS selectors thoroughly to ensure they accurately target the desired elements and combine CSS selectors with other locator strategies provided by Selenium for optimal results.
Learn More on Python Selenium
- Python Selenium Web Scraping
- Test Automation with Python Selenium
- Python Selenium WebDriver Tutorial
- Browser Interaction in Python Selenium
- Browser Profiles in Python Selenium
- Python Selenium Frames
Subscribe and Get Free Video Courses & Articles in your Email