selenium
目录
selenium
1. selenium简介
官方文档:https://selenium-python.readthedocs.io/
# selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 # selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如跳转、输入、点击、下拉等,来拿到网页渲染之后的结果,可支持多种浏览器 from selenium import webdriver browser=webdriver.Chrome() browser=webdriver.Firefox() browser=webdriver.PhantomJS() browser=webdriver.Safari() browser=webdriver.Edge()
2. 安装
2.1 安装selenium
pip3 install selenium
2.2 安装chromedriver
下载chromdriver.exe放到python安装路径的scripts目录中即可,注意最新版本是2.38,并非2.9 国内镜像网站地址:http://npm.taobao.org/mirrors/chromedriver/2.38/ 最新的版本去官网找:https://sites.google.com/a/chromium.org/chromedriver/downloads
2.3 验证安装
C:\Users\Administrator>python3 Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from selenium import webdriver >>> driver=webdriver.Chrome() #弹出浏览器 >>> driver.get('https://www.baidu.com') >>> driver.page_source
注意
selenium3默认支持的webdriver是Firfox,而Firefox需要安装geckodriver
下载链接:https://github.com/mozilla/geckodriver/releases
3. 使用
3.1 基本使用
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 browser = webdriver.Chrome() try: browser.get('https://www.baidu.com') input_tag = browser.find_element_by_id('kw') input_tag.send_keys('美女') # python2中输入中文错误,字符串前加个u input_tag.send_keys(Keys.ENTER) # 输入回车 wait=WebDriverWait(browser,10) wait.until(EC.presence_of_element_located((By.ID, 'content_left'))) # 等到id为content_left的元素加载完毕,最多等10秒 print(browser.page_source) print(browser.current_url) print(browser.get_cookies()) finally: browser.close()
3.2 选择器
3.2.1 基本方法
# 官网链接:http://selenium-python.readthedocs.io/locating-elements.html import time from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 driver = webdriver.Chrome() driver.get('https://www.baidu.com') wait = WebDriverWait(driver, 10) try: #===============所有方法=================== # 1、find_element_by_id # 2、find_element_by_link_text # 3、find_element_by_partial_link_text # 4、find_element_by_tag_name # 5、find_element_by_class_name # 6、find_element_by_name # 7、find_element_by_css_selector # 8、find_element_by_xpath # 强调: # 1、上述均可以改写成find_element(By.ID,'kw')的形式 # 2、find_elements_by_xxx的形式是查找到多个元素,结果为列表 # ===============示范用法=================== # 1、find_element_by_id print(driver.find_element_by_id('kw')) # 2、find_element_by_link_text # login=driver.find_element_by_link_text('登录') # login.click() # 3、find_element_by_partial_link_text login = driver.find_elements_by_partial_link_text('录')[0] login.click() # 4、find_element_by_tag_name print(driver.find_element_by_tag_name('a')) # 5、find_element_by_class_name button = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'tang-pass-footerBarULogin'))) button.click() # 6、find_element_by_name input_user = wait.until(EC.presence_of_element_located((By.NAME,'userName'))) input_pwd = wait.until(EC.presence_of_element_located((By.NAME,'password'))) commit=wait.until(EC.element_to_be_clickable((By.ID,'TANGRAM__PSP_10__submit'))) input_user.send_keys('18611453110') input_pwd.send_keys('xxxxxx') commit.click() # 7、find_element_by_css_selector driver.find_element_by_css_selector('#kw') # 8、find_element_by_xpath time.sleep(5) finally: driver.close()
3.2.3 xpath选择器
# 官网链接:http://selenium-python.readthedocs.io/locating-elements.html import time from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 driver = webdriver.PhantomJS() driver.get('https://doc.scrapy.org/en/latest/_static/selectors-sample1.html') # wait = WebDriverWait(driver, 3) driver.implicitly_wait(3) # 使用隐式等待 try: # find_element_by_xpath # //与/ # driver.find_element_by_xpath('//body/a') # 开头的//代表从整篇文档中寻找,body之后的/代表body的儿子,这一行找不到就会报错了 driver.find_element_by_xpath('//body//a') # 开头的//代表从整篇文档中寻找,body之后的//代表body的子子孙孙 driver.find_element_by_css_selector('body a') # 取第n个 res1 = driver.find_elements_by_xpath('//body//a[1]') # 取第一个a标签 print(res1[0].text) # 按照属性查找,下述三者查找效果一样 res1 = driver.find_element_by_xpath('//a[5]') res2 = driver.find_element_by_xpath('//a[@href="image5.html"]') res3 = driver.find_element_by_xpath('//a[contains(@href,"image5")]') # 模糊查找 print('==>', res1.text) print('==>', res2.text) print('==>', res3.text) # 其他 res1 = driver.find_element_by_xpath('/html/body/div/a') print(res1.text) res2 = driver.find_element_by_xpath('//a[img/@src="image3_thumb.jpg"]') # 找到子标签img的src属性为image3_thumb.jpg的a标签 print(res2.tag_name,res2.text) res3 = driver.find_element_by_xpath("//input[@name='continue'][@type='button']") # 查看属性name为continue且属性type为button的input标签 res4 = driver.find_element_by_xpath("//*[@name='continue'][@type='button']") # 查看属性name为continue且属性type为button的所有标签 time.sleep(5) finally: driver.close()
3.3 获取标签属性
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 browser = webdriver.Chrome() browser.get('https://www.amazon.cn/') wait = WebDriverWait(browser, 10) wait.until(EC.presence_of_element_located((By.ID, 'cc-lm-tcgShowImgContainer'))) tag = browser.find_element(By.CSS_SELECTOR, '#cc-lm-tcgShowImgContainer img') # 获取标签属性, print(tag.get_attribute('src')) # 获取标签ID,位置,名称,大小(了解) print(tag.id) print(tag.location) print(tag.tag_name) print(tag.size) browser.close()
3.4 等待元素加载
selenium只是模拟浏览器的行为,而浏览器解析页面是需要时间的(执行css,js),一些元素可能需要过一段时间才能加载出来,为了保证能查找到元素,必须等待
等待的方式分两种:
隐式等待:在browser.get(‘xxx‘)前就设置,针对所有元素有效
显式等待:在browser.get(‘xxx‘)之后设置,只针对某个元素有效
3.4.1 隐式等待
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 browser = webdriver.Chrome() # 隐式等待:在查找所有元素时,如果尚未被加载,则等10秒 browser.implicitly_wait(10) browser.get('https://www.baidu.com') input_tag = browser.find_element_by_id('kw') input_tag.send_keys('美女') input_tag.send_keys(Keys.ENTER) contents = browser.find_element_by_id('content_left') # 没有等待环节而直接查找,找不到则会报错 print(contents) browser.close()
3.4.2 显式等待
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 browser = webdriver.Chrome() browser.get('https://www.baidu.com') input_tag = browser.find_element_by_id('kw') input_tag.send_keys('美女') input_tag.send_keys(Keys.ENTER) # 显式等待:显式地等待某个元素被加载 wait = WebDriverWait(browser, 10) wait.until(EC.presence_of_element_located((By.ID, 'content_left'))) contents = browser.find_element(By.CSS_SELECTOR, '#content_left') print(contents) browser.close()
3.5 元素交互操作
3.5.1 点击,清空
import time from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 browser = webdriver.Chrome() browser.get('https://www.amazon.cn/') wait = WebDriverWait(browser, 10) input_tag = wait.until(EC.presence_of_element_located((By.ID,'twotabsearchtextbox'))) input_tag.send_keys('iphone 8') button = browser.find_element_by_css_selector('#nav-search > form > div.nav-right > div > input') button.click() time.sleep(3) input_tag = browser.find_element_by_id('twotabsearchtextbox') input_tag.clear() # 清空输入框 input_tag.send_keys('iphone7plus') button = browser.find_element_by_css_selector('#nav-search > form > div.nav-right > div > input') button.click() # browser.close()
3.5.2 Action Chains
import time from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 driver = webdriver.Chrome() driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') wait = WebDriverWait(driver, 3) # driver.implicitly_wait(3) # 使用隐式等待 try: driver.switch_to.frame('iframeResult') # 切换到iframeResult sourse=driver.find_element_by_id('draggable') target=driver.find_element_by_id('droppable') # 方式一:基于同一个动作链串行执行 # actions = ActionChains(driver) #拿到动作链对象 # actions.drag_and_drop(sourse, target) #把动作放到动作链中,准备串行执行 # actions.perform() #方式二:不同的动作链,每次移动的位移都不同 ActionChains(driver).click_and_hold(sourse).perform() distance = target.location['x']-sourse.location['x'] track = 0 while track < distance: ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform() track += 2 ActionChains(driver).release().perform() time.sleep(10) finally: driver.close()
3.5.3 自定义js代码(在交互动作比较难实现的时候)
from selenium import webdriver from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR from selenium.webdriver.common.keys import Keys # 键盘按键操作 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 try: browser=webdriver.Chrome() browser.get('https://www.baidu.com') browser.execute_script('alert("hello world")') # 打印警告 finally: browser.close()
3.6 其他用法
3.6.1 模拟浏览器的前进后退
# 模拟浏览器的前进后退 import time from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.baidu.com') browser.get('https://www.taobao.com') browser.get('http://www.sina.com.cn/') browser.back() time.sleep(10) browser.forward() browser.close()
3.6.2 cookies
#cookies from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.zhihu.com/explore') print(browser.get_cookies()) browser.add_cookie({'k1':'xxx', 'k2':'yyy'}) print(browser.get_cookies()) # browser.delete_all_cookies()
3.6.3 选项卡管理
# 选项卡管理:切换选项卡,有js的方式windows.open,有windows快捷键:ctrl+t等,最通用的就是js的方式 import time from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.baidu.com') browser.execute_script('window.open()') print(browser.window_handles) # 获取所有的选项卡 browser.switch_to_window(browser.window_handles[1]) browser.get('https://www.taobao.com') time.sleep(10) browser.switch_to_window(browser.window_handles[0]) browser.get('https://www.sina.com.cn') browser.close()
3.6.4 异常处理
from selenium import webdriver from selenium.common.exceptions import TimeoutException,NoSuchElementException,NoSuchFrameException try: browser = webdriver.Chrome() browser.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') browser.switch_to.frame('iframssseResult') except TimeoutException as e: print(e) except NoSuchFrameException as e: print(e) finally: browser.close()
相关推荐
xiangxiaojun 2020-09-23
Reiki 2020-08-16
letheashura 2020-08-14
tiankele0 2020-07-29
curiousL 2020-07-18
王练 2020-07-18
tiankele0 2020-07-18
amei0 2020-07-08
Reiki 2020-07-06
Ronnyxie 2020-07-06
xiangxiaojun 2020-07-05
zhanghaibing00 2020-06-28
xiongyouqiang 2020-06-28
Ronnyxie 2020-06-27
amei0 2020-06-26
letheashura 2020-06-26
tiankele0 2020-06-26
王练 2020-06-25