[2020-10] v3.13+ selenium 例子,真簡潔,真漂亮
自從 seleniun 官網被限制訪問後,且後面一段時間都用得較少,已經有一段時間未持續關注它的動態。時過幾年,發現它有一些非常好的新特性。下文都以 python 作為開發語言。
with 關鍵字
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
#This example requires Selenium WebDriver 3.13 or newer
with webdriver.Firefox() as driver:
wait = WebDriverWait(driver, 10)
driver.get("https://google.com/ncr")
driver.find_element(By.NAME, "q").send_keys("cheese" + Keys.RETURN)
first_result = wait.until(presence_of_element_located((By.CSS_SELECTOR, "h3>div")))
print(first_result.get_attribute("textContent"))
指定元素,前後左右相對定位
- above
- below
- toLeftOf
- toRightOf
- near
#from selenium.webdriver.support.relative_locator import with_tag_name
passwordField = driver.find_element(By.ID, "password")
emailAddressField = driver.find_element(with_tag_name("input").above(passwordField))
#from selenium.webdriver.support.relative_locator import with_tag_name
emailAddressField = driver.find_element(By.ID, "email")
passwordField = driver.find_element(with_tag_name("input").below(emailAddressField))
#from selenium.webdriver.support.relative_locator import with_tag_name
submitButton = driver.find_element(By.ID, "submit")
cancelButton = driver.find_element(with_tag_name("button").
to_left_of(submitButton))
#from selenium.webdriver.support.relative_locator import with_tag_name
cancelButton = driver.find_element(By.ID, "cancel")
submitButton = driver.find_element(with_tag_name("button").
to_right_of(cancelButton))
near()
Returns the WebElement, which is at most 50px away from the specified element.
#from selenium.webdriver.support.relative_locator import with_tag_name
emailAddressLabel = driver.find_element(By.ID, "lbl-email")
emailAddressField = driver.find_element(with_tag_name("input").
near(emailAddressLabel))
頁面載入策略(Page loading strategy)
normal
This will make Selenium WebDriver to wait for the entire page is loaded. When set to normal, Selenium WebDriver waits until the load event
fire is returned.
By default normal is set to browser if none is provided.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.page_load_strategy = 'normal'
driver = webdriver.Chrome(options=options)
# Navigate to url
driver.get("http://www.google.com")
driver.quit()
eager
This will make Selenium WebDriver to wait until the initial HTML document has been completely loaded and parsed, and discards loading of stylesheets, images and subframes.
When set to eager, Selenium WebDriver waits until DOMContentLoaded event fire
is returned.
none
When set to none Selenium WebDriver only waits until the initial page
is downloaded.
相關文章
- 自制真•簡單部落格
- 一個WPF開發的、介面簡潔漂亮的音訊播放器音訊播放器
- 如何讓你的程式碼整潔漂亮
- 真:隨筆
- 真隨筆
- HTC U12+真機上手圖賞:半透視機身 看起來相當漂亮
- 我真厲害
- JS -真與假-JS
- 真 · 逃避現實
- 鮮花:真實
- Flutter 真非同步Flutter非同步
- Google 真賤格Go
- 面試真經面試
- DBeaver如何快速格式化sql語句,真簡單!SQL
- 前端零基礎入門學習!前端真簡單前端
- 019 Linux tcpdump 抓包案例入門可真簡單啊?LinuxTCP
- 單元測試真
- 孩子特有的純真
- 坤坤真愛粉
- 藍橋杯真題
- Flutter 佈局真經Flutter
- 2016年807真題
- 真實的描寫暴力,讓我們真實地痛恨著暴力
- 史上最真實的VR遊戲,是如何構建“真實”的VR遊戲
- 簡潔Java之道Java
- Selenium - 簡介
- 國產真全面屏手機MIX4亮相:設計真討巧!
- 關於真實和真心
- leetcode-8,真噁心LeetCode
- 安卓真機除錯安卓除錯
- 純真破解工作室
- php的工作真難找PHP
- 從真實世界到渲染
- 真實場景再現
- 真氣APPiOS隱私策略APPiOS
- 真-Node多執行緒執行緒
- JavaScript身份證真偽查驗介面廠家有哪些?身份證查詢真偽JavaScript
- 《真·簡單》Golang 輕量級桌面程式 wails 庫(聖誕節限定)GolangAI