From: http://cuiqingcai.com/2599.html

Selenium教程:https://www.yiibai.com/selenium
selenium 官方参考文档:https://selenium-python.readthedocs.io/index.html
Selenium Documentation:https://www.seleniumhq.org/docs
Selenium 与 PhantomJS:http://www.cnblogs.com/miqi1992/p/8093958.html
自动化测试工具 Selenium:http://www.51testing.com/zhuanti/selenium.html
Selenium :http://www.ltesting.net/ceshi/open/kygncsgj/selenium
Selenium 中文网:http://www.selenium.org.cn
Selenium发展史:https://www.cnblogs.com/fnng/p/7426928.html
Free Selenium Tutorials:https://www.guru99.com/selenium-tutorial.html
Selenium自动化测试教程:http://www.51zxw.net/list.aspx?cid=615
知乎 - Selenium:https://www.zhihu.com/topic/19574589/top-answers
功能测试工具Selenium IDE:https://jingyan.baidu.com/article/ea24bc39bc48dada62b33139.html
Mechanize 和 Selenium:https://blog.csdn.net/u011974639/article/details/73148949
python爬虫从入门到放弃(八)之 Selenium库的使用:https://www.cnblogs.com/zhaof/p/6953241.html
python爬虫 --- 基于selenium用火狐模拟登陆爬搜索关键词的微博:https://blog.csdn.net/u010454729/article/details/51225388
selenium 模拟 fireFox浏览器,爬取网页信息:https://github.com/wu-yy/myhomeCrawler

前言

前面学习了 PhantomJS 的基本用法,归根结底它是一个没有界面的浏览器,而且运行的是 JavaScript 脚本,然而这就能写爬虫了吗?这又和Python有什么关系?接下来我们介绍的这个工具,统统解决掉你的疑惑。

简介

Selenium 是什么?一句话,自动化测试工具。简单的说就是一个可以用代码操所浏览器的工具。可以通过selenium进行搜索关键字,点击按钮等等操作。它支持各种浏览器,包括 Chrome,Safari,Firefox 等主流界面式浏览器,如果你在这些浏览器里面安装一个 Selenium 的插件,那么便可以方便地实现Web界面的测试。换句话说叫 Selenium 支持这些浏览器驱动。当selenium升级到3.0之后,对不同的浏览器驱动进行了规范。如果想使用selenium驱动不同的浏览器,必须单独下载并设置不同的浏览器驱动(注:部分浏览器驱动地址需要科学上网)。话说回来,PhantomJS 不也是一个浏览器吗,那么 Selenium 支持不?答案是肯定的,这样二者便可以实现无缝对接了。

然后又有什么好消息呢?Selenium支持多种语言开发,比如 Java,C,Ruby等等,有 Python 吗?那是必须的!哦这可真是天大的好消息啊。

嗯,所以呢?安装一下 Python 的 Selenium 库,再安装好 PhantomJS,不就可以实现 Python+Selenium+PhantomJS 的无缝对接了嘛!PhantomJS 用来渲染解析JS,Selenium 用来驱动以及与 Python 的对接,Python 进行后期的处理,完美的三剑客!

有人问,为什么不直接用浏览器而用一个没界面的 PhantomJS 呢?答案是:效率高!

Selenium 2,又名 WebDriver,主要新功能是集成了 Selenium 1.0 以及 WebDriver(WebDriver 曾经是 Selenium 的竞争对手)。也就是说 Selenium 2 是 Selenium 和 WebDriver 两个项目的合并,即 Selenium 2 兼容 Selenium,它既支持 Selenium API 也支持 WebDriver API。selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(Selenium Grid)。Selenium的核心Selenium Core基于JsUnit,完全由JavaScript编写,因此用于任何支持JavaScript的浏览器上。
selenium可以模拟真实浏览器,自动化测试工具,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问题。

更多详情可以查看 Webdriver 的简介。

Webdriver

嗯,通过以上描述,我们应该对 Selenium 有了大概对认识,接下来就让我们开始进入动态爬取的新世界吧。

本文参考内容来自

Selenium官网 SeleniumPython文档

用python写爬虫的时候,主要用的是selenium的Webdriver,我们可以通过下面的方式先看看Selenium.Webdriver支持哪些浏览器。首先导入 webdriver 模块。然后使用help函数

from selenium import webdriver
help(webdriver)

安装

首先安装 Selenium

pip install selenium

或者下载源码

下载源码

然后解压后运行下面的命令进行安装

python setup.py install

安装好了之后我们便开始探索抓取方法了。

快速开始

  

初步体验

声明浏览器对象

上面我们知道了selenium支持很多的浏览器,但是如果想要声明并调用浏览器则需要:

from selenium import webdriver
browser = webdriver.Chrome()
browser = webdriver.Firefox()

这里只写了两个例子,当然了其他的支持的浏览器都可以通过这种方式调用

一个例子感受一下 Selenium,这里用 Chrome 浏览器来测试,方便查看效果,到真正爬取的时候换回 PhantomJS 即可。

from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('http://www.baidu.com/')

运行这段代码,会自动打开浏览器,然后访问百度。

如果程序执行错误,浏览器没有打开,那么应该是没有装 Chrome 浏览器或者 Chrome 驱动没有配置在环境变量里。下载驱动,然后将驱动文件路径配置在环境变量即可。

安装三大浏览器驱动driver
        1. chromedriver 下载地址:http://chromedriver.chromium.org
            chromedriver 镜像下载地址 :http://npm.taobao.org/mirrors/chromedriver
        2. Firefox 的驱动 geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases
        3. IE 的驱动 IEdriver 下载地址:http://www.nuget.org/packages/Selenium.WebDriver.IEDriver

注意:下载解压后,将chromedriver.exe , geckodriver.exe , Iedriver.exe发到Python的安装目录,例如 D:\python 。 然后再将Python的安装目录添加到系统环境变量的Path下面。

爬虫 Selenium Chromium 与 Chromedriver对应版本( 注意是 chromium,不是 Chrome ):
淘宝镜像地址在每个文件夹的 notes.txt 中存有 chromium 和 Chromedriver 的版本对应(一般3个chromium版本对应1个Chromedriver 版本)。

chromium     chromedriver
v64-66 v2.37
v63-65 v2.36
v62-64 v2.35
v61-63 v2.34
v60-62 v2.33

然后打开Python IDLE分别输入以下代码来启动不同的浏览器

启动谷歌浏览器
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')

启动火狐浏览器
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.baidu.com/')

启动IE浏览器
from selenium import webdriver
browser = webdriver.Ie()
browser.get('http://www.baidu.com/')

模拟提交

下面的代码实现了“模拟百度提交搜索”的功能。首先等页面加载完成,然后输入关键字到搜索框文本,最后点击提交。

