`

Selenium Tutorial -6 How to Use CSS Selector for Identifying Web Elements for Se

 
阅读更多

http://www.softwaretestinghelp.com/css-selector-selenium-locator-selenium-tutorial-6/

 

In our previous Selenium tutorial we learned different types of locators. We also learned how to use: ID, ClassName, Name, Link Text, and Xpath locators for identifying web elements in a web page.

In continuation with that, today we will learn how to use CSS Selector as a Locator. This is our 6th tutorial in our free Selenium Training series.

Using CSS Selector as a Locator:

CSS Selector is combination of an element selector and a selector value which identifies the web element within a web page. The composite of element selector and selector value is known as Selector Pattern.

CSS Selector Selenium Locator

Selector Pattern is constructed using HTML tags, attributes and their values. The central theme behind the procedure to create CSS Selector and Xpath are very much similar underlying the only difference in their construction protocol.

Like Xpath, CSS selector can also locate web elements having no ID, class or Name.

So now gearing ahead, let us discuss the primitive types of CSS Selectors:

Using CSS Selector as a Locator

CSS Selector: ID

In this sample, we would access “Email” text box present in the login form at Gmail.com.

The Email textbox has an ID attribute whose value is defined as “Email”. Thus ID attribute and its value can be used to create CSS Selector to access the email textbox.

Creating CSS Selector for web element

Step 1: Locate / inspect the web element (“Email” textbox in our case) and notice that the html tag is “input” and value of ID attribute is “Email” and both of them collectively make a reference to the “Email Text box”. Hence the above data would be used to create CSS Selector.

Using CSS Selector as a Locator 2

Verify the locator value

Step 1: Type “css=input#Email” i.e. the locator value in the target box in the Selenium IDE and click on the Find button. Notice that the Email Text box would be highlighted.

Using CSS Selector as a Locator 3

Syntax

css=<HTML tag><#><Value of ID attribute>

  • HTML tag – It is tag which is used to denote the web element which we want to access.
  • – The hash sign is used to symbolize ID attribute. It is mandatory to use hash sign if ID attribute is being used to create CSS Selector.
  • Value of ID attribute – It is the value of an ID attribute which is being accessed.
  • The value of ID is always preceded by a hash sign.

Note: Also applicable for other types of CSS Selectors

  • While specifying CSS Selector in the target text box of Selenium IDE, always remember to prefix it with “css=”.
  • The sequence of the above artifacts is inalterable.
  • If two or more web elements have the same HTML tag and attribute value, the first element marked in the page source will be identified.

CSS Selector: Class

In this sample, we would access “Stay signed in” check box present below the login form at gmail.com.

The “Stay signed in” check box has a Class attribute whose value is defined as “remember”. Thus Class attribute and its value can be used to create CSS Selector to access the designated web element.

Locating an element using Class as a CSS Selector is very much similar to using ID, the lone difference lies in their syntax formation.

Creating CSS Selector for web element

Step 1: Locate / inspect the web element (“Stay signed in” check box in our case) and notice that the html tag is “label” and value of ID attribute is “remember” and both of them collectively make a reference to the “Stay signed in check box”.

Verify the locator value

Step 1: Type “css=label.remember” i.e. the locator value in the target box in the Selenium IDE and click on the Find Button. Notice that the “Stay signed in” check box would be highlighted.

Using CSS Selector as a Locator 4

Syntax

css=<HTML tag><.><Value of Class attribute>

  • – The dot sign is used to symbolize Class attribute. It is mandatory to use dot sign if Class attribute is being used to create CSS Selector.
  • The value of Class is always preceded by a dot sign.

CSS Selector: Attribute

In this sample, we would access “Sign in” button present below the login form at gmail.com.

The “Sign in” button has a type attribute whose value is defined as “submit”. Thus type attribute and its value can be used to create CSS Selector to access the designated web element.

Creating CSS Selector for web element

Step 1: Locate / inspect the web element (“Sign in” button in our case) and notice that the html tag is “input”, attribute is type and value of type attribute is “submit” and all of them together make a reference to the “Sign in” button.

Verify the locator value

Step 1: Type “css=input[type=’submit’]” i.e. the locator value in the target box in the Selenium IDE and click on the Find Button. Notice that the “Sign in” button would be highlighted.

Using CSS Selector as a Locator 5

Syntax

css=<HTML tag><[attribute=Value of attribute]>

  • Attribute – It is the attribute we want to use to create CSS Selector. It can value, type, name etc. It is recommended to choose an attribute whose value uniquely identifies the web element.
  • Value of attribute – It is the value of an attribute which is being accessed.

CSS Selector: ID/Class and attribute

In this sample, we would access “Password” text box present in the login form at gmail.com.

------------

The “Password” text box has an ID attribute whose value is defined as “Passwd”, type attribute whose value is defined as “password”. Thus ID attribute, type attribute and their values can be used to create CSS Selector to access the designated web element.

Creating CSS Selector for web element

Step 1: Locate / inspect the web element (“Password” text box in our case) and notice that the html tag is “input”, attributes are ID and type and their corresponding values are ”Passwd” and “password” and all of them together make a reference to the “Password” textbox.

Verify the locator value

Step 1: Type “css=input#Passwd[name=’Passwd’]” i.e. the locator value in the target box in the Selenium IDE and click on the Find Button. Notice that the “Password” text box would be highlighted.

Using CSS Selector as a Locator 5

Syntax

css=<HTML tag><. Or #><value of Class or ID attribute><[attribute=Value of attribute]>

Two or more attributes can also be furnished in the syntax. For example“css=input#Passwd[type=’password’][name=’Passwd’]”.

CSS Selector: Sub-string

CSS in Selenium allows matching a partial string and thus deriving a very interesting feature to create CSS Selectors using sub strings. There are three ways in which CSS Selectors can be created based on mechanism used to match the sub string.

Types of mechanisms

All the underneath mechanisms have symbolic significance.

  • Match a prefix
  • Match a suffix
  • Match a sub string

Let us discuss them in detail.

Match a prefix

It is used to correspond to the string with the help of a matching prefix.

Syntax

css=<HTML tag><[attribute^=prefix of the string]>                

  • – Symbolic notation to match a string using prefix.
  • Prefix – It is the string based on which match operation is performed. The likely string is expected to start with the specified string.

For Example: Let us consider “Password textbox”, so the corresponding CSS Selector would be:

css=input#Passwd[name^=’Pass’]

Match a suffix

It is used to correspond to the string with the help of a matching suffix.

Syntax

css=<HTML tag><[attribute$=suffix of the string]>                

  • – Symbolic notation to match a string using suffix.
  • Suffix – It is the string based on which match operation is performed. The likely string is expected to ends with the specified string.

For Example: Lets again consider “Password textbox”, so the corresponding CSS Selector would be:

css=input#Passwd[name$=’wd’]

Match a sub string

It is used to correspond to the string with the help of a matching sub string.

Syntax

css=<HTML tag><[attribute*=sub string]>                

  • – Symbolic notation to match a string using sub string.
  • Sub string – It is the string based on which match operation is performed. The likely string is expected to have the specified string pattern.

For Example: Lets again consider “Password textbox”, so the corresponding CSS Selector would be:

css=input#Passwd[name$=’wd’]

CSS Selector: Inner text

Inner text helps us identify and create CSS Selector using a string pattern that the HTML Tag manifests on the web page.

Consider, “Need help?” hyperlink present below the login form at gmail.com.

The anchor tag representing the hyperlink has a text enclosed within. Thus this text can be used to create CSS Selector to access the designated web element.

Syntax

css=<HTML tag><:><contains><(text)>

  • – The dot sign is used to symbolize contains method
  • Contains – It is the value of a Class attribute which is being accessed.
  • Text – The text that is displayed anywhere on the web page irrespective of its location.

This is one of the most frequently used strategies to locate web element because of its simplified syntax.

Owing to the fact that creating CSS Selector and Xpath requires a lot of efforts and practice, thus the process is only exercised by more sophisticated and trained users.

Next Tutorial #7: Proceeding ahead with our next tutorial, we would take the opportunity to introduce you with an extension of locating strategies. Thus, in the next tutorial, we would study the mechanism to locate web elements on Google Chrome and Internet Explorer.

We are covering Selenium Locators in more details as it is important part of Selenium Script creation.

分享到:
评论

相关推荐

    selenium-java-4.0.0-alpha-6_javaselenium_

    4. **selenium-devtools-4.0.0-alpha-6-sources.jar** 和 **selenium-remote-driver-4.0.0-alpha-6-sources.jar**、**selenium-api-4.0.0-alpha-6-sources.jar**、**selenium-support-4.0.0-alpha-6-sources.jar**:...

    selenium-server-standalone和Selenium-java的jar包

    在这个主题中,我们将深入探讨"**selenium-server-standalone**"和"**Selenium-java**"这两个jar包,以及它们在Java+Selenium自动化测试中的作用。 首先,**selenium-server-standalone.jar**是Selenium WebDriver...

    ruby selenium-client-1.2.18.gem

    4. command prompt go to –&gt; C:\selenium-remote-control-1.0.3\selenium-server-1.0.3&gt;java -jar selenium-server.jar 5. Download the RubyInstaller– The Ruby Installer is currently available only for the ...

    selenium-server-4.1.1.jar

    Selenium 服务器(selenium-server-4.1.1.jar)

    selenium-server-standalone-2.40

    在这个名为 "selenium-server-standalone-2.40" 的压缩包中,包含了Selenium Server的独立版本以及相关的Java库。 1. **Selenium Server Standalone**: Selenium Server Standalone是Selenium的核心组件之一,它...

    selenium-server-standalone-4.0.0-alpha-2.zip

    标题 "selenium-server-standalone-4.0.0-alpha-2.zip" 指的是 Selenium 的一个服务器独立版本的归档文件,该版本为 4.0.0 的 Alpha 2 版本。Selenium 是一个广泛使用的自动化测试工具,主要用于 Web 应用程序的测试...

    selenium-java-2.44.0、selenium-java-2.44.0-srcs、selenium-server-standalone-2.44.0

    首先,我们来了解一下 `selenium-java-2.44.0.jar`。这是一个预编译的Java库,其中包含了Selenium WebDriver的Java绑定。WebDriver是Selenium的一个核心部分,它提供了一个编程接口,允许测试脚本直接控制浏览器。...

    selenium css selector 定位详解

    ### Selenium CSS Selector定位详解 #### 一、引言 在自动化测试领域,Selenium作为一款强大的工具被广泛应用于Web页面的交互操作与测试。而CSS选择器(CSS Selector)是Selenium进行页面元素定位的一种非常重要的...

    selenium-selenium-4.5.0.zip源码

    在 `selenium-selenium-4.5.0.zip` 源码中,我们可以深入理解 Selenium 的内部实现,包括以下关键部分: 1. **WebDriver**: 这部分包含了各个浏览器驱动(如 ChromeDriver、GeckoDriver)的实现,它们作为桥梁,...

    selenium-server-standalone-3.0.0JAR包

    它的服务器独立版本,即 `selenium-server-standalone-3.0.0.jar`,是 Selenium 的核心组件之一,提供了远程控制浏览器的能力,支持多种浏览器如 Chrome、Firefox、IE 等。 该 JAR 包的版本号为 3.0.0,意味着它是 ...

    selenium-server-standalone-2.44.0.jar

    selenium-server-standalone-2.44.0, selenium最新服务器,

    selenium-server-standalone-3.141.0.jar

    最新版selenium-java,selenium-server-standalone-3.141.0.jar

    selenium-java-2.45.0.jar

    selenium-java-2.45.0.jar

    selenium-server-standalone-3.141.59

    selenium-server-standalone-3.141.59.jar selenium-server-standalone-3.141.59.jar

    selenium-server-standalone-3.9.1.jar

    selenium-server-standalone-3.9.1.jar,python插件,用于web自动化测试

    selenium-4.1.0-py3-none-any.whl

    selenium-4.1.0-py3-none-any.whl

    selenium-server-standalone-2.45.0和selenium-java-2.45.0(含srcs)

    "selenium-server-standalone-2.45.0" 和 "selenium-java-2.45.0(含srcs)" 提供了Selenium在Java环境下的核心组件,以及一个独立的服务器版本,方便进行Web应用的自动化测试。 1. **Selenium Server Standalone**...

    selenium-java-2.47.1.zip

    总之,"selenium-java-2.47.1.zip" 是一个用于自动化网页测试的重要工具,包含了一系列的Java库和资源,可以帮助你构建高效、可靠的Web应用测试框架。无论是初学者还是经验丰富的测试工程师,都能从中受益。

    selenium-htmlunit-driver-2.9.0

    selenium-htmlunit-driver-2.9.0jar包 selenium-htmlunit-driver-2.9.0jar包 selenium-htmlunit-driver-2.9.0jar包 selenium-htmlunit-driver-2.9.0jar包

    selenium-java-3.141.59.zip

    3. **By**:这个类提供了多种定位元素的方法,如 `By.id()`, `By.name()`, `By.cssSelector()` 等。 4. **Actions**:这个类允许你构建复杂的用户操作序列,比如拖放、双击等。 5. **Wait** 和 **Expected...

Global site tag (gtag.js) - Google Analytics