python之selenium自动化测试初始

根据商品id获取属于哪一个店铺能够获取到,在抓取的时候,使用curl函数库,
学习到了curl能够模拟HTTP请求,而且获得响应,返回html,若是须要获取
指定的元素,则可使用正则表达式,匹配元素,若是不想写的话可使用一些PHP解析dom
插件,好比simple_html_dom。能够取https://github.com/samacs/simple_html_dom pull
可是根据商铺获取商品时候,商品是ajax异步加载过来的,异步的时候若是没登陆,没法抓取。html

可是想法是,模拟登录以后,把HTTP的response响应中的head中的cookie存起来,下次请求的时候把cookie放大HTTP请求头中
想法是使用一个帐号,模拟请求登陆接口时候,使用curl模拟HTTP登陆请求,须要设置HTTP请求head的User-Agent,请求类型content-type:form-encode方式
能够进去,可是下一步须要验证身份,输入手机号获取验证码。不知道怎么整了。java

可是能够摸索的过程当中,了解了web自动化测试。python

①、selenium+Chromedriver+python+Chrome测试git

自动化测试工具:
1、pip是一个Python包管理工具。pip install selenium时报错 pip版本过低。
更新pip版本命令使用:python -m pip install --upgrade pipgithub

安装pip时,须要有两种安装方式:
①、一种在线安装, 就是pip install selenium
②、第二种离线安装,先把pip下载到本地:下载地址:https://pypi.python.org/pypi/pipweb

升级pip

pip.exe install -U pip-9.0.2-py2.py3-none-any.whlajax

安装selenium

pip.exe install selenium-3.11.0-py2.py3-none-any.whl
2、https://npm.taobao.org/mirrors/chromedriver
选择版本的时候要和Chrome浏览器版本一致。安装完须要把Chromedriver放到环境变量中。正则表达式

写一个python脚本(hello.py):
from selenium import webdriverchrome

browser = webdriver.Chrome()
browser.get('http://www.qq.com/')npm

运行python hello.py,这样,会启动Chrome浏览器,自动打开www.qq.com

selenium经过Chromedriver驱动打开Chrome浏览器。这样的测试脚本的客户端必须和浏览器的客户端在一块。
因此说,若是脚本是在Linux上运行,若是Linux没有安装Chrome的话、或者Linux没有安装图形安装界面。这种方法就
没办法实现测试了。这样可使用另外一种方法。安装selenium-server版(这版本的话是个jar包,须要安装jdk)+Chromedriver+Chrome(这样启动selenium服务)
测试脚本中经过连接selenium服务的端口就能够,远程测试了。

②、selenium-server版+Chromedriver+Chrome+python

去selenium官网下载selenium的server版,我在这里下载的是2.46,下载的是个jar包,因此须要jdk启动它。
正好在安装NetBeans的时候也须要安装jdk,因此不用安装了,可是进入cmd输入java --version报错未知的命令。应该是jdk不在
环境变量中,因此把java的加到path环境变量中。

开启selenium服务可使用下面的命令:

java -jar F:\chromedriver_win32\selenium-server-standalone-2.46.0.jar ^
-timeout=20 ^
-browserTimeout=60 ^
-Dwebdriver.chrome.driver=F:\chromedriver_win32\chromedriver.exe

输入这个命令后,会启动selenium服务,命令会输出
jdk版本信息
会监听:http://127.0.0.1:4444/wd/hub

DOS中的清除屏幕命令cls至关于Unix中的clear,在Linux中若是一个命令长了可使用\斜线换行,可是在DOS中这个很差使,须要使用^符号。

学习到了python中注释方法。单行注释,可使用#号,多上注释可使用三个引号,能够是三个单引号也能够是三个双引号。

写一个python脚本(selenium_common.py)代码以下:
from selenium import webdriver

driver = webdriver.Chrome()
executor_url = driver.command_executor._url
session_id = driver.session_id
print(session_id)
print(executor_url)
driver.get("http://www.spiderpy.cn/")

我想每次脚本启动时打开多个回话(即浏览器,感受不太好,在网上搜索到以下代码)
可是我想服用这个session回话的话,怎么使用可使用
写一个python脚本(selenium_common2.py)。

from selenium import webdriver

driver = webdriver.Chrome()
executor_url = driver.command_executor._url
session_id = driver.session_id
driver.get("http://www.spiderpy.cn/")

print(session_id)
print(executor_url)

driver2 = webdriver.Remote(command_executor=executor_url, desired_capabilities={})
driver2.session_id = session_id
print(driver2.current_url)

可是这样会从新打开一个空白的回话。在网上查询是python的Remote类每次实例化的时候,会执行start_session()
因此会从新打开一次空白回话。

看网上是重写了一个Remote类

class ReuseChrome(Remote):

def __init__(self, command_executor, session_id):
    self.r_session_id = session_id
    Remote.__init__(self, command_executor=command_executor, desired_capabilities={})

def start_session(self, capabilities, browser_profile=None):
    """
    重写start_session方法
    """
    if not isinstance(capabilities, dict):
        raise InvalidArgumentException("Capabilities must be a dictionary")
    if browser_profile:
        if "moz:firefoxOptions" in capabilities:
            capabilities["moz:firefoxOptions"]["profile"] = browser_profile.encoded
        else:
            capabilities.update({'firefox_profile': browser_profile.encoded})

    self.capabilities = options.Options().to_capabilities()
    self.session_id = self.r_session_id
    self.w3c = False
相关文章
相关标签/搜索