from selenium import webdriver
from selenium.webdriver.common.keys import Keysdriver = webdriver.Chrome()
driver.get("http://www.baidu.com")
print driver.title
elem = driver.find_element_by_name("wd")    # 百度首页搜索框 name="wd"
elem.send_keys("MM")                        # 输入关键字
elem.send_keys(Keys.RETURN)                 # 模拟 点击 enter键,提交
print driver.page_source                    # 打印 js 渲染后的网页

其中 driver.get 方法会打开请求的URL,WebDriver 会等待页面完全加载完成之后才会返回,即程序会等待页面的所有内容加载完成,JS渲染完毕之后才继续往下执行。注意:如果这里用到了特别多的 Ajax 的话,程序可能不知道是否已经完全加载完毕。

WebDriver 提供了许多寻找网页元素的方法,譬如 find_element_by_* 的方法。例如一个输入框可以通过  find_element_by_name 方法寻找 name 属性来确定。

然后我们输入来文本然后模拟点击了回车,就像我们敲击键盘一样。我们可以利用 Keys 这个类来模拟键盘输入。

注意:获取网页渲染后的源代码。输出 page_source 属性即可。这样,就可以做到网页的动态爬取了。        

                   如果想知道更过代码中driver对象有那些方法,可以通过 dir(driver) 查看

  

测试用例

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keysclass PythonOrgSearch(unittest.TestCase):def setUp(self):self.driver = webdriver.Chrome()def test_search_in_python_org(self):driver = self.driverdriver.get("http://www.python.org")self.assertIn("Python", driver.title)elem = driver.find_element_by_name("q")elem.send_keys("pycon")elem.send_keys(Keys.RETURN)assert "No results found." not in driver.page_sourcedef tearDown(self):self.driver.close()if __name__ == "__main__":unittest.main()

运行程序,同样的功能,我们将其封装为测试标准类的形式。

The test case class is inherited from unittest.TestCase. Inheriting from TestCase class is the way to tell unittest module that this is a test case. The setUp is part of initialization, this method will get called before every test function which you are going to write in this test case class. The test case method should always start with characters test. The tearDown method will get called after every test method. This is a place to do all cleanup actions. You can also call quit method instead of close. The quit will exit the entire browser, whereas close will close a tab, but if it is the only tab opened, by default most browser will exit entirely.

测试用例是继承了 unittest.TestCase 类,继承这个类表明这是一个测试类。setUp方法是初始化的方法,这个方法会在每个测试类中自动调用。每一个测试方法命名都有规范,必须以 test 开头,会自动执行。最后的 tearDown 方法会在每一个测试方法结束之后调用。这相当于最后的析构方法。在这个方法里写的是 close 方法,你还可以写 quit 方法。不过 close 方法相当于关闭了这个 TAB 选项卡,然而 quit 是退出了整个浏览器。当你只开启了一个 TAB 选项卡的时候,关闭的时候也会将整个浏览器关闭。

driver = webdriver.Chrome()
driver.maximize_window()    # 最大化浏览器
driver.implicitly_wait(20)  # 设置隐式时间等待
access_url = 'https://www.baidu.com'
driver.get(access_url)element_login = driver.find_element_by_id('login')  # 网页的登录按钮
element_login.click()  # 点击登录按钮
sleep(2)               # 显式等待 2slinks = driver.find_element_by_tag_name('a')  # 查找所有的 a 标签
link = links[3]  # 提取 第四个 a 标签
link.click()     # 点击 提取 的 a 标签

Selenium常见元素定位方法和操作的学习介绍https://blog.csdn.net/eastmount/article/details/48108259

Selenium切换窗口句柄及调用Chrome浏览器:https://blog.csdn.net/eastmount/article/details/53253278

页面操作

访问页面

from selenium import webdriverbrowser = webdriver.Chrome()browser.get("http://www.baidu.com")
print(browser.page_source)
browser.close()

上述代码运行后,会自动打开Chrome浏览器,并登陆百度打印百度首页的源代码,然后关闭浏览器

查找元素

查找单个元素

from selenium import webdriverbrowser = webdriver.Chrome()browser.get("http://www.taobao.com")
input_first = browser.find_element_by_id("q")
input_second = browser.find_element_by_css_selector("#q")
input_third = browser.find_element_by_xpath('//*[@id="q"]')
print(input_first)
print(input_second)
print(input_third)
browser.close()

这里通过三种不同的方式去获取响应的元素,第一种是通过id的方式,第二个中是CSS选择器,第三种是xpath选择器,结果都是相同的。

查找元素的方法

Selenium提供了8种定位方式:id、name、class name、tag name、link text、partial link text、xpath、css selector。

这8种定位方式在Python selenium中所对应的方法为:

查找  单个元素  的  方法

find_element_by_id()
find_element_by_name()
find_element_by_class_name()
find_element_by_tag_name()
find_element_by_link_text()
find_element_by_partial_link_text()
find_element_by_xpath()
find_element_by_css_selector()

查找  多个元素  的  方法
find_elements_by_name
find_elements_by_id
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector

下面这种方式是比较通用的一种方式:这里需要记住By模块所以需要导入

from selenium.webdriver.common.by import By

from selenium import webdriverfrom selenium.webdriver.common.by import Bybrowser = webdriver.Chrome()browser.get("http://www.taobao.com")
input_first = browser.find_element(By.ID,"q")
print(input_first)
browser.close()

当然这种方法和上述的方式是通用的,browser.find_element(By.ID,"q")这里By.ID中的ID可以替换为其他几个

多个元素查找

其实多个元素和单个元素的区别,举个例子:find_elements,单个元素是find_element,其他使用上没什么区别,通过其中的一个例子演示:

from selenium import webdriverbrowser = webdriver.Chrome()
browser.get("http://www.taobao.com")
lis = browser.find_elements_by_css_selector('.service-bd li')
print(lis)
browser.close()

这样获得就是一个列表

当然上面的方式也是可以通过导入from selenium.webdriver.common.by import By 这种方式实现

lis = browser.find_elements(By.CSS_SELECTOR,'.service-bd li')

页面交互

玩转python selenium鼠标键盘操作(ActionChains):https://www.jb51.net/article/92682.htm
Selenium鼠标与键盘事件常用操作方法示例:https://www.jb51.net/article/145502.htm

仅仅抓取页面没有多大卵用,我们真正要做的是做到和页面交互,比如点击,输入等等。那么前提就是要找到页面中的元素。WebDriver提供了各种方法来寻找元素。例如下面有一个表单输入框。

<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']")

你还可以通过它的文本链接来获取,但是要小心,文本必须完全匹配才可以,所以这并不是一个很好的匹配方式。

