Selenium模块
Selenium模块学习
简单配置
# webdriver
https://link.zhihu.com/?target=https%3A//github.com/mozilla/geckodriver/releases
# 测试
from selenium import webdriver
driverPath = r"D:\Software\Tools\github\firefoxDriver\geckodriver.exe"
driver = webdriver.Firefox(executable_path=driverPath) # Firefox浏览器
# driver = webdriver.Firefox("驱动路径")
# 打开网页
driver.get("https://www.baidu.com")
常用属性与方法
# 元素定位
line = driver.find_element(By.XPATH, '//*[@id="username"]')
find_element_by_xpath('//*[@id="username"]')
##在element变成elements就是找所有满足的条件,返回数组
# 控制窗口大小
driver.set_window_size(480, 800)
# 前进后退
driver.forward()
driver.back()
# 刷新
driver.refresh()
# 输入
driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("shivers")
# 清除
driver.find_element(By.XPATH, '//*[@id="username"]').clear
# 单击
driver.find_element(By.XPATH, '//*[@id="login"]').click()
# 提交
driver.find_element(By.XPATH, '//*[@id="username"]').send_keys("shivers").submit()
# 获取元素的文本
driver.text
# 获得属性值
driver.get_attribute(name)
# 每隔1秒(默认0.5秒)检测一次该页面元素是否存在,超过20秒则抛出异常
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
WebDriverWait(driver, 20, 1).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="navbar"]/div/div[3]/div/button'))
)
# 隐式等待 10秒
driver.implicitly_wait(10) # seconds
# 打印当前页面title
title = driver.title
# 打印当前页面URL
now_url = driver.current_url
# 获取结果数目
user = driver.find_element_by_class_name('nums').text
# 导出cookie
cookie_list = driver.get_cookies()
# 导入cookie
for item in cookie_list:
cookie = {
'name': item['name'],
'value': item['value'],
"expires":None,
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False,
}
driver.add_cookie(cookie)
# 删除所有cookie信息
delete_all_cookies()
# 调用js
js="alert(1);"
driver.execute_script(js)
# 警告框
alert = driver.switch_to_alert()
alert.text # 返回 alert/confirm/prompt 文字信息
alert.accept() # 接受现有警告框
alert.dismiss() # 解散现有警告框
alert.send_keys('shivers') # 发送文本至警告框
# 窗口截图
driver.get_screenshot_as_file("D:\\baidu_img.jpg")
# 关闭浏览器
driver.close() # 关闭单个
driver.quit() # 关闭所有
## xpath 基本用法
//form//input[@name="phone"] # 定位特定元素
//button[text()="登录"] # 定位文本
//button[contains(text(),"登录")] # 模糊定位文本
//button[contains(@class,"btn")] # 模糊定位特定元素
//button[starts-with(@class,"btn")] # 定位以特定开头元素
//input[ends-with(@class,"-special")] # 定位以特定结尾元素
//input[@name="phone" and @datatype="m"] # and
//input[@name="phone" or @datatype="m"] # or
//*[@id="username"] # 任意匹配
# 利用节点
ancestor:祖先节点 包括父
parent:父节点
prceding-sibling:当前元素节点标签之前的所有兄弟节点
prceding:当前元素节点标签之前的所有节点
following-sibling:当前元素节点标签之后的所有兄弟节点
following:当前元素节点标签之后的所有节点
//div[text()="akk"]/following-sibling::div//span[text()="shivers"]
# 键盘操作
'''
send_keys(Keys.BACK_SPACE) 删除键
send_keys(Keys.SPACE) 空格键
send_keys(Keys.TAB) 制表键
send_keys(Keys.ESCAPE) 回退键
send_keys(Keys.ENTER) 回车键
send_keys(Keys.CONTROL,'a') 全选
send_keys(Keys.CONTROL,'c') 复制
send_keys(Keys.CONTROL,'x') 剪切
send_keys(Keys.CONTROL,'v') 粘贴
send_keys(Keys.F1) 键盘 F1
send_keys(Keys.F12) 键盘 F12
'''
# 鼠标操作
from selenium.webdriver.common.action_chains import ActionChains
above = driver.find_element_by_link_text("设置")
ActionChains(driver).move_to_element(above).perform()
'''
perform(): 执行所有 ActionChains 中存储的行为;
context_click(): 右击;
double_click(): 双击;
drag_and_drop(): 拖动;
move_to_element(): 鼠标悬停;
'''
# 上传文件
driver.find_element_by_name("file").send_keys('D:\\upload_file.txt')
简单示例
# educode 签到脚本
from selenium import webdriver
url = 'https://www.educoder.net/'
url2 = 'https://www.educoder.net/users/pqrh7ti9y/classrooms'
username = ''
password = ''
def login(driver):
driver.get(url)
xpa1 = '//span[text()="登录"]'
xpa2 = '//input[@id="login"]'
xpa3 = '//input[@id="password"]'
xpa4 = '//button/span[text()="登 录"]'
xpa5 = '//a[text()="我的个人主页"]'
driver.find_element_by_xpath(xpa1).click()
driver.implicitly_wait(2)
driver.find_element_by_xpath(xpa2).send_keys(username)
driver.find_element_by_xpath(xpa3).send_keys(password)
driver.find_element_by_xpath(xpa4).click()
driver.implicitly_wait(2)
cookies = driver.get_cookies()
return cookies
def qiandao(driver, cookies):
for item in cookies:
cookie = {
'name': item['name'],
'value': item['value'],
"expires":None,
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False,
}
driver.add_cookie(cookie)
xpa1 = '//span/a[text()="签到"]'
driver.get(url2)
driver.find_element_by_xpath(xpa1).click()
def main():
driverPath = r"D:\Software\Tools\github\firefoxDriver\geckodriver.exe"
driver = webdriver.Firefox(executable_path=driverPath)
cookies = login(driver)
qiandao(driver, cookies)
if __name__ == '__main__':
main()
参考https://zhuanlan.zhihu.com/p/111859925