selenium的使用
selenium是自动化测试工具,可以通过代码控制浏览器获取网页数据。
基本使用
1.创建浏览器对象并且加载页面
from selenium
import webdriver
driver
= webdriver
.Chrome
()
driver
.get
('https://www.baidu.com')
2. 基本配置
from selenium
import webdriver
options
= webdriver
.ChromeOptions
()
options
.add_experimental_option
('excludeSwitches', ['enable-automation'])
options
.add_experimental_option
("prefs", {"profile.managed_default_content_settings.images": 2})
driver
= webdriver
.Chrome
(options
=options
)
3. 基本使用
from selenium
import webdriver
brower
= webdriver
.Chrome
()
try:
brower
.get
('https://www.baidu.com')
input = brower
.find_element_by_id
('kw')
input.send_keys
('Python')
input.send_keys
(Keys
.ENTER
)
wait
= WebDriverWait
(brower
, 10)
wait
.until
(EC
.presence_of_element_located
((By
.ID
, 'wrapper')))
print(brower
.current_url
)
print(brower
.get_cookies
())
print(brower
.page_source
)
except:
print('异常!')
brower
.close
()
4. 创建浏览器对象
from selenium
import webdriver
brower
= webdriver
.Chrome
()
brower
= webdriver
.Firefox
()
brower
= webdriver
.Edge
()
brower
= webdriver
.PhantomJS
()
brower
= webdriver
.Safari
()
5. 访问网页
from selenium
import webdriver
brower
= webdriver
.Chrome
()
brower
.get
('https://www.taobao.com')
print(brower
.page_source
)
brower
.close
()
6. 查找单个元素
from selenium
import webdriver
browser
= webdriver
.Chrome
()
browser
.get
('https://www.taobao.com')
input_frist
= browser
.find_element_by_id
('q')
input_second
= browser
.find_element_by_css_selector
('#q')
input_third
= browser
.find_element_by_xpath
('//*[@id="q"]')
print(input_frist
)
print(input_second
)
print(input_third
)
browser
.close
()
7.查找多个元素
from selenium
import webdriver
browser
= webdriver
.Chrome
()
browser
.get
('https://www.taobao.com')
lis
= browser
.find_elements_by_css_selector
('.service-bd li')
print(lis
)
browser
.close
()
8. 元素交互动作 - 对获取的元素调用交互方法
from selenium
import webdriver
import time
browser
= webdriver
.Chrome
()
browser
.get
('https://www.taobao.com')
input = browser
.find_element_by_id
('q')
input.send_keys
('iPhone')
time
.sleep
(1)
input.clear
()
input.send_keys
('iPad')
button
= browser
.find_element_by_class_name
('btn-search')
button
.click
()
9. 交互动作 - 将动作添加到动作链中串行执行
from selenium
import webdriver
from selenium
.webdriver
import ActionChains
browser
= webdriver
.Chrome
()
url
= 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser
.get
(url
)
browser
.switch_to
.frame
('iframeResult')
source
= browser
.find_element_by_css_selector
('#draggable')
target
= browser
.find_element_by_css_selector
('#droppable')
actions
= ActionChains
(browser
)
actions
.drag_and_drop
(source
, target
)
actions
.perform
()
10. 执行JavaScript
from selenium
import webdriver
browser
= webdriver
.Chrome
()
browser
.get
('https://www.zhihu.com/explore')
browser
.execute_script
('window.scrollTo(0, document.body.scrollHeight)')
browser
.execute_script
('alert("To Bottom")')
11. 获取元素信息
from selenium
import webdriver
from selenium
.webdriver
import ActionChains
browser
= webdriver
.Chrome
()
url
= 'https://www.zhihu.com/explore'
browser
.get
(url
)
logo
= browser
.find_element_by_id
('special')
print(logo
)
print(logo
.get_attribute
('class'))
from selenium
import webdriver
browser
= webdriver
.Chrome
()
url
= 'https://www.zhihu.com/explore'
browser
.get
(url
)
input = browser
.find_element_by_class_name
('Button--blue')
print(input.text
)
from selenium
import webdriver
browser
= webdriver
.Chrome
()
url
= 'https://www.zhihu.com/explore'
browser
.get
(url
)
input = browser
.find_element_by_class_name
('Button--blue')
print(input.id)
print(input.location
)
print(input.tag_name
)
print(input.size
)
12. 等待
隐式等待
当使用了隐式等待执行测试的时候,如果webDriver没有在DOM中找到元素,将继续等待,
超出设定时间后则抛出找不到元素的异常,换句话说,当查找元素或元素并没有立即出现的时候,隐式等待
将等待一段时间再查找DOM,默认的时间是0
from selenium
import webdriver
browser
= webdriver
.Chrome
()
browser
.implicitly_wait
(10)
url
= 'https://www.zhihu.com/explore'
browser
.get
(url
)
input = browser
.find_element_by_class_name
('zu-top-add-question')
print(input)
显示等待
等到满足某个条件成立
from 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
browser
= webdriver
.Chrome
()
browser
.get
('https://www.taobao.com/')
wait
= WebDriverWait
(browser
, 10)
input = wait
.until
(EC
.presence_of_element_located
((By
.ID
, 'q')))
button
= wait
.until
(EC
.element_to_be_clickable
((By
.CSS_SELECTOR
, '.btn-search')))
print(input, button
)
10. 前进后退
import time
from selenium
import webdriver
browser
= webdriver
.Chrome
()
browser
.get
('https://www.baidu.com/')
browser
.get
('https://www.taobao.com/')
browser
.get
('https://www.jd.com/')
browser
.back
()
time
.sleep
(1)
browser
.forward
()
browser
.close
()
11. Cookies
from selenium
import webdriver
browser
= webdriver
.Chrome
()
browser
.get
('https://maoyan.com/board/4?offset=0')
print(browser
.get_cookies
())
print()
browser
.add_cookie
({'name': '小明', 'domain': 'www.zhihu,com', 'value': 'germey'})
print(browser
.get_cookies
())
print()
browser
.delete_all_cookies
()
print(browser
.get_cookies
())
browser
.close
()
12. 选项卡
from selenium
import webdriver
import time
browser
= webdriver
.Chrome
()
browser
.get
('https://www.baidu.com')
browser
.execute_script
('window.open()')
print(browser
.window_handles
)
browser
.switch_to
.window
(browser
.window_handles
[1])
browser
.get
('https://www.taobao.com')
time
.sleep
(1)
browser
.switch_to
.window
(browser
.window_handles
[0])
browser
.get
('https://www.baidu.com')