而且你在用 xpath 的时候还需要注意的是,如果有多个元素匹配了 xpath,它只会返回第一个匹配的元素。如果没有找到,那么会抛出 NoSuchElementException 的异常。

获取了元素之后,下一步当然就是向文本输入内容了,可以利用下面的方法

element.send_keys("some text")

同样你还可以利用 Keys 这个类来模拟点击某个按键。

element.send_keys("and some", Keys.ARROW_DOWN)

你可以对任何获取到到元素使用 send_keys 方法,就像你在 GMail 里面点击发送键一样。不过这样会导致的结果就是输入的文本不会自动清除。所以输入的文本都会在原来的基础上继续输入。你可以用下面的方法来清除输入文本的内容。

element.clear()

这样输入的文本会被清除。

对于获取的元素调用交互方法

from selenium import webdriverimport timebrowser = webdriver.Chrome()
browser.get("http://www.taobao.com")
input_str = browser.find_element_by_id('q')
input_str.send_keys("ipad")
time.sleep(1)
input_str.clear()
input_str.send_keys("MakBook pro")
button = browser.find_element_by_class_name('btn-search')
button.click()

运行的结果可以看出程序会自动打开Chrome浏览器并打开淘宝输入ipad,然后删除,重新输入MakBook pro,并点击搜索

Selenium所有的api文档:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

交互动作

将动作附加到动作链中串行执行

from selenium import webdriver
from selenium.webdriver import ActionChainsbrowser = webdriver.Chrome()url = "http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable"
browser.get(url)
browser.switch_to.frame('iframeResult')
source = browser.find_element_by_css_selector('#draggable')
target = browser.find_element_by_css_selector('#droppable')actions = ActionChains(browser)
actions.drag_and_drop(source, target)
actions.perform()actions.drag_and_drop_by_offset(source, 400, 0).perform()

更多操作参考:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

执行JavaScript

这是一个非常有用的方法,这里就可以直接调用js方法来实现一些操作,下面的例子是通过登录知乎然后通过js翻到页面底部,并弹框提示

from selenium import webdriver
browser = webdriver.Chrome()
browser.get("http://www.zhihu.com/explore")
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
browser.execute_script('alert("To Bottom")')

获取元素属性

get_attribute('class')

from selenium import webdriverbrowser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
logo = browser.find_element_by_id('zh-top-link-logo')
print(logo)
print(logo.get_attribute('class'))

获取文本值

text

from selenium import webdriverbrowser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.text)

获取ID,位置,标签名

id、location、tag_name、size

from selenium import webdriverbrowser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.id)
print(input.location)
print(input.tag_name)
print(input.size)

填充表单

我们已经知道了怎样向文本框中输入文字,但是其它的表单元素呢?例如下拉选项卡的的处理可以如下

element = driver.find_element_by_xpath("//select[@name='name']")
all_options = element.find_elements_by_tag_name("option")
for option in all_options:print("Value is: %s" % option.get_attribute("value"))option.click()

首先获取了第一个 select 元素,也就是下拉选项卡。然后轮流设置了 select 选项卡中的每一个 option 选项。你可以看到,这并不是一个非常有效的方法

其实 WebDriver 中提供了一个叫 Select 的方法,可以帮助我们完成这些事情。

from selenium.webdriver.support.ui import Select
select = Select(driver.find_element_by_name('name'))
select.select_by_index(index)
select.select_by_visible_text("text")
select.select_by_value(value)

如你所见,它可以根据索引来选择,可以根据值来选择,可以根据文字来选择。是十分方便的。

全部取消选择怎么办呢?很简单

select = Select(driver.find_element_by_id('id'))
select.deselect_all()

这样便可以取消所有的选择。

另外我们还可以通过下面的方法获取所有的已选选项。

select = Select(driver.find_element_by_xpath("xpath"))
all_selected_options = select.all_selected_options

获取所有可选选项是

options = select.options

如果你把表单都填好了,最后肯定要提交表单对吧。怎吗提交呢?很简单

driver.find_element_by_id("submit").click()

这样就相当于模拟点击了 submit 按钮,做到表单提交。

当然你也可以单独提交某个元素

element.submit()

方法,WebDriver 会在表单中寻找它所在的表单,如果发现这个元素并没有被表单所包围,那么程序会抛出 NoSuchElementException 的异常。

元素拖拽

要完成元素的拖拽,首先你需要指定被拖动的元素和拖动目标元素,然后利用 ActionChains 类来实现

element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")from selenium.webdriver import ActionChains
action_chains = ActionChains(driver)
action_chains.drag_and_drop(element, target).perform()
actions.drag_and_drop_by_offset(element, 400, 0).perform()

这样就实现了元素从 source 拖动到 target 的操作

Frame

在很多网页中都是有Frame标签,所以我们爬取数据的时候就涉及到切入到frame中以及切出来的问题,通过下面的例子演示
这里常用的是switch_to.from()和switch_to.parent_frame()

import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementExceptionbrowser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult')
source = browser.find_element_by_css_selector('#draggable')
print(source)
try:logo = browser.find_element_by_class_name('logo')
except NoSuchElementException:print('NO LOGO')
browser.switch_to.parent_frame()
logo = browser.find_element_by_class_name('logo')
print(logo)
print(logo.text)

页面切换 和 打开新窗口

如果在一个页面上点击一个链接之后,并不是在当前页面上打开,而是重新打开一个新页面;这种情况下如何跳转到新的页面上操作?首先,需要了解的是每个窗口都有句柄的,可以理解为浏览器窗口的唯一标识符,根据这个标识符来确定新打开的窗口。打开新页面后,selenium 的 focus 还是在 原来的页面上,所以需要使用 switch_to.window 方法把 焦点(focus) 切换到新页面上

如果是新打开的 iframe 就使用 switch_to_frame('xxx')
如果是新打开的 tab 就使用 switch_to_window('')

一个浏览器肯定会有很多窗口,所以我们肯定要有方法来实现窗口的切换。切换窗口的方法如下

driver.switch_to_window("windowName")

switch_to_window 方法现在已经废弃,鼠标放在这个方法上提示 使用 switch_to.window 代替

另外你可以使用 window_handles 方法来获取每个窗口的操作对象。例如

for handle in driver.window_handles:driver.switch_to_window(handle)

另外切换 frame 的方法如下

driver.switch_to_frame("frameName.0.child")

这样焦点会切换到一个 name 为 child 的 frame 上。

打开新窗口主要使用 JavaScript 实现:

# 新标签页打开这个url
js="window.open("url")"
driver.execute_script(js)
time.sleep(2)

