安装selenium
pip install selenium
下载浏览器驱动
驱动下载完成后放入python根目录,selenium会自动解析
API 大全来喽
八大元素定位
id
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.ID, "id")
name
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.NAME, "name")
class_name
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.CLASS_NAME, "class_name")
link_text
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.LINK_TEXT, "link_text")
partial_link_text
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.PARTIAL_LINK_TEXT, "partial_link_text")
tag_name
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.TAG_NAME, "tag_name")
xpath
xpath相对定位
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.XPATH, "//botton")
xpath绝对定位
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.XPATH, "/html/body/div/button/")
css
from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Edge() driver.get("https://steam-404.github.io/") driver.find_element(By.CSS_SELECTOR, ".ikun")
浏览器常用api
```python
from selenium import webdriver
from selenium.webdriver.common.by import Bydriver = webdriver.Edge()
选择Edge驱动
driver.get("https://steam-404.github.io")
打开指定页面
driver.maximize_window()
浏览器窗口最大化
driver.minimize_window()
浏览器窗口最小化
driver.set_window_size(1920, 1080)
设置浏览器窗口大小
title = driver.title
获取网页标题
url = driver.current_url
获取当前URL
driver.back()
页面后退
driver.forward()
页面前进
driver.refresh()
页面刷新
driver.implicitly_wait(0.5)
隐式等待
message = driver.find_element(By.ID, "id").text
获取元素文本
alert = driver.switch_to.alert
捕获弹窗
print(alert.text)
获取弹窗文本
alert.accept()
点击确定按钮
alert.dismiss()
点击取消按钮
alert.send_keys("xxx")
向弹窗内写入内容
driver.quit()
结束会话,关闭浏览器
- 鼠标键盘事件
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
driver = webdriver.Edge()
driver.get("https://steam-404.github.io/")
ikun = driver.find_element(By.ID, 'id')
# 随便找一个实体元素
ActionChains.click(ikun)
# 鼠标点击
ActionChains.perform(ikun)
# 提交表单
ActionChains.context_click(ikun)
# 鼠标右击
ActionChains.double_click(ikun)
# 鼠标双击
ActionChains.move_to_element(ikun)
# 鼠标悬停
Keys.BACK_SPACE(ikun)
# 回退
Keys.SPACE(ikun)
# 空格
Keys.CONTROL(ikun)
# ctrl
Keys.F1(ikun)
# F1键
Keys.CONTROL('c')
# ctrl和c组合键