本文共 9765 字,大约阅读时间需要 32 分钟。
It is one of the locators in selenium using which we identify web elements on the web page. CSS stands for Cascading Style Sheets, which is used for styling the different elements of an HTML webpage. CSS Selector locator is always the best way to locate elements on the web page. CSS is always the same irrespective of browsers.
它是Selenium中的一种定位剂,通过它我们可以识别网页上的Web元素。 CSS代表级联样式表,用于样式化HTML网页的不同元素。 CSS选择器定位器始终是在网页上定位元素的最佳方法。 不论浏览器如何,CSS始终相同。
Below are some of the mainly used formats of CSS Selectors.
以下是一些CSS选择器主要使用的格式。
css=tag#id
Example:
例:
Value to be added in the By.cssSelector
method:
要在By.cssSelector
方法中添加的值:
css=input#Email
package com.journaldev.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver;public class SeleniumCSSLocators { public static void main (String [] args){ WebDriver driver = new ChromeDriver(); driver.get("https://www.gmail.com"); // Here Tag = input and Id = Email driver.findElement(By.cssSelector("input#Email")).sendKeys("journaldev"); } }
If multiple elements have same HTML tag and class, then selenium will recognize first one.
如果多个元素具有相同HTML标签和类,则Selenium将识别第一个。
Syntax: css=tag.class
语法 : css=tag.class
Example:
例:
Value to be added in the By.cssSelector method:
要在By.cssSelector方法中添加的值:
css=input.inputtext
package com.journaldev.selenium;import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class SeleniumCSSLocators { public static void main (String [] args){ WebDriver driver = new ChromeDriver(); driver.get("https://www.facebook.com/"); // Here Tag = input and Class = email driver.findElement(By.cssSelector("input.inputtext[name=email]")).sendKeys("journaldev"); } }
If multiple elements have the same HTML tag and attribute, then selenium will recognize the first one.
如果多个元素具有相同HTML标签和属性,则Selenium将识别第一个元素。
Syntax:
语法 :
css=tag[attribute=value]
Example:
例:
Value to be added in the By.cssSelector method:
要在By.cssSelector方法中添加的值:
css=input[name=identifier]
package com.journaldev.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class SeleniumCSSLocators { public static void main (String [] args){ WebDriver driver = new ChromeDriver(); driver.get("https://www.gmail.com"); // Here Tag = input and Id = identifier driver.findElement(By.cssSelector("input[name=identifier]")).sendKeys("journaldev"); } }
Syntax:
句法:
css=tag.class[attribute=value]
Example:
例:
Value to be added in the By.cssSelector method:
要在By.cssSelector方法中添加的值:
css=input.inputtext[name=email]
package com.journaldev.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class SeleniumCSSLocators { public static void main (String [] args){ WebDriver driver = new ChromeDriver(); driver.get("https://www.facebook.com/"); // Here Tag = input and Class = email driver.findElement(By.cssSelector("input.inputtext[name=email]")).sendKeys("journaldev"); } }
There are some special CSS selectors to match partial values of the attributes like ^, $, and *.
有一些特殊CSS选择器可以匹配^,$和*等属性的部分值。
In order to select the element that starts with something, we use ^ which means ‘starts with’.
为了选择以某些内容开头的元素,我们使用^表示“开头为” 。
Syntax:
句法:
css=<[attribute^=string prefix]
Value to be added in the By.cssSelector method:
要在By.cssSelector方法中添加的值:
css=input.input[id^='Em']
In the script add the below step to find the element and write a text as “journaldev”
在脚本中,添加以下步骤以查找元素并将文本写为“ journaldev”
package com.journaldev.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CSSLocatorsStartswith { public static void main (String [] args){ WebDriver driver = new ChromeDriver(); driver.get("https://www.facebook.com/"); driver.findElement(By.cssSelector("input[id^='Em']")).sendKeys("journaldev"); } }
In order to select the element that ends with something, we use $ which means ‘ends with’.
为了选择以某事物结尾的元素,我们使用$表示“以...结尾” 。
Syntax:
句法:
css=[attribute$=attributeValue]
Value to be added in the By.cssSelector method:
要在By.cssSelector方法中添加的值:
css=input[id$='100']
package com.journaldev.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CSSLocatorsEndswith { public static void main (String [] args){ WebDriver driver = new ChromeDriver(); driver.get("https://www.facebook.com/"); driver.findElement(By.cssSelector("input[id$='100']")).sendKeys("journaldev"); } }
In order to select the element that contains a substring, we use * which means ‘sub-string’.
为了选择包含子字符串的元素,我们使用*表示'sub-string' 。
Syntax:
句法:
css=[attribute*=attributeValue]
Example:
例:
package com.journaldev.selenium; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class CSSLocatorsContains { public static void main (String [] args){ WebDriver driver = new ChromeDriver(); driver.get("https://www.facebook.com/"); driver.findElement(By.cssSelector("input[id*='id']")).sendKeys("journaldev"); } }
We can also use the contains() method.
我们也可以使用contains()方法。
driver.findElement(By.cssSelector("input:contains('id')")).sendKeys("journaldev");
Syntax: parentLocator>childLocator
语法 :parentLocator> childLocator
Example:
例:
For the Sample HTML:
对于示例HTML:
CSS Locator: div#buttonDiv>button
CSS定位器 :div#buttonDiv> button
Description: ‘div#buttonDiv>button’ will first go to div element with id ‘buttonDiv’ and then it will select its child element – ‘button’.
说明 :'div#buttonDiv> button'将首先转到ID为'buttonDiv'的div元素,然后将选择其子元素-'button'。
Syntax: parentLocator>locator1 locator2
语法 :parentLocator> locator1 locator2
CSS Locator: div#buttonDiv button
CSS Locator :div#buttonDiv按钮
Description: ‘div#buttonDiv button’ will go first to div element with id ‘buttonDiv’ and then it will select ‘button’ element inside it (which may be its child or sub child).
描述 :'div#buttonDiv button'将首先转到ID为'buttonDiv'的div元素,然后将在其中选择'button'元素(可以是其子元素或子子元素)。
Example:
例:
For the Sample HTML:
对于示例HTML:
CSS Locator: #testingTypes li:nth-child(2)
CSS Locator :#testingTypes li:nth-child(2)
Description:: ‘#testingTypes li:nth-child(2)’ will select the element with id ‘testingTypes’ and then locate the 2nd child of type li i.e. ‘Security Testing’ list item.
说明:: '#testingTypes li:nth-child(2)'将选择ID为'testingTypes'的元素,然后找到类型为li的第二个子元素,即“ Security Testing”列表项。
Selenium CSS Selector is one of the most frequently used strategies to locate web element because of its simplified syntax.
Selenium CSS Selector是简化Web元素定位的最常用策略之一。
翻译自:
转载地址:http://cjlzd.baihongyu.com/