Python+Selenium练习篇之27-多窗口之间切换 :https://blog.csdn.net/u011541946/article/details/70132672
python selenium打开新窗口,多窗口切换:https://blog.csdn.net/DongGeGe214/article/details/52169761
python/selenium/chrome打开新窗口并实现窗口切换: https://blog.csdn.net/zwq912318834/article/details/79206953

selenium之 定位以及切换frame(iframe):https://blog.csdn.net/huilan_same/article/details/52200586

selenium iframe 切换

chrome浏览器打开标签页的快捷键是ctrl+t,那把ctrl+t的按键事件传入即可,很多种实现方式,以下只列出两种:

1:
Actions actionOpenLinkInNewTab = new Actions(driver);
actionOpenLinkInNewTab.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).perform();

2:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");

示例代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox() # 默认的火狐浏览器
for i in range(5):# 这句代码相当于在浏览器窗口下按下ctrl+t打开一个新的标签页driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
time.sleep(10) # 等待所有窗口完全打开,10秒够用了, 如果不打开得不到所有句柄,只能得到部分。
handles = driver.window_handles
print(len(handles))
print(handles)

通常要确保页面加载完成,可以使用 selenium.webdriver.support.ui.WebDriverWait

打开新窗口示例代码:

from selenium import webdriver
# 打开谷歌浏览器
browser = webdriver.Chrome()# 打开窗口
browser.get("https://www.baidu.com/")
# 打开新窗口
newwindow_js = 'window.open("https://www.baidu.com");'  # js
browser.execute_script(newwindow_js)                    # 执行 js 打开新的页面 # 切换到新的窗口
handles = browser.window_handles        # 得到 所有页面  句柄
browser.switch_to_window(handles[-1])   # 切换焦点到 对应页面

选项卡管理

通过执行js命令实现新开选项卡window.open()
不同的选项卡是存在列表里browser.window_handles
通过browser.window_handles[0]就可以操作第一个选项卡

import time
from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')
print(browser.window_handles)
browser.switch_to_window(browser.window_handles[1])
browser.get('https://www.taobao.com')
time.sleep(1)
browser.switch_to_window(browser.window_handles[0])
browser.get('https://python.org')

弹窗处理

当你出发了某个事件之后,页面出现了弹窗提示,那么你怎样来处理这个提示或者获取提示信息呢?

alert = driver.switch_to_alert()

通过上述方法可以获取弹窗对象。

历史记录

那么怎样来操作页面的前进和后退功能呢?(前进 和 后退 针对的是 浏览器浏览的网页 的 历史记录)

driver.forward()
driver.back()

Cookies处理

get_cookies()
delete_all_cookes()
add_cookie()

from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
print(browser.get_cookies())
browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'zhaofan'})
print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies())

为页面添加 Cookies,用法如下

# Go to the correct domain
driver.get("http://www.example.com")# Now set the cookie. This one's valid for the entire domain
cookie = {‘name’ : ‘foo’, ‘value’ : ‘bar’}
driver.add_cookie(cookie)

获取页面 Cookies,用法如下

# Go to the correct domain
driver.get("http://www.example.com")# And now output all the available cookies for the current URL
driver.get_cookies()

以上便是 Cookies 的处理,同样是非常简单的。

元素选取

关于元素的选取,有如下的API。

单个元素选取

find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

多个元素选取

find_elements_by_name
find_elements_by_xpath
find_elements_by_link_text
find_elements_by_partial_link_text
find_elements_by_tag_name
find_elements_by_class_name
find_elements_by_css_selector

另外还可以利用 By 类来确定哪种选择方式

from selenium.webdriver.common.by import Bydriver.find_element(By.XPATH, '//button[text()="Some text"]')
driver.find_elements(By.XPATH, '//button')

By 类的一些属性如下

ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"

更详细的元素选择方法参见官方文档

元素选择

页面等待

Python selenium 三种等待方式详解(必会):https://www.jb51.net/article/92672.htm

这是非常重要的一部分,现在的网页越来越多采用了 Ajax 技术,这样程序便不能确定何时某个元素完全加载出来了。这会让元素定位困难而且会提高产生 ElementNotVisibleException 的概率。所以 Selenium 提供了两种等待方式,一种是隐式等待,一种是显式等待。

隐式等待是 等待特定的时间。显式等待 是 指定某一条件直到这个条件成立时继续执行。当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出找不到元素的异常, 换句话说,当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找 DOM,默认的时间是0

显式等待

首先指定一个等待条件,并且再指定一个最长等待时间,然后在这个时间段内进行判断是否满足等待条件,如果成立就会立即返回,如果不成立,就会一直等待,直到等待你指定的最长等待时间,如果还是不满足,就会抛出异常,如果满足了就会正常返回

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()
driver.get("http://somedomain/url_that_delays_loading")
try:element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
finally:driver.quit()

程序默认会 500ms 调用一次来查看元素是否已经生成,如果本来元素就是存在的,那么会立即返回。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECbrowser = webdriver.Chrome()
browser.get('https://www.taobao.com/')
wait = WebDriverWait(browser, 10)
input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
print(input, button)

上述的例子中的条件:EC.presence_of_element_located()是确认元素是否已经出现了
EC.element_to_be_clickable()是确认元素是否是可点击的

下面是一些内置的等待条件,你可以直接调用这些条件,而不用自己写某些等待条件了。

常用的判断条件:
title_is                    标题是某内容
title_contains          标题包含某内容
presence_of_element_located      元素加载出,传入定位元组,如(By.ID, 'p')
visibility_of_element_located       元素可见,传入定位元组
visibility_of                                 可见,传入元素对象
presence_of_all_elements_located            所有元素加载出
text_to_be_present_in_element                某个元素文本包含某文字
text_to_be_present_in_element_value      某个元素值包含某文字
frame_to_be_available_and_switch_to_it frame      加载并切换
invisibility_of_element_located                              元素不可见
element_to_be_clickable                                       元素可点击
staleness_of                        判断一个元素是否仍在DOM,可判断页面是否已经刷新
element_to_be_selected       元素可选择,传元素对象
element_located_to_be_selected 元素可选择,传入定位元组
element_selection_state_to_be 传入元素对象以及状态,相等返回True,否则返回False
element_located_selection_state_to_be 传入定位元组以及状态,相等返回True,否则返回False
alert_is_present 是否出现Alert

更多操作参考:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

from selenium.webdriver.support import expected_conditions as ECwait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID,'someid')))

隐式等待

隐式等待比较简单,就是简单地设置一个等待时间,单位为秒。到了一定的时间发现元素还没有加载,则继续等待我们指定的时间,如果超过了我们指定的时间还没有加载就会抛出异常,如果没有需要等待的时候就已经加载完毕就会立即执行

from selenium import webdriverdriver = webdriver.Chrome()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

当然如果不设置,默认等待时间为0。

from selenium import webdriverbrowser = webdriver.Chrome()
browser.implicitly_wait(10)
browser.get('https://www.zhihu.com/explore')
input = browser.find_element_by_class_name('zu-top-add-question')
print(input)

浏览器的前进和后退

back()
forward()

import time
from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('https://www.baidu.com/')
browser.get('https://www.taobao.com/')
browser.get('https://www.python.org/')
browser.back()
time.sleep(1)
browser.forward()
browser.close()

异常处理

这里的异常比较复杂,官网的参考地址:
http://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions
这里只进行简单的演示,查找一个不存在的元素

from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementExceptionbrowser = webdriver.Chrome()
try:browser.get('https://www.baidu.com')
except TimeoutException:print('Time Out')
try:browser.find_element_by_id('hello')
except NoSuchElementException:print('No Element')
finally:browser.close()

程序框架

对于页面测试和分析,官方提供了一个比较明晰的代码结构,可以参考。

页面测试架构

API

到最后,肯定是放松最全最重要的API了,比较多,希望大家可以多加练习。

API

Selenium 用法详解

来源:https://www.cnblogs.com/themost/p/6900852.html

selenium用法详解

selenium主要是用来做自动化测试,支持多种浏览器,爬虫中主要用来解决JavaScript渲染问题。模拟浏览器进行网页加载,当requests,urllib无法正常获取网页内容的时候

一、声明浏览器对象

注意点一,Python文件名或者包名不要命名为selenium,会导致无法导入

from selenium import webdriver
#webdriver可以认为是浏览器的驱动器,要驱动浏览器必须用到webdriver,支持多种浏览器,这里以Chrome为例
browser = webdriver.Chrome()

二、访问页面并获取网页html

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
print(browser.page_source)#browser.page_source是获取网页的全部html
browser.close()

三、查找元素

# 单个元素 常用的查找方法
        find_element_by_name
        find_element_by_xpath
        find_element_by_link_text
        find_element_by_partial_link_text
        find_element_by_tag_name
        find_element_by_class_name
        find_element_by_css_selector
    # 多个元素,elements多个s
        input_first = browser.find_elements_by_id('q')

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_first = browser.find_element_by_id('q')
input_second = browser.find_element_by_css_selector('#q')
input_third = browser.find_element_by_xpath('//*[@id="q"]')
print(input_first,input_second,input_third)
browser.close()# 也可以使用通用的方法
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input_first = browser.find_element(BY.ID,'q')#第一个参数传入名称,第二个传入具体的参数
print(input_first)
browser.close()

四、元素交互操作-搜索框传入关键词进行自动搜索

from selenium import webdriver
import time
browser = webdriver.Chrome()
browser.get('https://www.taobao.com')
input = browser.find_element_by_id('q')  # 找到搜索框
input.send_keys('iPhone')                # 传送入关键词
time.sleep(5)
input.clear()       # 清空搜索框
input.send_keys('MM')
button = browser.find_element_by_class_name('btn-search') # 找到搜索按钮
button.click()

更多操作: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.remote.webelement#可以有属性、截图等等

五、交互动作,驱动浏览器进行动作,模拟拖拽动作,将动作附加到动作链中串行执行

from selenium import webdriver
from selenium.webdriver import ActionChains#引入动作链browser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult')#切换到iframeResult框架
source = browser.find_element_by_css_selector('#draggable')#找到被拖拽对象
target = browser.find_element_by_css_selector('#droppable')#找到目标
actions = ActionChains(browser)#声明actions对象
actions.drag_and_drop(source, target)
actions.perform()#执行动作

更多操作: http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

六、执行JavaScript

有些动作可能没有提供api,比如进度条下拉,这时,我们可以通过代码执行JavaScript

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
browser.execute_script('window.scrollTo(0, document.body.scrollHeight)')
browser.execute_script('alert("To Bottom")')

七、获取元素信息

# 获取属性
from selenium import webdriver
from selenium.webdriver import ActionChainsbrowser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
logo = browser.find_element_by_id('zh-top-link-logo')#获取网站logo
print(logo)
print(logo.get_attribute('class'))
browser.close()# 获取文本值
from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.text)#input.text文本值
browser.close()# 获取Id,位置,标签名,大小
from selenium import webdriver
browser = webdriver.Chrome()
url = 'https://www.zhihu.com/explore'
browser.get(url)
input = browser.find_element_by_class_name('zu-top-add-question')
print(input.id)       # 获取id
print(input.location) # 获取位置
print(input.tag_name) # 获取标签名
print(input.size)     # 获取大小
browser.close()

八、Frame操作(即 一个html网页中 嵌套 另一个html网页)

frame相当于独立的网页,如果在父类网frame查找子类的,则必须切换到子类的frame,子类如果查找父类也需要先切换

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementExceptionbrowser = webdriver.Chrome()
url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'
browser.get(url)
browser.switch_to.frame('iframeResult')
source = browser.find_element_by_css_selector('#draggable')
print(source)
try:logo = browser.find_element_by_class_name('logo')
except NoSuchElementException:print('NO LOGO')
browser.switch_to.parent_frame()
logo = browser.find_element_by_class_name('logo')
print(logo)
print(logo.text)

九、等待

隐式等待:当使用了隐式等待执行测试的时候,如果 WebDriver没有在 DOM中找到元素,将继续等待,超出设定时间后则抛出找不到元素的异常。换句话说,当查找元素或元素并没有立即出现的时候,隐式等待将等待一段时间再查找 DOM,默认的时间是0

from selenium import webdriverbrowser = webdriver.Chrome()
browser.implicitly_wait(10)#等待十秒加载不出来就会抛出异常,10秒内加载出来正常返回
browser.get('https://www.zhihu.com/explore')
input = browser.find_element_by_class_name('zu-top-add-question')
print(input)

显式等待:指定一个等待条件,和一个最长等待时间,程序会判断在等待时间内条件是否满足,如果满足则返回,如果不满足会继续等待,超过时间就会抛出异常

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ECbrowser = webdriver.Chrome()
browser.get('https://www.taobao.com/')
wait = WebDriverWait(browser, 10)
input = wait.until(EC.presence_of_element_located((By.ID, 'q')))
button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.btn-search')))
print(input, button)

常用属性

