selenium是一个web的自动化测试工具,可以直接运行在浏览器上,让浏览器自动加载页面,获取需要的数据,甚至页面截屏
无头浏览器:一个完整的浏览器内核,但不包括显示和用户交互页面的浏览器,可通过截屏呈现
# 导入模块
from selenium import webdriver
# 驱动
driver=webdriver.PhantomJS()
# 打开百度
driver.get('https://www.baidu.com/')
# 定位操作输入内容
driver.find_element_by_id('kw').send_keys('python')
# 点击按钮
driver.find_element_by_id('su').click()
# 截屏
driver.save_screenshot('baidu.png')
# 查看当前请求的url地址
print(driver.current_url)
# 查看网页源码
print(driver.page_source)
from selenium import webdriver
import time
driver = webdriver.Chrome()
# 打开百度
driver.get('https://www.baidu.com/')
# 获取cookie
cookie = driver.get_cookies()
print(cookie)
# 窗口最大化
driver.maximize_window()
# 退出当前窗口
time.sleep(3)
driver.close()
# 退出浏览器
time.sleep(1)
driver.quit()
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
根据id来查找某个元素
driver.find_element_by_id('kw').send_keys('python')
driver.find_element(By.ID,'kw').send_keys('python')
根据类名查找元素
driver.find_element_by_class_name('s_ipt').send_keys('python')
driver.find_element(By.CLASS_NAME,'s_ipt').send_keys('python')
根据name属性的值来查找元素
driver.find_element_by_name('wd').send_keys('python')
driver.find_element(By.NAME,'wd').send_keys('python')
根据标签名来查找元素
dr=driver.find_element_by_tag_name('input')
print(dr)
find_element是获取第一个满足条件的元素
dr=driver.find_element(By.TAG_NAME,'input')
print(dr)
find_elements是获取所有满足条件的元素
dr=driver.find_elements(By.TAG_NAME,'input')
print(dr)
根据xpath语法来获取元素
driver.find_element_by_xpath('//input[@id="kw"]').send_keys('python')
driver.find_element(By.XPATH,'//input[@id="kw"]').send_keys('python')
clear()方法可以清除输入框中的内容
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get('https://www.baidu.com/')
m=driver.find_element_by_id('kw')
m.send_keys('python')
time.sleep(2)
m.clear()
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.douban.com/')
切换iframe
login_iframe = driver.find_element_by_xpath('//div[@class="login"]/iframe')
driver.switch_to.frame(login_iframe)
time.sleep(2)
切换登录方式 密码登录
driver.find_element_by_xpath('//li[@class="account-tab-account"]').click()
定位 checkbox 按钮
driver.find_element_by_id('account-form-remember').click()
输入账号和密码
driver.find_element_by_id('username').send_keys('12345')
driver.find_element_by_id('password').send_keys('xxxxx')
点击登录按钮
driver.find_element_by_class_name('btn-account').click()
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver=webdriver.Chrome()
driver.get('https://www.17sucai.com/pins/demo-show?id=5926')
# 切换iframe
driver.switch_to.frame(driver.find_element_by_id('iframe'))
# 定位下拉框
selectTag=Select(driver.find_element_by_class_name('nojs'))
# 根据value值选择
selectTag.select_by_value('AU')
# 根据索引值选择
selectTag.select_by_index(2)
from selenium import webdriver
driver=webdriver.Chrome()
driver.get('https://www.17sucai.com/pins/demo-show?id=5926')
# 切换iframe
driver.switch_to.frame(driver.find_element_by_id('iframe'))
# 定位下拉框
selectTag = driver.find_element_by_id('dk_container_country-nofake').click()
key = int(input('请输入:'))
if key == 1:
driver.find_element_by_xpath('//*[@id="dk_container_country-nofake"]/div/ul/li[1]').click()
elif key == 2:
driver.find_element_by_xpath('//*[@id="dk_container_country-nofake"]/div/ul/li[2]').click()
elif key == 3:
driver.find_element_by_xpath('//*[@id="dk_container_country-nofake"]/div/ul/li[3]').click()
导入模块
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver=webdriver.Chrome()
driver.get('https://www.baidu.com/')
定位到输入框
inputTag=driver.find_element_by_id('kw')
定位到百度一下按钮
button=driver.find_element_by_id('su')
实例化对象
actions=ActionChains(driver)
将鼠标移入到输入框中
actions.move_to_element(inputTag)
输入内容
actions.send_keys_to_element(inputTag,'python')
鼠标移动到百度一下按钮
actions.move_to_element(button)
点击
actions.click()
提交行为链上的操作
actions.perform()