- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use regular expressions in a CSS locator?
We can use regular expressions in a CSS locator. We can identify elements by matching their attributes partially with the help of regular expressions. In CSS, there are multiple methods to achieve this. They are listed below −
Using the wild character *. This means the string contains our given text.
Syntax− driver.find_element_by_css_selector("input[name*='sel']")It will search the input tag which contains the 'name' attribute containing 'sel' text.
Using the wild character ^. This means the string starts with our given text.
Syntax− driver.find_element_by_css_selector("input[name^='Tut']")
It will search the input tag which contains the 'name' attribute starting with 'Tut' text.
Using the wild character $. This means the string ends with our given text.
Syntax− driver.find_element_by_css_selector("input[name$='nium']")
It will search the input tag which contains the 'name' attribute ending with 'nium' text.
Example
Code Implementation with * wild character in css.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() # identifying the edit box with the help of * in css selector driver. find_element_by_css_selector("input[id*='sc-i']"). send_keys("Selenium") #to close the browser driver.close()
Example
Code Implementation with ^ wild character in css.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() # identifying the edit box with the help of ^ in css selector driver. find_element_by_css_selector("input[id^='gsc']"). send_keys("Selenium") #to close the browser driver.close()
Example
Code Implementation with $ wild character in css.
from selenium import webdriver driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") # to maximize the browser window driver.maximize_window() #get method to launch the URL driver.get("https://www.tutorialspoint.com/index.htm") #to refresh the browser driver.refresh() # identifying the edit box with the help of $ in css selector driver. find_element_by_css_selector("input[id$='id1']"). send_keys("Selenium") #to close the browser driver.close()
- Related Articles
- How to use CSS selector as a locator in Selenium?
- How to use regular expressions in css in Selenium with python?
- How to use regular expressions with TestNG?
- How do you use regular expressions in Cucumber?
- Use a character class in Java Regular Expressions
- How to use regular expressions in xpath in Selenium with python?
- How to use regular expressions (Regex) to filter valid emails in a Pandas series?
- Use the ? quantifier in Java Regular Expressions
- How do we use wildcard or regular expressions with a jQuery selector?
- How to split a string using regular expressions in C#?
- How to compare regular expressions in Perl and Python?
- How to match whitespace in python using regular expressions
- How to extract numbers from a string using regular expressions?
- How to remove consonants from a string using regular expressions in Java?
- How to remove vowels from a string using regular expressions in Java?