title_is             标题是某内容
title_contains       标题包含某内容
presence_of_element_located      元素加载出,传入定位元组,如(By.ID, 'p')
visibility_of_element_located    元素可见,传入定位元组
visibility_of                    可见,传入元素对象
presence_of_all_elements_located 所有元素加载出
text_to_be_present_in_element    某个元素文本包含某文字
text_to_be_present_in_element_value    某个元素值包含某文字
frame_to_be_available_and_switch_to_it frame     加载并切换
invisibility_of_element_located                  元素不可见
element_to_be_clickable                          元素可点击
staleness_of                          判断一个元素是否仍在DOM,可判断页面是否已经刷新
element_to_be_selected                元素可选择,传元素对象
element_located_to_be_selected        元素可选择,传入定位元组
element_selection_state_to_be         传入元素对象以及状态,相等返回True,否则返回False
element_located_selection_state_to_be 传入定位元组以及状态,相等返回True,否则返回False
alert_is_present                      是否出现Alert

详细内容:http://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

十一、前进后退-实现浏览器的前进后退以浏览不同的网页

import time
from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('https://www.baidu.com/')
browser.get('https://www.taobao.com/')
browser.get('https://www.python.org/')
browser.back()
time.sleep(1)
browser.forward()
browser.close()

十二、Cookies

from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('https://www.zhihu.com/explore')
print(browser.get_cookies())
browser.add_cookie({'name': 'name', 'domain': 'www.zhihu.com', 'value': 'germey'})
print(browser.get_cookies())
browser.delete_all_cookies()
print(browser.get_cookies())

选项卡管理 增加浏览器窗口

import time
from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.execute_script('window.open()')
print(browser.window_handles)
browser.switch_to_window(browser.window_handles[1])
browser.get('https://www.taobao.com')
time.sleep(1)
browser.switch_to_window(browser.window_handles[0])
browser.get('http://www.fishc.com')

十三、异常处理

from selenium import webdriverbrowser = webdriver.Chrome()
browser.get('https://www.baidu.com')
browser.find_element_by_id('hello')from selenium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementExceptionbrowser = webdriver.Chrome()
try:browser.get('https://www.baidu.com')
except TimeoutException:print('Time Out')
try:browser.find_element_by_id('hello')
except NoSuchElementException:print('No Element')
finally:browser.close()

# 详细文档:http://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions

Chrome headless

什么是 Headless Chrome

Headless Chrome 是 Chrome 浏览器的无界面形态,可以在不打开浏览器的前提下,使用所有 Chrome 支持的特性运行你的程序。相比于现代浏览器,Headless Chrome 更加方便测试 web 应用,获得网站的截图,做爬虫抓取信息等。相比于较早的 PhantomJS,SlimerJS 等,Headless Chrome 则更加贴近浏览器环境。

Headless Chrome作用

为了提高selenium脚本的执行速度,我们可能会考虑使用PhantomJS这类的Headless 浏览器,但这些工具对JavaScript支持不好或者对web的支持不好,占用资源多,跟真实浏览器存在一定的差异等等问题。Chrome 浏览器提供的Headless Chrome,简单说我们也可以在不打开chrome GUI的情况在Chrome下执行我们的Selenium脚本,可提升脚本的执行效率。

Headless Chrome 对Chrome版本要求

官方文档中介绍,mac 和linux 环境要求 chrome 版本是 59+,而 windows 版本的 chrome 要求是 60+

如果想进一步了解 headless,请移步官网:https://developers.google.cn/web/updates/2017/04/headless-chrome

使用

1. 首先需要下载 Chrome 浏览器(推荐使用 Chrome Canary版本) 。

