WebDriver介绍
阅读原文时间:2023年07月14日阅读:1

Fetching a Page

driver.get("http://www.google.com")

Locating UI Elements (WebElements)

  1. By ID
...


driver.find_element_by_id("coolestWidgetEvah")

or

from selenium.webdriver.common.by import By
element = driver.find_element(by=By.ID, value="coolestWidgetEvah")

  2. By Class Name:

Cheddar
Gouda


cheeses = driver.find_elements_by_class_name("cheese")

or

from selenium.webdriver.common.by import By
cheeses = driver.find_elements(By.CLASS_NAME, "cheese")

  3. By Tag Name:


frame = driver.find_element_by_tag_name("iframe")

or

from selenium.webdriver.common.by import By
frame = driver.find_element(By.TAG_NAME, "iframe")

  4. By Name:


cheese = driver.find_element_by_name("cheese")

or

from selenium.webdriver.common.by import By
cheese = driver.find_element(By.NAME, "cheese")

  5. By Link Text:

search for cheese>
cheese = driver.find_element_by_partial_link_text("cheese")

or

from selenium.webdriver.common.by import By
cheese = driver.find_element(By.PARTIAL_LINK_TEXT, "cheese")

  6. By CSS:

milkcheese


cheese = driver.find_element_by_css_selector("#food span.dairy.aged")

or

from selenium.webdriver.common.by import By
cheese = driver.find_element(By.CSS_SELECTOR, "#food span.dairy.aged")

  7. By XPath:

Driver Tag and Atttribute Name Attribute Values Native XPath Support
HtmlUnit Driver Lower-cased As they appear in the HTML Yes
Internet Explorer Driver Lower-cased As they appear in the HTML No
Firefox Driver Case insensitive As they appear in the HTML Yes



inputs = driver.find_elements_by_xpath("//input")

or

from selenium.webdriver.common.by import By
inputs = driver.find_elements(By.XPATH, "//input")
Getting text values:
element = driver.find_element_by_id("element_id")
element.text

User Input - Filling In Forms

我们已经看到如何将文本输入到textarea或文本字段中,但其他元素呢?
您可以“切换”复选框的状态,并且可以使用“单击”来设置类似于所选OPTION标记的内容。
处理SELECT标签并不算太糟糕:

select = driver.find_element_by_tag_name("select")
allOptions = select.find_elements_by_tag_name("option")
for option in allOptions:
print "Value is: " + option.get_attribute("value")
option.click()

这将在页面上找到第一个“SELECT”元素,并依次循环选择每个选项,打印出它们的值并依次选择每个选项。

正如你会注意到的,这不是处理SELECT元素的最有效方式。
WebDriver的支持类包括一个名为“Select”的类,它提供了与这些类交互的有用方法。

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_tag_name("select"))
select.deselect_all()
select.select_by_visible_text("Edam")

这将从页面上的第一个SELECT中取消选择所有OPTION,然后选择显示文本“Edam”的OPTION。

完成表格填写后,您可能需要提交。一种方法是找到“提交”按钮并点击它:

driver.find_element_by_id("submit").click()

或者,WebDriver在每个元素上都有“提交”的便利方法。如果你在表单中的元素上调用它,WebDriver会遍历DOM直到找到包含表单,然后调用它。

如果元素不在表单中,NoSuchElementException则会抛出:

element.submit()

Moving Between Windows and Frames(在Windows和框架之间移动)

一些Web应用程序有许多框架或多个窗口。WebDriver支持使用“switchTo”方法在命名窗口之间移动:

driver.switch_to.window("windowName")

driver现在所有的呼叫将被解释为指向特定的窗口。但你怎么知道这个窗口的名字?看看打开它的javascript或链接:

Click here to open a new window

或者,您可以将“窗口句柄”传递给“switchTo()。window()”方法。知道这一点,可以遍历每个打开的窗口,如下所示:

or handle in driver.window_handles:

driver.switch_to.window(handle)

您也可以在一帧之间切换(或者切换到iframe):

driver.switch_to.frame("frameName")

Popup Dialogs(导航:历史和位置)

driver.get("http://www.example.com") # python doesn't have driver.navigate

重申:“ navigate().to()”和“ get()”完全一样。一个比另一个更容易打字!
“导航”界面还提供了在浏览器历史记录中前后移动的功能:

driver.forward()
driver.back()

请注意,此功能完全取决于底层浏览器。

如果您习惯了一种浏览器的行为而另一种浏览器的行为,那么当您调用这些方法时,可能会发生意想不到的情况。

Cookies
# Go to the correct domain

driver.get("http://www.example.com")

# Now set the cookie. Here's one for the entire domain

# the cookie name here is 'key' and its value is 'value'

driver.add_cookie({'name':'key', 'value':'value', 'path':'/'})

# additional keys that can be passed in are:

# 'domain' -> String,
# 'secure' -> Boolean,
# 'expiry' -> Milliseconds since the Epoch it should expire.

# And now output all the available cookies for the current URL

for cookie in driver.get_cookies():
print "%s -> %s" % (cookie['name'], cookie['value'])

# You can delete cookies in 2 ways

# By name

driver.delete_cookie("CookieName")

# Or all of them

driver.delete_all_cookies()

Changing the User Agent(更改用户代理)

使用Firefox驱动程序很容易:

profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "some UA string")
driver = webdriver.Firefox(profile)

Drag And Drop(拖放)

from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")

ActionChains(driver).drag_and_drop(element, target).perform()

Firefox驱动程序

profile = webdriver.FirefoxProfile()
profile.native_events_enabled = True
driver = webdriver.Firefox(profile)

WebDriver:高级用法
显式和隐式等待
1、显式等待
一个显式等待是你定义的一段代码,用于等待某个条件发生然后再继续执行后续代码。显式等待是等元素加载!!!
2、隐式等待
相当于设置全局的等待,在定位元素时,对所有元素设置超时时间。隐式等待是等页面加载,而不是元素加载!!!
(隐式等待就是针对页面的,显式等待是针对元素的。)
显式等待

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

ff = webdriver.Firefox()
ff.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
finally:
ff.quit()

预期条件

from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

隐式等待

from selenium import webdriver

ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")

RemoteWebDriver

截图

from selenium import webdriver

driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX.copy())
driver.get("http://www.google.com")
driver.get_screenshot_as_file('/Screenshots/google.png')

使用FirefoxProfile

from selenium import webdriver
fp = webdriver.FirefoxProfile()

set something on the profile…

driver = webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.FIREFOX, browser_profile=fp)

使用ChromeOptions

from selenium import webdriver
options = webdriver.ChromeOptions()

set some options

driver = webdriver.Remote(desired_capabilities=options.to_capabilities())