这篇文章主要介绍基于Python的Selenium的WebDriver组件的使用。
1. 安装Python
点击这里下载安装包
2. 安装Selenium
安装好Python后,可以使用管道安装Selenium。1python -m pip install Selenium
3. 安装WebDriver
可以使用下面的示例代码去验证Driver是否安装成功。1
2
3
4
5
6
7
8
9
10
11
12from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox() # 根据使用的Driver去调用,如Chrome()、Edge()、Safari()。
driver.get("https://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()执行脚本一定要在命令行中去执行,不然会提示找不到Driver。
开始学习
初次使用1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76# Navigating
driver.get("https://www.google.com")
# Interactiong with the page
# with html node “”
element = driver.find_element_by_id("passwd-id")
element = driver.find_element_by_name("passwd")
element = driver.find_element_by_xpath("//input[@id='passwd-id']")
element.send_keys("some text") # 在选中文本域中输入文本。
element.send_keys(" and some", Keys.ARROW_DOWN) # 输入文本并按下鼠标。
element.clear() # 清楚选中文本域的内容。
# Filling in forms
element = driver.find_element_by_xpath("//select[@name='name']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print("Value is: %s" % option.get_attribute("value"))
option.click()
from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('name'))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)
select = Select(driver.find_element_by_id('id'))
select.deselect_all()
select = Select(driver.find_element_by_xpath("xpath"))
all_selected_options = select.all_selected_options
options = select.options
# Assume the button has the ID "submit" :)
driver.find_element_by_id("submit").click()
element.submit()
# Drag and drop
element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")
from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)
action_chains.drag_and_drop(element, target).perform()
# Moving between windows and frames
driver.switch_to_window("windowName")
for handle in driver.window_handles:
driver.switch_to_window(handle)
driver.switch_to_frame("frameName")
driver.switch_to_frame("frameName.0.child")
driver.switch_to_default_content()
# Popup dialogs
alert = driver.switch_to_alert()
# Navigation: history and location
driver.get("http://www.example.com")
driver.forward()
driver.back()
# Cookies
# Go to the correct domain
driver.get("http://www.example.com")
# Now set the cookie. This one's valid for the entire domai
cookie = {'name':'foo', 'value':'bar'}
driver.add_cookie(cookie)
# And now output all the available cookies for the current URL
driver.get_cookies()
进一步使用
定位页面元素
一种方式是通过特定元素的API去查询。(Public Methods)find_element_by_id # id号是唯一的,返回的元素要么为空,要么只有一个。
find_element_by_name # 存在多个。
find_element_by_xpath # 存在多个。
find_element_by_link_text # 存在多个。
find_element_by_partial_link_text # 存在多个。
find_element_by_tag_name # 存在多个。
find_element_by_class_name # 存在多个。
find_element_by_css_selector # 存在多个。当未查询到相应元素时,会抛出异常 NoSuchElementException
一种方式是通过By去查询。(Private Methods)1
2
3
4from selenium.webdriver.common.by import By
driver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')
By的属性包含:1
2
3
4
5
6
7
8ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
id和name不做过多说明。
XPath
以下面这个html页面为例,讲述XPath的使用方式。1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10login_form = driver.find_element_by_xpath("/html/body/form[1]") # Absolute path
login_form = driver.find_element_by_xpath("//form[1]") # First form element in the HTML
login_form = driver.find_element_by_xpath("//form[@id='loginForm']") # The form element with attribute named id and the value loginForm
username = driver.find_element_by_xpath("//form[[email protected]='username']")
username = driver.find_element_by_xpath("//form[@id='loginForm']/input[1]")
username = driver.find_element_by_xpath("//input[@name='username']")
clear_button = driver.find_element_by_xpath("//input[@name='continue'][@type='button']")
clear_button = driver.find_element_by_xpath("//form[@id='loginForm']/input[4]")
(Partial) Link Text1
2
3
4
5
6
7
Are you sure you want to do this?
1
2continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')
Tag Name1
2
3
4
5
6
Welcome
Site content goes here.
1heading1 = driver.find_element_by_tag_name('h1')
Class Name1
2
3
4
5
Site content goes here.
1content = driver.find_element_by_class_name('content')
CSS Selectors1
2
3
4
5
Site content goes here.
1content = driver.find_element_by_css_selector('p.content')
等待
显式等待(Explicit Waits)1
2
3
4
5
6
7
8
9
10
11
12
13from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "myDynamicElement"))
)
finally:
driver.quit()
隐式等待(Implicit Waits)
参考文档