2. 下载 selenium 驱动 Chrome 的驱动(chromedriver 镜像 :http://npm.taobao.org/mirrors/chromedriver/)

注意 版本问题,可以根据时间下载最新的 Chrome 驱动

3. 添加 Chrome 驱动的环境变量,或者直接把 Chrome 驱动放在 python 的安装目录下

(因为 python 已经设置环境变量,所以可以直接放在 python安装目录下即可)

示例代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import selenium.webdriver.support.ui as ui
# from selenium.webdriver.common.action_chains import ActionChainschrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
browser = webdriver.Chrome(chrome_options=chrome_options)
# 打开浏览器 设定等待加载时间 访问URL
wait = ui.WebDriverWait(browser, 10)def test_1():browser.get('https://www.baidu.com/')print('打开浏览器')print(browser.title)browser.find_element_by_id('kw').send_keys('测试')print('关闭')browser.quit()print('测试完成')def test_2():url = "https://www.newrank.cn/public/info/list.html?period=pgcweek&type=data"browser.get(url)print(browser.page_source)  # 打印渲染后的页面代码def test_3():url = 'http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable'browser.get(url)browser.switch_to.frame('iframeResult')           # 切换到 嵌套的 html 页面source = browser.find_element_by_css_selector('#draggable')print(source)try:logo = browser.find_element_by_class_name('logo')except NoSuchElementException:print('NO LOGO')browser.switch_to.parent_frame()      # 返回到父 html 页面logo = browser.find_element_by_class_name('logo')print(logo)print(logo.text)if __name__ == "__main__":test_1()test_2()test_3()pass

运行结果截图:

使用 示例 :

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Author      :
# @File        : test.py
# @Software    : PyCharm
# @description : XXXimport time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
# from selenium.common.exceptions import
from selenium.webdriver.support.ui import WebDriverWait  # available since
from selenium.webdriver.common.keys import Keysdef is_element_exist(driver, css):find_elements = driver.find_elements_by_css_selector(css_selector=css)if len(find_elements) == 0:print("元素未找到:%s" % css)return Falseelif len(find_elements) == 1:return Trueelse:print("找到%s个元素:%s" % (len(find_elements), css))return Falsedef test():driver = webdriver.Chrome()print("加载驱动完成..")#  设置 隐式等待时间driver.implicitly_wait(10)driver.get("https://ww.baidu.com")  # 加载页面print("加载页面完成..")time.sleep(1)# 方法一try:assert "百度一下" in driver.titleprint('断言 百度 标题 成功')except Exception as e:print('断言 百度 标题 失败', format(e))driver.maximize_window()  # 浏览器全屏显示print("最大化页面窗口完成..")elem = driver.find_element_by_name("wd")  # Find the query boxelem.send_keys("今日头条" + Keys.RETURN)# elem.submit()  提交表单方法print("输入搜索关键字...")time.sleep(1)  # Let the page load, will be added to the API'''# driver.find_element_by_id("kw").clear()# driver.find_element_by_id("kw").send_keys(u"pyse自动化测试")# driver.type("//*[@id='kw']",u"pyse自动化测试")# driver.find_element_by_id("su").send_keys(Keys.ENTER)# driver.click("//*[@id='su']")# 也可定位登陆按钮,通过enter(回车)代替click()# driver.find_element_by_id("su").send_keys(Keys.ENTER)'''# 方法一  采用包含判断,建议第一种try:driver.find_element_by_xpath("//*[@id='su']")print("校验通过,百度一下按钮存在")except NoSuchElementException:assert 0, "校验不通过"# raw_input()  # 停止在当前光标处;# 方法二time.sleep(1)# 验证 今日头条_百度搜索 标题是否存在if "今日头条_百度搜索" == driver.title:print('断言 今日头条 标题 成功')else:print('断言 今日头条 标题 失败')print(driver.title)# raw_input()  # 停止在当前光标处;# 更多验证方法try:assert "今日头条_百度搜索" in driver.titleprint(u"标题验证 Pass")except AssertionError as e:print("找不到这个标题")# 判断页面上有无 id 为 kw 的元素if is_element_exist(driver, "#kw"):driver.find_element_by_id("kw").send_keys("")# 判断页面有无标签为 input元素if is_element_exist(driver, "input"):driver.find_element_by_tag_name("input").send_keys("今日头条 新闻")try:# we have to wait for the page to refresh, the last thing that seems to be updated is the titleWebDriverWait(driver, 10).until(lambda driver: driver.title.lower().startswith(""))# You should see "cheese! - Google Search"print(u"等待时间,打印当前页面的标题 :" + driver.title)finally:print(u"-----> 请按Enter 键进行下一步操作...")input('press any key continue')  # 停止在当前光标处;# driver.close()print(u"执行完成,即将关闭驱动...")driver.close()driver.quit()  # 与close方法相同if __name__ == '__main__':test()

登录百度账号,并在 贴吧发帖:

import time
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver import ActionChains
# from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ECclass LoginBD(object):def __init__(self):super(LoginBD, self).__init__()self.browser = Noneself.wait = Noneself.action_chains = Nonepassdef __del__(self):passdef init_chrome_browser(self):"""初始化 Chrome 浏览器:return:"""chrome_options = webdriver.ChromeOptions()# 启动参数# chrome_options.add_argument('--headless')chrome_options.add_argument('disable-infobars')chrome_options.add_argument('--disable-gpu')chrome_options.add_argument("window-size=1024,768")chrome_options.add_argument("--no-sandbox")# mobile_emulation = {'deviceName': 'iPhone 6'}# chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)# proxy_ip = 'http://127.0.0.1:8080'# chrome_options.add_argument("--proxy-server={0}".format(proxy_ip))########################################################################## http_proxy = "http://127.0.0.1:8080"# # 代理参数# desired_capabilities = options.to_capabilities()# desired_capabilities['acceptSslCerts'] = True# desired_capabilities['acceptInsecureCerts'] = True# desired_capabilities['proxy'] = {#     "httpProxy": http_proxy,#     "ftpProxy": http_proxy,#     "sslProxy": http_proxy,#     "noProxy": None,#     "proxyType": "MANUAL",#     "class": "org.openqa.selenium.Proxy",#     "autodetect": False,# }########################################################################## 启动浏览器self.browser = webdriver.Chrome(options=chrome_options)self.browser.maximize_window()# self.browser = webdriver.Chrome(chrome_options=options, desired_capabilities=desired_capabilities)# 打开浏览器 设定等待加载时间 访问URLself.wait = WebDriverWait(self.browser, 10)# self.wait = ui.WebDriverWait(self.browser, 10)self.action_chains = ActionChains(self.browser)self.browser.delete_all_cookies()passdef login_bd(self):bd_url = 'https://www.baidu.com'self.init_chrome_browser()self.browser.get(bd_url)time.sleep(2)login_href_xpath = '//div[@id="u1"]/a[@name="tj_login"]'self.wait.until(EC.presence_of_all_elements_located((By.XPATH, login_href_xpath)))login_href = self.browser.find_element_by_xpath(login_href_xpath)login_href.click()p_element_xpath = '//p[@class="tang-pass-footerBarULogin pass-link"]'self.wait.until(EC.presence_of_all_elements_located((By.XPATH, p_element_xpath)))p_element = self.browser.find_element_by_xpath(p_element_xpath)p_element.click()input_username = self.browser.find_element_by_id('TANGRAM__PSP_10__userName')username = input('input username :')input_username.send_keys(username)input_password = self.browser.find_element_by_id('TANGRAM__PSP_10__password')password = input('input password :')input_password.send_keys(password)btn = self.browser.find_element_by_id('TANGRAM__PSP_10__submit')btn.click()a_span_element_xpath = '//a[@id="s_username_top"]/span'login_result = self.wait.until(EC.text_to_be_present_in_element((By.XPATH, a_span_element_xpath), username))if login_result:print('login success')else:print('login fail')passdef test(self):bar_url = 'https://tieba.baidu.com/f?ie=utf-8&kw={0}'.format('美女')self.browser.get(bar_url)js = "window.scrollTo(0, document.body.scrollHeight)"self.browser.execute_script(js)tie_tittle_xpath = '//input[@name="title"]'self.wait.until(EC.presence_of_all_elements_located((By.XPATH, tie_tittle_xpath)))tie_element = self.browser.find_element_by_xpath(tie_tittle_xpath)tittle_text = input('input tie tittle :')tie_element.send_keys(tittle_text)tie_content_xpath = '//div[@id="ueditor_replace"]'tie_content = self.browser.find_element_by_xpath(tie_content_xpath)tie_content_text = input('input tie content :')tie_content.send_keys(tie_content_text)btn_commit_xpath = '//button[@class="btn_default btn_middle j_submit poster_submit"]'btn_commit = self.browser.find_element_by_xpath(btn_commit_xpath)btn_commit.click()input('press any key to continue...')passdef main():temp = LoginBD()temp.login_bd()temp.test()if __name__ == "__main__":main()pass

运行截图:

手动登录淘宝后,用程序抓取商品信息。

使用 selenium 登录淘宝时,可以被淘宝检测 出来,所以手动登录。

import time
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutExceptiondef test_selenium():option = ChromeOptions()option.add_experimental_option('excludeSwitches', ['enable-automation'])browser = Chrome(options=option)browser.maximize_window()action_chains = ActionChains(browser)wait = WebDriverWait(browser, 5)username = '淘宝账号'pwd = '淘宝密码'url = 'https://login.taobao.com/member/login.jhtml'browser.get(url)# 鼠标移动,模拟人的行为action_chains.move_by_offset(random.randint(10, 60), random.randint(10, 60)).perform()qr_code_element = browser.find_element_by_xpath('//i[@id="J_Quick2Static"]')qr_code_element.click()user_name_element = browser.find_element_by_xpath('//input[@name="TPL_username"]')for ch in username:user_name_element.send_keys(ch)time.sleep(0.5)password_element = browser.find_element_by_xpath('//input[@name="TPL_password"]')for ch in pwd:password_element.send_keys(ch)time.sleep(0.5)btn_submit = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="submit"]/button[@data-ing]')))btn_submit.click()# action_chains.click(btn_submit)# while True:#     try:#         user_name = wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@class="site-nav-login-info-nick "]')))#         if user_name:#             user_name_info = user_name.text#             print(user_name_info)#             break#     except TimeoutException as te:#         pass#     # action_chains.click(btn_submit)#     slide_bar = browser.find_element_by_xpath('//span[@class="nc_iconfont btn_slide"]')#     if slide_bar:#         print('滑动验证码')##         # tracks = get_track(500)#         action_chains.click_and_hold(slide_bar).perform()#         for x in (50, 100, 200, 400):#             action_chains.move_by_offset(xoffset=x, yoffset=random.randint(1, 10)).perform()#         # for i in range(10):#         #     x = i * 10 + random.randint(1, 50)#         #     action_chains.move_by_offset(xoffset=x, yoffset=random.randint(1, 10)).perform()#         #     time.sleep(0.2)#         # action_chains.release().perform()#         # action_chains.drag_and_drop_by_offset(slide_bar, 400, random.randint(1, 10)).perform()#         time.sleep(random.randint(3, 5))##     user_name_element = browser.find_element_by_xpath('//input[@name="TPL_username"]')#     user_name_element.clear()#     user_name_element.send_keys(username)##     password_element = browser.find_element_by_xpath('//input[@name="TPL_password"]')#     password_element.clear()#     password_element.send_keys(pwd)#     password_element.send_keys(Keys.ENTER)##     btn_submit = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@class="submit"]/button[@data-ing]')))#     btn_submit.click()#     # action_chains.click(btn_submit)input('登录淘宝后按任意键继续......')user_name = wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@class="site-nav-login-info-nick "]')))if user_name:user_name_info = user_name.textprint(user_name_info)# input('按任意键继续......')# browser.get('https://www.taobao.com')browser.get('https://s.taobao.com/search?initiative_id=tbindexz_20170306&ie=utf8&spm=a21bo.2017.201856-taobao-item.2&sourceId=tb.index&search_type=item&ssid=s5-e&commend=all&imgfile=&q=激光灯&suggest=0_2&_input_charset=utf-8&wq=激光&suggest_query=激光&source=suggest')# input('按任意键继续......')produce_info_xpath = '//div[contains(@class, "J_MouserOnverReq")]//div[@class="row row-2 title"]/a'produce_info = browser.find_elements_by_xpath(produce_info_xpath)for produce in produce_info:print(produce.text.replace(' ', ''))if __name__ == '__main__':    test_selenium()

python selenium 用法 和 Chrome headless相关推荐

  1. python headless_Python 爬虫杂记 - Chrome Headless

    Chrome Headless使用 测试 Chrome 版本: 62.0.3202.89(正式版本)(64 位) Python环境:python2.7 注: Headless模式需要59版本及以上! ...

  2. Python+selenium用法 上

    下载chromdriver到: python目录下 chrom目录下 添加chrom路径到path Python selenium 三种等待方式详解 1. 强制等待 time.sleep(10) 2. ...

  3. Python selenium自动化操作Chrome浏览器

    ​ ​ 活动地址:CSDN21天学习挑战赛 文章目录 一.Selenium搭建环境 1.安装 2.安装Chrome安装浏览器驱动WebDriver 1)查看Chrome浏览器版本 2)在驱动器版本里选 ...

  4. Python+Selenium 自动化-指定chrome驱动运行selenium实例演示,运行指定位置下的浏览器驱动

    from selenium import webdriveroptions = webdriver.ChromeOptions() # 指定驱动 driver_path = "D:\pyau ...

  5. python+Selenium自动化操作Chrome模拟手机浏览器

    在使用Chrome浏览网页时,我们可以使用Chrome开发者工具模拟手机浏览器,在使用Selenium操作Chrome时同样也可以模拟手机浏览器.主要有以下两种用途. 测试H5页面在不同分辨率设备上的 ...

  6. 在Docker中使用Python Selenium和Headless Chrome进行网站自动化测试的方法

    by Joyz 通过乔伊斯 在Docker中使用Python Selenium和Headless Chrome进行网站自动化测试的方法 (A recipe for website automated ...

  7. python selenium headless chrome chromedriver 等安装

    python selenium 略 chrome: https://blog.csdn.net/zhuyiquan/article/details/79537623 6及以下版本不适用 6及以下版本不 ...

  8. python chrome headless_[技巧] chrome headless 爬虫抓取websoket 数据

    目录 源起 分析 实践 总结 源起 周末答应了一个朋友帮他看一下一个网站应该怎么爬,费话不说直接先上网站 https://datacenter.jin10.com/price 数据一直在不停的闪,直觉 ...

  9. Python + Selenium + Chrome Driver 自动化点击+评论+刷弹幕(仅供学习)

    Python + Selenium + Chrome Driver 自动化点击 评论 刷弹幕 首先说明,这篇博文仅供学习!仅供学习!仅供学习! 不要拿去做其他事,封号概不负责!!! 突发奇想 首先先说 ...

最新文章

  1. 新乡职业学院对口计算机分数线,新乡职业技术学院2020年录取分数线(附2017-2020年分数线)...
  2. VMDNS服务器未响应,vmware克隆虚拟机后进行网络配置
  3. C#中数据流(文件流、内存流、网络流等)相关知识点梳理
  4. Python中文编码判别及转换
  5. 简单DNS服务器架设
  6. In addition, Microsoft is also developing
  7. glibc版本查看_[译] 写一个简单的内存分配器(替换glibc中的malloc函数)
  8. Word 2013 使用技巧
  9. Hadoop HDFS原理
  10. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)
  11. python自动生成字幕_语音自动转文字和自动生成字幕
  12. php开发中处理emoji表情和颜文字的兼容问题
  13. Basic Concepts in Graphics and Architecture
  14. 10个必须掌握的SQL常用语句
  15. Excel VBA-正则表达式汇总
  16. 详解unity中tranform.worldToLocalMatrix
  17. 股票短线详解,股票短线的方法技巧?
  18. ibdata1导致mysql打不开_关于mysql:mysql文件ibdata1增长过大导致服务器无法写的问题...
  19. 所见即所得:8款实用HTML5开发框架
  20. 销售PK工程师~ 佛与道~ 阴VS阳

热门文章

  1. 机器学习相关从业者如何兼顾理论与工程能力
  2. 肖仰华 | 知识图谱与认知智能
  3. (Android开发辅助工具)动态广播注册解注工具
  4. 秒懂系列 | 史上最简单的Python Django入门教程
  5. promise ,async 小记
  6. Unix下5种I/O模型
  7. hdu - 2667 Proving Equivalences(强连通)
  8. 正确的 zip 压缩与解压代码
  9. java中Scanner类的使用
  10. Leetcode--738. 单调递增的数字