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 一 ...
随机推荐
- C++栈学习——顺序栈和链栈的差别
C++中栈有顺序栈和链栈之分.在顺序栈中,定义了栈的栈底指针(存储空间首地址base).栈顶指针top以及顺序存储空间的大小stacksize(个人感觉这个数据成员是能够不用定义的) //顺序栈数据结 ...
- PyCharm黄色波浪线提示: Simplify chained comparison
译过来就是,可简化连锁比较: case 1 if a >= 0 and a <= 9: 1 可简化为: if 0 <= a <= 9: 1 就像我们的数学表达式一样.显然这种情 ...
- sqlserver使用存储过程发送http请求
本文主要向大家介绍了SQLServer数据库访问发送Http请求,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助. -- 通用读取获取数据存储过程 --开启Sql Serve ...
- Android下Notification,样式style,主题theme的功能实现
一:Notification 1.NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVIC ...
- 深度学习attention 机制了解
Attention是一种用于提升基于RNN(LSTM或GRU)的Encoder + Decoder模型的效果的的机制(Mechanism),一般称为Attention Mechanism.Attent ...
- unity, 删除animationClip中的position曲线
删除clip中所有的position曲线: using UnityEngine; using System.Collections; using UnityEditor; public class r ...
- [na]wireshark排查打印机问题
抓包工具排除故障 前言:上网慢,可能是内网堵了.装上wireshark,可抓到广播包,多播包,以及发给自己的包.如果想抓lan内其他人之间的通信包,那就要在sw上做端口镜像. 1.背景 调试打印机的人 ...
- Python Socket网络编程详解
Socket 简介 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. s ...
- Java 的双重分发与 Visitor 模式
双重分发(Double Dispatch) 什么是双重分发? 谈起面向对象的程序设计时,常说起的面向对象的「多态」,其中关于多态,经常有一个说法是「父类引用指向子类对象」. 这种父类的引用指向子类对象 ...
- do-release-upgrade升级笔记
事后的总结: 后来可能因为阿里云镜像并不是完全底层无关镜像,do-release-upgrade后的18.04版本在经过一次异常的内核版本升级以后,restart失败,因为是虚机还很难处理,不得不直接 ...