Selenium+PhantomJS

Selenium是一个Web的自动化测试工具

1
pip install selenium

PhantomJS 是一个基于Webkit的“无界面”(headless)浏览器,它会把网站加载到内存并执行页面上的 JavaScript
< PhantomJS 是一个功能完善(虽然无界面)的浏览器而非一个 Python 库,所以它不需要像 Python 的其他库一样安装,但我们可以通过Selenium调用PhantomJS来直接使用 >

1
2
3
4
5
6
7
8
9
10
11
# WebDriver 有点儿像可以加载网站的浏览器
from selenium import webdriver

# 调用环境变量指定的PhantomJS浏览器创建浏览器对象
driver = webdriver.PhantomJS()
# get方法会一直等到页面被完全加载
driver.get(url)
<!----------more----------->
#查看页面快照
driver.save_screenshot("abc.png")
driver.quit()

元素的选取

find_element_by_id()
1
2
3
for example:
# <div id="coolestWidgetEvah">...</div>
element = driver.find_element_by_id("coolestWidgetEvah")
find_elements_by_name()
1
2
3
for example:
# <input name="cheese" type="text"/>
cheese = driver.find_element_by_name("cheese")
find_elements_by_xpath()
1
2
3
4
for example:
# <input type="text" name="example" />
# <INPUT type="text" name="other" />
inputs = driver.find_elements_by_xpath("//input")
1
2
3
for example:
# <a href="http://www.google.com/search?q=cheese">cheese</a>
cheese = driver.find_element_by_link_text("cheese")
1
2
3
for example:
# <a href="http://www.google.com/search?q=cheese">search for cheese</a>>
cheese = driver.find_element_by_partial_link_text("cheese")
find_elements_by_tag_name()
1
2
3
for example:
# <iframe src="..."></iframe>
frame = driver.find_element_by_tag_name("iframe")
find_elements_by_class_name()
1
2
3
4
for example:

<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>
cheeses = driver.find_elements_by_class_name("cheese")
find_elements_by_css_selector()
1
2
3
for example:
# <div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>
cheese = driver.find_element_by_css_selector("#food span.dairy.aged")

鼠标动作链

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
#导入 ActionChains 类
from selenium.webdriver import ActionChains

# 鼠标移动到 ac 位置
ac = driver.find_element_by_xpath('element')
ActionChains(driver).move_to_element(ac).perform()


# 在 ac 位置单击
ac = driver.find_element_by_xpath("elementA")
ActionChains(driver).move_to_element(ac).click(ac).perform()

# 在 ac 位置双击
ac = driver.find_element_by_xpath("elementB")
ActionChains(driver).move_to_element(ac).double_click(ac).perform()

# 在 ac 位置右击
ac = driver.find_element_by_xpath("elementC")
ActionChains(driver).move_to_element(ac).context_click(ac).perform()

# 在 ac 位置左键单击不放
ac = driver.find_element_by_xpath('elementF')
ActionChains(driver).move_to_element(ac).click_and_hold(ac).perform()

# 将 ac1 拖拽到 ac2 位置
ac1 = driver.find_element_by_xpath('elementD')
ac2 = driver.find_element_by_xpath('elementE')
ActionChains(driver).drag_and_drop(ac1, ac2).perform()

下拉框

1
2
3
4
5
6
7
8
9
10
11
12
# 导入 Select 类
from selenium.webdriver.support.ui import Select

# 找到 name 的选项卡
select = Select(driver.find_element_by_name('status'))

#
select.select_by_index(索引)
select.select_by_value(属性值)
select.select_by_visible_text(文本的值)
# 全部取消选择
select.deselect_all()

页面切换

1
2
driver.switch_to.window("this is window name").click()
driver.switch_to_window(driver.window_handles[index])

页面前进和后退

1
2
driver.forward()     #前进
driver.back() # 后退

执行js

1
driver.execute_script(js)

隐式等待

1
2
3
4
from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(10)

显示等待

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
title_is
title_contains
presence_of_element_located
visibility_of_element_located
visibility_of
presence_of_all_elements_located
text_to_be_present_in_element
text_to_be_present_in_element_value
frame_to_be_available_and_switch_to_it
invisibility_of_element_located
element_to_be_clickable – it is Displayed and Enabled.
staleness_of
element_to_be_selected
element_located_to_be_selected
element_selection_state_to_be
element_located_selection_state_to_be
alert_is_present
-------------end-------------
0%