建立 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.tw/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.tw/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.tw/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