• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / Write Dynamic CSS Selector in Selenium using multiple ways

Write Dynamic CSS Selector in Selenium using multiple ways

December 25, 2015 by Mukesh Otwani 45 Comments

Selenium_locators

Selenium supports multiple locators which will help you to identify web element and perform the operation based on your requirements.We will discuss about CSS Selector in Selenium and you will feel the importance of the same.

Selenium supports 8 locators, please check below screenshot.

 

Selenium_locators

 

This is one of the most frequent asked questions in interview like how many selectors are present and which one is best and where to use which locator?

In terms of performance, CSS perform well as compared to XPATH and CSS will not change based on browsers, that is it will behave same in all browsers but xpath will behave differently in IE browser.

Recently I published an article on IE browser issues and raised the same point.

 

In this post we will discuss some basics CSS Selector in Selenium and usage of them.

Please find the below table which will give you brief introduction about different ways to write CSS in Selenium.

I have listed down only limited one but widely used in any application.

 

Symbol used while writing CSS selector in Selenium Webdriver

 

Css Selector Table

 

 

Please check the Youtube video for CSS 

 

 

 

 

Different ways to write CSS selector in Selenium to identify Dynamic elements

 

I will use WordPress application for demo purpose and will try to show all ways.

Before starting with CSS in Selenium make sure you have firebug installed and active.

If you are completely new to Selenium then download firebug and firepath and start your CSS exercise.

 

Start-Firebug-Select-CSS

 

 

Now type only  “a” in the search bar and you will notice all links with be highlighted but we do not want all the link so we will add some filter condition to get unique match.

 

 

Get all elements

 

Now have a close look and check what each link is doing.

 

 

get all input tags

 

Last time just type input and you will get 6 matching nodes.

It means it is returning all elements which, comes under input tags.

Check W3School link if you are not familiar with input tag.

get login using attribute

 

Now you are confident with single tag. So now lets add some condition and check the result.

 

 

 

Find CSS Selector using Single Attribute

 

 

Syntax- tagname[attribute='value']



Example- input[id='user_login']

 

CSS Single attribute

 

 

 

Find CSS using Multiple attributes.

 

Syntax- tagname[attribute1='value1'][attribute2='value2']

 

 

Selenium_css_multiple_attribue

 

 

Find CSS using id and Class name

 

Syntax using ID

tagname#id



Syntax using Classname

tagname.classname

Class name generally will not be unique so always try class name with some other attributes.

Selenium_Css_Id_attribute

 

Selenium_Css_Class_attribute

 

 

Find CSS using contains

In CSS we will use * symbol to check particular attribute contains that value or not

Syntax- tagname[attribute*='value']

In below example it will partially check if link title contains “Password lost ” if yes then it will match.

I have taken only link in this example but you can take any tag and any attribue.

 

8_css

One more example with id attribute

9_css

 

 

 

Find CSS using Start-with

 

Syntax- tagname[attribute^='value']

In below example I have taken only link but you can take any element.

 

CSS Start-with method

 

 

Find CSS using ends-with

Syntax- tagname[attribute$='value']

 

 

CSS Selector in Selenium

 

Now you must be having a clear picture in mind for CSS Selector in Selenium and I would recommend to try all technique at least once using same or some other applications.

Check this link from SauceLabs for CSS Selector

You can always use XPATH and CSS in your script depends on your requirement.

If you want to run your test script in all browser like Cross Browser testing then go with CSS because it will have same in all browsers.

 

Please drop your comment or your feedback below if any and if you find this article then please share with your friends as well.

 

You can like our Facebook Page for more updates Learn Automation Page
Join our facebook group for any query Selenium Group Discussion

 

 

Filed Under: Basic Selenium Tagged With: CSS Selector with Selenium

Reader Interactions

Comments

  1. jay says

    May 13, 2020 at 7:24 AM

    Hi mukesh, how to use Chropath in chrome? can you share any links?

    Reply
    • Mukesh Otwani says

      May 13, 2020 at 10:35 AM

      Hi Jay,

      Thanks for approaching me. Please check this link https://www.youtube.com/watch?v=Su3Vku9bmuI

      Reply
  2. Chris says

    April 15, 2020 at 9:30 AM

    Hi Mukesh,

    Is there a way to use css selector in an Appium Webview for identical radio button as shown below in xml format

    All these CSS Selector I have tried but receiving 404 error
    My code option 1
    driver.findElement(By.cssSelector(“p:contains(‘+ Add Another’)”)).click();
    My code option 2
    driver.findElement(By.cssSelector(“li div input.radio p:contains(‘+ Add Another’)”)).click();
    My code option 3
    driver.findElement(By.cssSelector(“input[type=’radio’][value=’+ Add Another’]”)).click();

    XML:

    Use Permanent Address

    + Add Another

    Reply
    • Mukesh Otwani says

      April 15, 2020 at 10:01 AM

      Hi Chris,

      Kindly check this link http://appium.io/docs/en/commands/element/find-elements/

      Reply
  3. vissu says

    April 9, 2020 at 6:13 PM

    Hi Mukesh,

    I have a scenario After clicking on exported to excel button data should exported to excel(Excel file is downloaded) But How to give wait To Script Till excel file is downloaded.

    Usually it takes 50 sec to one minutes to download file after clicking on exported to excel button

    Reply
    • Mukesh Otwani says

      April 10, 2020 at 8:56 PM

      Hi Vissu,

      There is a library called Watchservice API using which you check changes in a folder. Check this link https://howtodoinjava.com/java8/java-8-watchservice-api-tutorial/

      Reply
  4. Angle says

    February 26, 2020 at 10:08 AM

    Hi Mukesh,
    I have recently started learning automation through your videos..and currently I am watching
    https://www.youtube.com/watch?v=Hby6_WiidqM&list=PL6flErFppaj2ArNxLyR4nQ4JV8qFc56-M&index=12
    I am unable to add fire path/firebug add on in firefox…
    it seems it’s discontinued since 2017
    so is there any alternative plugin/addon of firepath that i can use to practice css/xml locators..
    or how can i practice in chrome.

    Reply
    • Mukesh Otwani says

      February 26, 2020 at 5:57 PM

      Hi Angle,

      You can use chropath for firefox & chrome.
      Chrome: https://chrome.google.com/webstore/detail/chropath/ljngjbnaijcbncmcnjfhigebomdlkcjo?hl=en
      Firefox: https://addons.mozilla.org/en-US/firefox/addon/chropath-for-firefox/

      Reply
  5. Shashank says

    April 4, 2019 at 4:20 PM

    Hi Mukesh.

    Is it possible to write css for visible text.

    Ex: div:contains(‘Container’)

    Where html for above is

    div

    Container
    .

    I did not work for me. What is css equivalent for xpath text() function

    Reply
    • Mukesh Otwani says

      April 4, 2019 at 6:11 PM

      Hi Shashank,

      Apologies, I never tried such combination and don’t know whether it is syntactically correct or not.

      Reply
  6. Shambo says

    February 16, 2017 at 1:06 AM

    Hi Mukesh
    Still its not working…its ben so many days that my eclipse is not working.How to fix this has been a headache for me??

    As u said i tried with this version of firefox(51.0.1 (32-Bit)).But still no luck

    Reply
    • Mukesh Otwani says

      February 16, 2017 at 11:47 AM

      Hi Shambo, I am using FF 46 now and its working fine.

      Reply
  7. Shambo says

    January 31, 2017 at 1:19 AM

    Yahh Mukesh i will definitely reply you very soon.Thanks for ur reply

    Reply
  8. Shambo says

    January 28, 2017 at 9:08 PM

    Heyy Mukesh,

    This is Shambo
    Thanks for your reply.
    Java-1.8 version
    Selenium-2.53.1
    Firefox-52.0b1 (64-bit)

    Plese tell me the solution.

    Reply
    • Mukesh Otwani says

      January 30, 2017 at 9:20 AM

      Hi Shamboo,

      Try with Firefox 32 bit version & don’t use beta version. Reply back with your comments.

      Reply
  9. Shambo says

    January 27, 2017 at 8:26 PM

    Hi sir.
    Please resolve this issue asap.I am not able to practice from a very long time in eclipse.Whenever i run any code,i get this error.

    1.org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms.

    2.Exception in thread “main” org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary(C:\Program Files\Mozilla Firefox\firefox.exe) on port 7055; process output follows:

    please provide me the output.I am in dire need of the solution

    Reply
    • Mukesh Otwani says

      January 28, 2017 at 6:01 PM

      Hi Shamboo,

      Kindly tell me the version of java, selenium, & firefox which you are using?

      Reply
  10. Veeresh says

    January 24, 2017 at 5:58 PM

    Hi Mukesh
    Can you tell me the disadvantages if we use CSS over Xpath

    Reply
    • Mukesh Otwani says

      January 25, 2017 at 5:23 PM

      Hi Veeresh,

      There is always a question among selenium user about usage of CSS or Xpath. CSS works best if you are using IE but also it also works in Chrome and firefox. Traversing the DOM using CSS in IE8 doesn’t works and here xpath performs. No doubt CSS works faster than Xpath. But when automating complex applications, you will find difficulty with creation of CSS and that time xpath help you to traverse through DOM in detail.

      Finally, I would say that if your script works with CSS then go with it otherwise go for Xpath.

      Reply
  11. anurag says

    January 13, 2017 at 5:26 PM

    plz resolve this issue
    how to handle css value that changes each time when page reloads
    driver.findElement(By.cssSelector(“#ctl00_ContentPlaceHolderMain_GvLairagePen_ctl01_ddlAddLairageSize > option[value=\”132\”]”)).click();
    above value changes every time.i have to check every time source page to know last one value plz tell how to handle this dynamically changed value.

    Reply
    • Mukesh Otwani says

      January 13, 2017 at 10:31 PM

      Hi Anurag,

      Give a try to relative xpath

      Reply
  12. Nilesh says

    November 18, 2016 at 2:40 PM

    hi Mukesh,
    Can we use combination of xpath and css?

    Reply
    • Mukesh Otwani says

      November 23, 2016 at 3:24 PM

      No Nilesh.

      Reply
  13. sanjay says

    November 10, 2016 at 3:25 PM

    Thank you sir, all your tutorials really helped me alot. You are one of the best selenium tutor i could say.

    Reply
    • Mukesh Otwani says

      November 10, 2016 at 4:15 PM

      Hey Sanjay,

      Thank you so much for your kind word. Keep learning 🙂

      Reply
  14. Krishnateja says

    October 13, 2016 at 4:58 AM

    It’s very helpful and detail.

    Reply
    • Mukesh Otwani says

      October 13, 2016 at 8:43 AM

      Thanks Krishna

      Reply
  15. Gaurav Khurana says

    September 27, 2016 at 10:25 PM

    Nice explanation thanks

    Do using the xpath will always cause failures in different browsers ? Then i think we should not use xpath and use CSS as most of the applications these days are tested on most of the browsers.

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 10:48 AM

      Hi Gaurav,

      Some application do not have id and other attributes in most of cases so we have to take help of xpath and CSS. I always use my custom xpath and CSS to make my script robust.

      Reply
  16. Mukesh says

    September 22, 2016 at 2:22 PM

    Thanks so much for the above info. Can you please share the syntax for using CSS with multiple attributes?

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 2:44 AM

      Thanks mate will update the same.

      Reply
  17. Shaik Ghouse says

    August 23, 2016 at 8:23 PM

    Super explanations..Its very easy to understand.

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 11:12 PM

      Thanks Shaik 🙂

      Reply
  18. pankaj says

    May 26, 2016 at 4:04 PM

    Thank’s man you are simply awesome .I try to learn selenium with different – different site but your article’s are so simple that anyone can understand it easily .I follow your all article’s and video’s.

    Reply
    • Mukesh Otwani says

      May 30, 2016 at 12:37 AM

      Thanks Mate keep visiting.

      Reply
  19. manas says

    April 3, 2016 at 1:35 AM

    sir could u post dynamic webtable example

    Reply
    • Mukesh Otwani says

      April 4, 2016 at 11:47 PM

      Hi Manas,

      Kindly check below post.

      http://learn-automation.com/handle-calender-in-selenium-webdriver/

      Reply
  20. Tarun says

    January 29, 2016 at 10:32 AM

    Hi Mukesh,

    Could you tell me how to identify visible text using css, like in xpath we make use of text().

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 4:31 PM

      Hi Tarun,

      will update you soon on this.

      Reply
  21. Mallikarjun says

    January 4, 2016 at 4:13 PM

    Useful as always

    Reply
    • Mukesh Otwani says

      January 6, 2016 at 1:03 AM

      Thanks Mallikarjun

      Reply
  22. Roopesh CN says

    December 26, 2015 at 12:49 AM

    Thank you for sharing good about CSS selector.

    Reply
    • Mukesh Otwani says

      December 29, 2015 at 10:07 PM

      Thanks Roopesh

      Reply
  23. rama says

    December 25, 2015 at 3:17 PM

    thanks sir,your Tutorial r very useful .

    Reply
    • Mukesh Otwani says

      December 29, 2015 at 10:09 PM

      Thanks Rama

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • Syllabus For Playwright Online Training Program
  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?