python使用selenium
首先安装
pip install selenium
测试抓取baidu,其中的chromedriver.exe需要自己下载,百度有很多的
import timefrom selenium import webdriverbrowser = webdriver.Chrome('C:\Program Files (x86)\Google\ChromeDriver\chromedriver.exe') # 可选参数,如果不指定将搜索环境变量 browser.get('http://www.baidu.com/')
time.sleep(5)
执行结果如下
进阶
import time from selenium import webdriver from selenium.webdriver.common.keys import Keysdriver = webdriver.Chrome('C:\Program Files (x86)\Google\ChromeDriver\chromedriver.exe') driver.get('https://www.baidu.com/') elem = driver.find_element_by_id('kw') elem.send_keys("test") elem.send_keys(Keys.RETURN) print(driver.page_source)#<input type="text" name="passwd" id="passwd-id" /> element = driver.find_element_by_id("passwd-id") element = driver.find_element_by_name("passwd") element = driver.find_elements_by_tag_name("input") element = driver.find_element_by_xpath("//input[@id='passwd-id']")
如果遇到错误
C:\Users\bin\AppData\Local\Programs\Python\Python36\python.exe C:/Users/bin/PycharmProjects/Test/Test/Crawler/TestSelenium.py Traceback (most recent call last):File "C:\Users\bin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in startstdout=self.log_file, stderr=self.log_file)File "C:\Users\bin\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__restore_signals, start_new_session)File "C:\Users\bin\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 990, in _execute_childstartupinfo) FileNotFoundError: [WinError 2] 系统找不到指定的文件。During handling of the above exception, another exception occurred:Traceback (most recent call last):File "C:/Users/bin/PycharmProjects/Test/Test/Crawler/TestSelenium.py", line 3, in <module>browser = webdriver.Chrome()File "C:\Users\bin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__self.service.start()File "C:\Users\bin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 81, in startos.path.basename(self.path), self.start_error_message) selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
原因是没有安装chrome driver,到 https://sites.google.com/a/chromium.org/chromedriver/downloads 下载,
如果是windows就将chrome driver添加到Path环境变量中,Linux直接放到 usr/bin目录下
python使用selenium相关推荐
- python selenium脚本_怎样开始写第一个基于python的selenium脚本
1.下载并安装python(http://www.python.org/geti/). 2.安装selenium(http://pypi.python.org/pypi/selenium)下载并解压缩 ...
- 使用Python在Selenium WebDriver中获取WebElement的HTML源代码
我正在使用Python绑定来运行Selenium WebDriver. from selenium import webdriver wd = webdriver.Firefox() 我知道我可以抓住 ...
- python批量下载文件-python使用selenium实现批量文件下载
背景 实现需求:批量下载联想某型号的全部驱动程序. 一般在做网络爬虫的时候,都是保存网页信息为主,或者下载单个文件.当涉及到多文件批量下载的时候,由于下载所需时间不定,下载的文件名不定,所以有一定的困 ...
- Python之selenium:selenium库的简介、安装、使用方法之详细攻略
Python之selenium:selenium库的简介.安装.使用方法之详细攻略 目录 selenium库的简介 1.Selenium需要一个驱动程序来与所选的浏览器交互 selenium库的安装 ...
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...
- [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- Python学习--Selenium模块
1. Python学习--Selenium模块介绍(1) 2.Python学习--Selenium模块学习(2) 其他: 1. Python学习--打码平台 转载于:https://www.cnblo ...
- python selenium_Python+selenium自动化测试
前言 Selenium v1.0 的核心组件是 Selenium RC:Selenium v2.0 的核心组件是 WebDriver:因此可以说 v1.0 版本和 v2.0 版本完全是两套东西.而 S ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍(转载)
转载地址:[python爬虫] Selenium常见元素定位方法和操作的学习介绍 一. 定位元素方法 官网地址:http://selenium-python.readthedocs.org/locat ...
最新文章
- 2017 阿里技术-年度精选
- java 定时 spring_Spring定时任务实现与配置(一)
- c语言 随时启停程序 按钮,单片机C语言控制单按钮启停程序(高手请进)
- 体育测试数据绘图软件,原创健身运动体育测试数据统计app界面
- linux文件浏览 ls,linux浏览文件命令
- php mssql_init,Php Mssql操作简单封装支持存储过程
- 用年轻人的压力赚钱,解压馆是门好生意吗?
- cacti的安装和配置
- 【蓝桥杯练习--递归】费解的开关
- 初识机器阅读理解(Machine Reading Comprehension)
- 同步和异步的区别是什么?
- slack 使用说明_我如何使用Slack作为自己的个人助理
- mysql dbi dbd_安装DBI和DBD包
- Linux-Ngrok内网穿透
- 计算机相关专业提升学历的解决方案(硕士研究生)
- 用python爬取外网
- 质量员考试建筑八大员考试给排水工程遵循的原则和质量监管措施
- 李永乐复习全书概率论与数理统计 第一、二章 随机事件和概率、随机变量及其概率分布
- Google,微软等世界级大厂的面试套路,原来如此
- 智慧农业物联网解决方案—农业产业园区信息化建设
热门文章
- js中event,event.srcElement,event.target在IE和firefox下的兼容性
- 新浪微博RSS Feed实现中的问题
- java使用Jsch实现远程操作linux服务器进行文件上传、下载,删除和显示目录信息...
- ERR_CONTENT_LENGTH_MISMATCH
- 【踩坑之路】CentOS 下 LNMN 环境配置
- Ajax (部分二:prototype.js代码)总结页面向后台传Form值、单个值和后台向前台传一个或是一组值...
- MySQL 存储过程初研究
- 设计模式:行为型模式(2)
- Datawindow.net 子数据窗口出错
- C# 按钮控制windows音量