python使用selenium
首先安装
pip install selenium
测试抓取baidu,其中的chromedriver.exe需要自己下载,百度有很多的
import time
from selenium import webdriver browser = 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 Keys driver = 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 start
stdout=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_child
startupinfo)
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 start
os.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实现自动登录163邮箱和Locating Elements介绍
前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...
- [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论
前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...
- [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...
- [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈
我自认为这是自己写过博客中一篇比较优秀的文章,同时也是在深夜凌晨2点满怀着激情和愉悦之心完成的.首先通过这篇文章,你能学到以下几点: 1.可以了解Python简单爬取图片的一些思路和方法 ...
- [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)
转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...
- 【Python】 Selenium 模拟浏览器 寻路
selenium 最开始我碰到SE,是上学期期末,我们那个商务小组做田野调查时发的问卷的事情.当时在问卷星上发了个问卷,但是当时我对另外几个组员的做法颇有微词,又恰好开始学一些软件知识了,就想恶作剧( ...
- Python 配置 selenium 模拟浏览器环境,带下载链接
使用浏览器渲染引擎.直接用浏览器在显示网页时解析HTML,应用CSS样式并执行JavaScript的语句. 这方法在爬虫过程中会打开一个浏览器,加载该网页,自动操作浏览器浏览各个网页,顺便把数据抓下来 ...
- python爬虫---selenium库的用法
python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...
- [python爬虫] Selenium常见元素定位方法和操作的学习介绍
这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...
- 基于python的Selenium使用小结
之前介绍过基于Unittest和TestNG自动化测试框架,然而基于Web端的测试的基础框架是需要Selenium做主要支撑的,这里边给大家介绍下Web测试核心之基于Python的Selenium 一 ...
随机推荐
- MFC添加自定义窗口消息
原文链接: http://www.cnblogs.com/smartvessel/archive/2011/07/18/2109472.html 1. 在头文件stdafx.h中增加一个自定义消息宏 ...
- (面试题)synchronized 和 java.util.concurrent.locks.Lock 的异同
主要相同点: Lock 能完成 synchronized 所实现的所有功能: 主要不同点: Lock 有比 synchronized 更精确的线程语义和更好的性能. synchronized 会自动释 ...
- MySql(十四):MySql架构设计——可扩展性设计之数据切分
一.前言 通过 MySQL Replication 功能所实现的扩展总是会受到数据库大小的限制,一旦数据库过于庞大,尤其是当写入过于频繁,很难由一台主机支撑的时候,我们还是会面临到扩展瓶颈.这时候,我 ...
- springBoot bean注入
1.@Component:把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/> 2.@Autow ...
- MTStatusBarOverlay (状态栏,添加自定义内容库)
NSString * message = [NSString stringWithFormat:@"%@成功", text]; MTStatusBarOverlay *overla ...
- Atitti dbutil获取多个返回结果集的解决
Atitti dbutil获取多个返回结果集的解决 1.1. 多个select默认只返回第一个resultset1 1.2. 调用存储过程,也是返回第一个select的1 1.3. 如果insert前 ...
- modelsim编译Xilinx器件库的另一种方法(节省时间)
以前在用modelsim对Xilinx进行器件库编译时,我用的比较多的是直接在ISE中编译器件库,感觉很方便简单,就是编译时间有点长.自从前段时间,在自己电脑装MathType,360杀毒软件将它视为 ...
- [na]wireshark排查打印机问题
抓包工具排除故障 前言:上网慢,可能是内网堵了.装上wireshark,可抓到广播包,多播包,以及发给自己的包.如果想抓lan内其他人之间的通信包,那就要在sw上做端口镜像. 1.背景 调试打印机的人 ...
- /etc/sudoers 配置
/etc/sudoers ## Allow root to run any commands anywhere root ALL=(ALL) ALL #第一个root是用户账号 第二列的ALL是登陆者 ...
- HTML5学习笔记(十六):原型、类和继承【JS核心知识点】
理解原型 在JavaScript中,只要声明了一个函数,就会为该函数创建一个名为prototype的属性,该属性指向当前函数的原型对象. 而函数的原型对象有一个constructor属性,该属性指向刚 ...