创建 CSS 表达式的一些规则是什么?
创建 CSS 表达式有一些规则。CSS 是 Selenium 中重要的定位器之一。借助 id、类名以及标签名和 HTML 属性的组合等属性,可以开发自定义 CSS。
创建 CSS 的方法如下所示:
使用类名 HTML 属性。
这将选择由 (.)classname 表示的特定类的 Web 元素。
语法:driver. find_element_by_css_selector(".name")
这里 name 是属性 class 的值。
- 使用 id HTML 属性。
这将选择由 (#) id 表示的特定 id 的 Web 元素。
语法:driver. find_element_by_css_selector("#search")
这里 search 是属性 id 的值。
使用标签名和属性值的组合。
这将选择特定属性值组合的 Web 元素。
这由 tagname[attribute='value'] 表示。
语法:driver. find_element_by_css_selector ("input[id='result']")
示例
使用 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://tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of class name attribute
driver. find_element_by_css_selector(".gsc-input").
send_keys("Selenium")
#to close the browser
driver.close()示例
使用 css 中的 id 属性的代码实现。
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://tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of id attribute
driver. find_element_by_css_selector("#gsc-i-id1").
send_keys("Selenium")
#to close the browser
driver.close()示例
使用 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://tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of tagname and attribute value
driver. find_element_by_css_selector("input[name='search']").
send_keys("Selenium")
#to close the browser
driver.close()
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP