首先安装

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的更多相关文章

  1. [Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍

    前三篇文章介绍了安装过程和通过Selenium实现访问Firefox浏览器并自动搜索"Eastmount"关键字及截图的功能.而这篇文章主要简单介绍如何实现自动登录163邮箱,同时 ...

  2. [Python爬虫] Selenium+Phantomjs动态获取CSDN下载资源信息和评论

    前面几篇文章介绍了Selenium.PhantomJS的基础知识及安装过程,这篇文章是一篇应用.通过Selenium调用Phantomjs获取CSDN下载资源的信息,最重要的是动态获取资源的评论,它是 ...

  3. [Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒

    前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(I ...

  4. [python爬虫] Selenium定向爬取海量精美图片及搜索引擎杂谈

    我自认为这是自己写过博客中一篇比较优秀的文章,同时也是在深夜凌晨2点满怀着激情和愉悦之心完成的.首先通过这篇文章,你能学到以下几点:        1.可以了解Python简单爬取图片的一些思路和方法 ...

  5. [Python爬虫] Selenium爬取新浪微博客户端用户信息、热点话题及评论 (上)

    转载自:http://blog.csdn.net/eastmount/article/details/51231852 一. 文章介绍 源码下载地址:http://download.csdn.net/ ...

  6. 【Python】 Selenium 模拟浏览器 寻路

    selenium 最开始我碰到SE,是上学期期末,我们那个商务小组做田野调查时发的问卷的事情.当时在问卷星上发了个问卷,但是当时我对另外几个组员的做法颇有微词,又恰好开始学一些软件知识了,就想恶作剧( ...

  7. Python 配置 selenium 模拟浏览器环境,带下载链接

    使用浏览器渲染引擎.直接用浏览器在显示网页时解析HTML,应用CSS样式并执行JavaScript的语句. 这方法在爬虫过程中会打开一个浏览器,加载该网页,自动操作浏览器浏览各个网页,顺便把数据抓下来 ...

  8. python爬虫---selenium库的用法

    python爬虫---selenium库的用法 selenium是一个自动化测试工具,支持Firefox,Chrome等众多浏览器 在爬虫中的应用主要是用来解决JS渲染的问题. 1.使用前需要安装这个 ...

  9. [python爬虫] Selenium常见元素定位方法和操作的学习介绍

    这篇文章主要Selenium+Python自动测试或爬虫中的常见定位方法.鼠标操作.键盘操作介绍,希望该篇基础性文章对你有所帮助,如果有错误或不足之处,请海涵~同时CSDN总是屏蔽这篇文章,再加上最近 ...

  10. 基于python的Selenium使用小结

    之前介绍过基于Unittest和TestNG自动化测试框架,然而基于Web端的测试的基础框架是需要Selenium做主要支撑的,这里边给大家介绍下Web测试核心之基于Python的Selenium 一 ...

随机推荐

  1. SharePoint 2013 Designer工作流——Parallel Block的应用

    参考目录 安装和配置SharePoint 2013 Workflow SharePoint 2013 实现多级审批工作流 在自定义Workflow时,往往会遇到这样场景,某个审批需要被多人查阅,每个查 ...

  2. CentOS7安装Tomcat

    一.二进制包安装Tomcat 1.下载解压二进制包 wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.32/bi ...

  3. Unix网络编程 之 基本套接字调用(一)

    Unix/Linux支持伯克利风格的套接字编程,它同一时候支持面向连接和面向无连接类型的套接字. 套接字最经常使用的一些系统调用: socket() bind() connect() listen() ...

  4. spring 下载

    Spring官网(https://spring.io/)改版后,直接下载Jar包的链接,下面汇总 1.直接输入地址,改相应版本即可:http://repo.springsource.org/libs- ...

  5. 常用代码之四:创建jason,jason转换为字符串,字符串转换回jason,c#反序列化jason字符串的几个代码片段

    1.创建jason,并JSON.stringify()将之转换为字符串. 直接使用var customer={}, 然后直接customer.属性就可以直接赋值了. 也可以var customer = ...

  6. SQL Server中关于跟踪(Trace)那点事(转载)

    前言 一提到跟踪俩字,很多人想到警匪片中的场景,同样在我们的SQL Server数据库中“跟踪”也是无处不在的,如果我们利用好了跟踪技巧,就可以针对某些特定的场景做定向分析,找出充足的证据来破案. 简 ...

  7. U8客开插件-一、标准单据标准按钮执行前验证操作

    今天要做的就是在标准的单据的标准按钮之前进行验证操作,如果验证通过执行保存,如果不通过给予提示不进行保存. 下面拿销售出库单的保存按钮进行举例: 第一步:在程序中 ctrl+Shift  点击保存之后 ...

  8. 安装ganglia过程中出现错误 perl(RRDp) is needed by rrdtool-1.2.30-1.el5.rf.x86_64

    用rpm -ivh *.rpm安装ganglia的rpm包,然后出现下面的错误: warning: rrdtool-1.2.30-1.el5.rf.x86_64.rpm: Header V3 DSA/ ...

  9. java打印条形码Code128C

    生成编码类型为Code128C的条形码的javaCODE: package test; import java.awt.Color; import java.awt.Graphics; import ...

  10. Sql Server删除数据表中重复记录 三种方法

    本文介绍了Sql Server数据库中删除数据表中重复记录的方法. [项目]数据库中users表,包含u_name,u_pwd两个字段,其中u_name存在重复项,现在要实现把重复的项删除![分析]1 ...