美文网首页我爱编程
selenium常用方法总结

selenium常用方法总结

作者: zyznull | 来源:发表于2018-02-08 19:24 被阅读0次

最近在学习爬虫的过程,发现selenium是一个很好用的工具,一些模拟登陆的场景,或者动态生成的网页,在不追求效率的情况下,用selenium + python爬取是十分方便的。
简单来说,selenium做的事就是模拟用户操作访问网页,所以理解起来比较容易,定位元素的方法也比较丰富,只是在网页跳转时尤其是动态加载的网页跳转时容易出现问题,动态加载的网页没办法判断何时加载完成网页,以至于接下来的操作依旧在原网页上进行,现在还未找到好的解决办法。
下面时我自己整理的常用方法

1. 初始化

driver = webdriver.Chrome('')#chromedriver的地址

2. 定位

selenium提供了八种常用的定位方法

  1. find_element_by_id()
  2. find_element_by_name()
  3. find_element_by_class_name()
  4. find_element_by_tag_name()
  5. find_element_by_link_text()
  6. find_element_by_partial_link_text()
    7.find_element_by_xpath()(可以结合chrome自带的copyxpath方法使用,不过有时不准确)
    8.find_element_by_css_selector()

3. 获取属性

text 文本
tag_name
get_attribute('属性名')
ex: driver.find_element_by_id('main').get_attribute('href')
dirver.current_url 获取当前页面的url
title:获取当前页面的title

4.操作对象

  1. click 点击对象
  2. send_keys 在对象上模拟按键输入
  3. clear 清除对象的内容,如果可以的话

相关文章

  • selenium常用方法总结

    最近在学习爬虫的过程,发现selenium是一个很好用的工具,一些模拟登陆的场景,或者动态生成的网页,在不追求效率...

  • selenium踩过的坑

    Python selenium常用方法总结 1. 显示等待和隐式等待 1. 显示等待:WebDriverWait(...

  • Selenium 自动化测试不同类型上传文件实用方法总结

    在使用selenium进行UI自动化时,经常会遇到文件上传的坑。所以本文总结一下selenium常用的文件上传方法...

  • Selenium2常用方法封装

    Selenium2常用方法封装

  • selenium核心技术

    一 selenium 元素定位 二 webdirver的常用属性 三 selenium webdriver方法 四...

  • Selenium的常用方法

    最近整理了一些selenium的常用方法,分享给大家 导入模块: from selenium import web...

  • selenium常用方法

    一、操作页面的方法 二、获取html文件后再进行解析(翻页) 三、多页面切换 四、定位 1、CSS定位

  • selenium常用方法

    只要坚持下去,你就发现幸运一定会发生在别人身上。 上一篇:selenium简介 浏览器操作方法 1、创建对象dri...

  • Selenium常用方法

    启动浏览器 driver =webdriver.Chrome() 打开网页 driver.get(URL) 获取当...

  • Selenium 常用方法

    1.获取当前页面的Url: 方法:current_url 实例:driver.current_url 2.获取元素...

网友评论

    本文标题:selenium常用方法总结

    本文链接:https://www.haomeiwen.com/subject/wbzttftx.html