赞
踩
目录
pip install Selenium
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.maximize_window()
- from selenium import webdriver
-
- driver = webdriver.Chrome()
-
- driver.get('https://www.baidu.com')
定位一个元素 | 定位多个元素 | 解释 |
find_element_by_id | find_elements_by_id | 通过元素id定位 |
find_element_by_name | find_elements_by_name | 通过元素name定位 |
find_element_by_xpath | find_elements_by_xpath | 通过xpath表达式定位 |
find_element_by_link_text | find_elements_by_link_tex | 通过完整超链接定位 |
find_element_by_partial_link_text | find_elements_by_partial_link_text | 通过部分链接定位 |
find_element_by_tag_name | find_elements_by_tag_name | 通过标签定位 |
find_element_by_class_name | find_elements_by_class_name | 通过类名进行定位 |
find_elements_by_css_selector | find_elements_by_css_selector | 通过css选择器进行定位 |
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.get('https://www.baidu.com')
- driver.find_element_by_id('kw')
- from selenium import webdriver
- from selenium.webdriver.common.by import By
-
- driver = webdriver.Chrome()
- driver.get('https://www.baidu.com')
- driver.find_element(By.ID, 'kw')
方法 | 解释 |
click() | 点击某个元素 |
send_keys(输入的值) | 模拟输入 |
clear() | 清除操作 |
submit() | 提交表单 |
get_attribute(name) | 获取元素的属性值 |
location | 获取元素的位置 |
text | 获取元素的文本值 |
size | 获取元素的大小 |
id | 获取元素的id值 |
tag_name | 获取元素的标签名 |
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.get('https://www.baidu.com')
- driver.find_element_by_id('kw').send_keys('我是autofelix')
- driver.find_element_by_id('su').click()
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.maximize_window()
-
- driver.get('https://www.baidu.com')
-
- js_sql = '''
- document.getElementById('kw').value = '我是autofelix'
- '''
- driver.execute_script(js_sql)
网页中frame的话,需要执行切入切出操作
switch_to.from(子iframe的id名称) 切入
switch_to.parent_frame(父iframe的id名称) 切出
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.maximize_window()
-
- driver.get('https://www.baidu.com')
-
- //这个网址并没有iframe,我臆测有,你们看看就行
- driver.switch_to.frame('我臆测出来的iframe')
方法 | 说明 |
delete_all_cookies() | 删除当前页面所有cookies |
get_cookie(name) | 获取指定cookie值 |
get_cookies() | 获取当前页面所有cookies值 |
add_cookie() | 设置cookie值 |
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.maximize_window()
-
- driver.get('https://www.baidu.com')
-
- driver.delete_all_cookies()
- driver.add_cookie({'name': 'name', 'domain': '.baidu.com', 'value': 'autofelix'})
方法 | 解释 |
window_handles | 保存所有选项卡的元组 |
switch_to.window() | 切换选项卡 |
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.maximize_window()
-
- driver.get('https://www.baidu.com')
- driver.get('https://www.taobao.com')
-
- driver.switch_to.window(driver.window_handles[0])
- driver.switch_to.window(driver.window_handles[1])
ActionChains模块
方法 | 说明 |
move_to_element(above) | 鼠标右击 |
double_click() | 鼠标双击 |
drag_and_drop() | 左键按住拖动 |
perform() | 动作存储 |
- from selenium import webdriver
- from selenium.webdriver import ActionChains
- from selenium.webdriver.common.by import By
- from selenium.webdriver.support.wait import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
-
- # 初始化谷歌浏览器
- driver = webdriver.Chrome()
-
- # 最大化窗口
- driver.maximize_window()
-
- # 打开头条登陆网址
- driver.get('https://sso.toutiao.com')
-
- # 等待某个元素是否出现
- WebDriverWait(self.driver, 10).until(
- EC.text_to_be_present_in_element((By.XPATH, '//*[@id="mobile-code-get"]/span'), u'发送')
- )
-
- # 实例化鼠标操作
- action = ActionChains(self.driver)
-
- # 按住滑块
- action.click_and_hold(self.driver.find_element_by_xpath('//*[@id="captcha_container"]')).perform()
-
- # 将滑块移动x的距离
- action.move_by_offset(xoffset=x, yoffset=0).perform()
-
- # 释放滑块
- action.release().perform()
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.implicitly_wait(10)
-
- driver.get('https://www.baidu.com')
- 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
-
- driver = webdriver.Chrome()
- driver.implicitly_wait(10)
-
- driver.get('https://www.baidu.com')
- WebDriverWait(driver, 10).until(
- EC.presence_of_element_located((By.ID, 'kw'))
- )
back 后退
forward 前进
refresh 刷新浏览器
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.get('https://www.baidu.com')
- driver.get('https://www.taobao.com')
- driver.get('https://www.jd.com')
-
- driver.back()
- driver.forward()
- driver.refresh()
close 关闭当前标签页
quit 关闭整个浏览器
- from selenium import webdriver
-
- driver = webdriver.Chrome()
- driver.get('https://www.baidu.com')
-
- //打开百度页面后,关闭整个浏览器
- driver.quit()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。