【python爬虫】selenium基本使用代码示例

Edge Driver下载地址:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ 一般操作

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
# 从selenuim中导入webdriver
from selenium import webdriver
from time import sleep
# 特殊按键操作
from selenium.webdriver.common.keys import Keys

if __name__ == "__main__":
browser = webdriver.Edge(executable_path='../../msedgedriver.exe')
# 网址输入操作
browser.get("https://xgpax.top/")
# 数据选择操作
title = browser.find_element_by_class_name('banner-title-inner')
sleep(3)
print(title.get_attribute("class"), title.text)
# 执行js脚本
browser.execute_script('window.scrollTo(0,document.body.clientHeight)')
# 通过xpath查找
search = browser.find_element_by_xpath('//*[@id="leftbar_search_input"]')
# 点击事件触发
search.click()
# 键盘输入
search.send_keys("转存")
# 输入特殊键 enter
search.send_keys(Keys.ENTER)
sleep(5)
# 浏览器退出操作
browser.quit()

iframe和windows操作

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
from selenium import webdriver
from time import sleep

if __name__ == "__main__":
brow = webdriver.Edge("../../msedgedriver.exe")
brow.get("http://www.yhdongman.com/show/28217-31716-0.html")
sleep(2)
search = brow.find_element_by_css_selector("#bdcs-search-form-input")
search.send_keys("shabi")
# iframe操作 切换浏览器作用域
# 获取iframe
iframe = brow.find_element_by_xpath('//*[@id="dm456_player"]/iframe')
# 切换到iframe
brow.switch_to.frame(iframe)
# 进行操作
iframe_img = brow.find_element_by_xpath('//*[@id="player"]')
iframe_img.click()
# 返回默认文档
brow.switch_to.default_content()
# 窗口操作
# 点击打开新窗口
brow.find_element_by_xpath('/html/body/div[7]/div/a[1]').click()
# 切换到新窗口
brow.switch_to.window(brow.window_handles[1])
# 关闭新窗口
brow.close()
# 切换回来
brow.switch_to.window(brow.window_handles[0])
sleep(2)
brow.get("https://taobao.com")
sleep(3)
brow.get("https://baidu.com")
sleep(5)
brow.quit()

动作链操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from selenium import webdriver
from time import sleep
# 导入动作链
from selenium.webdriver import ActionChains

if __name__ == "__main__":
edge = webdriver.Edge(executable_path="../../msedgedriver.exe")
edge.get("https://xgpax.top/")
edge.fullscreen_window()
sleep(5)
a = edge.find_element_by_xpath('//*[@id="post-340"]/header/div/a')
# 实例化 动作链对象 传入浏览器驱动对象
action = ActionChains(edge)
# 长按 传入目标对象
action.click_and_hold(a)
# 发起动作
for i in range(100):
action.move_by_offset(0, 1)
sleep(0.1)
# 放开动作链
action.release()