selenium用法详解
模拟浏览器进行网页加载,当requests,urllib无法正常获取网页内容的时候
一、声明浏览器对象
注意点一,Python文件名或者包名不要命名为selenium,会导致无法导入
from selenium import webdriver
#webdriver可以认为是浏览器的驱动器,要驱动浏览器必须用到webdriver,支持多种浏览器,这里以Chrome为例
browser = webdriver.Chrome()
二、访问页面并获取网页html
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.taobao.com') print(browser.page_source)#browser.page_source是获取网页的全部html browser.close() |
三、查找元素
单个元素
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.taobao.com') input_first = browser.find_element_by_id('q') input_second = browser.find_element_by_css_selector('#q') input_third = browser.find_element_by_xpath('//*[@id="q"]') print(input_first,input_second,input_third) browser.close() |
常用的查找方法
find_element_by_name find_element_by_xpath find_element_by_link_text find_element_by_partial_link_text find_element_by_tag_name find_element_by_class_name find_element_by_css_selector |
也可以使用通用的方法
from selenium import webdriver from selenium.webdriver.common.by import By browser = webdriver.Chrome() browser.get('https://www.taobao.com') input_first = browser.find_element(BY.ID,'q')#第一个参数传入名称,第二个传入具体的参数 print(input_first) browser.close() 多个元素,elements多个s input_first = browser.find_elements_by_id('q') |
四、元素交互操作-搜索框传入关键词进行自动搜索
from selenium import webdriver import time browser = webdriver.Chrome() browser.get('https://www.taobao.com') input = browser.find_element_by_id('q')#找到搜索框 input.send_keys('iPhone')#传送入关键词 time.sleep(5) input.clear()#清空搜索框 input.send_keys('男士内裤') button = browser.find_element_by_class_name('btn-search')#找到搜索按钮 button.click() |
五、交互动作,驱动浏览器进行动作,模拟拖拽动作,将动作附加到动作链中串行执行
from selenium import webdriver from selenium.webdriver import ActionChains#引入动作链 browser = webdriver.Chrome() url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) browser.switch_to.frame('iframeResult')#切换到iframeResult框架 source = browser.find_element_by_css_selector('#draggable')#找到被拖拽对象 target = browser.find_element_by_css_selector('#droppable')#找到目标 actions = ActionChains(browser)#声明actions对象 actions.drag_and_drop(source, target) actions.perform()#执行动作 |
六、执行JavaScript
有些动作可能没有提供api,比如进度条下拉,这时,我们可以通过代码执行JavaScript
from selenium import webdriver browser = webdriver.Chrome() browser.get('https://www.zhihu.com/explore') browser.execute_script('window.scrollTo(0, document.body.scrollHeight)') browser.execute_script('alert("To Bottom")') |
七、获取元素信息
获取属性
from selenium import webdriver from selenium.webdriver import ActionChains browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) logo = browser.find_element_by_id('zh-top-link-logo')#获取网站logo print(logo) print(logo.get_attribute('class')) browser.close() 获取文本值 from selenium import webdriver browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) input = browser.find_element_by_class_name('zu-top-add-question') print(input.text)#input.text文本值 browser.close() # 获取Id,位置,标签名,大小 from selenium import webdriver browser = webdriver.Chrome() url = 'https://www.zhihu.com/explore' browser.get(url) input = browser.find_element_by_class_name('zu-top-add-question') print(input.id)#获取id print(input.location)#获取位置 print(input.tag_name)#获取标签名 print(input.size)#获取大小 browser.close() |
八、Frame操作
frame相当于独立的网页,如果在父类网frame查找子类的,则必须切换到子类的frame,子类如果查找父类也需要先切换
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException browser = webdriver.Chrome() url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' browser.get(url) browser.switch_to.frame('iframeResult') source = browser.find_element_by_css_selector('#draggable') print(source) try: logo = browser.find_element_by_class_name('logo') except NoSuchElementException: print('NO LOGO') browser.switch_to.parent_frame() logo = browser.find_element_by_class_name('logo') print(logo) print(logo.text) |
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。