python编程使用selenium模拟登陆淘宝实例代码

yipeiwu_com5年前Python基础

selenium简介

selenium 是一个web的自动化测试工具,不少学习功能自动化的同学开始首选selenium ,相因为它相比QTP有诸多有点:

  • * 免费,也不用再为破解QTP而大伤脑筋
  • * 小巧,对于不同的语言它只是一个包而已,而QTP需要下载安装1个多G 的程序。
  • * 这也是最重要的一点,不管你以前更熟悉C、 java、ruby、python、或都是C# ,你都可以通过selenium完成自动化测试,而QTP只支持VBS
  • * 支持多平台:windows、linux、MAC ,支持多浏览器:ie、ff、safari、opera、chrome
  • * 支持分布式测试用例的执行,可以把测试用例分布到不同的测试机器的执行,相当于分发机的功能。

selenium安装(Windows)

方法1、通过pip 安装

C:\Users\fnngj>python3 -m pip install selenium

方法2、通过下载包安装

直接下载selenium包:

https://pypi.python.org/pypi/selenium

解压,cmd进入目录:

C:\selenium\selenium2.53.5> python3 setup.py install

python使用selenium模拟登陆淘宝

实例代码

#coding=utf-8

import time
import datetime
import sys
import os
import random

import logging

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

def common_click(driver,element_id,sleeptime=3):
  actions = ActionChains(driver)
  actions.move_to_element(element_id)
  actions.click(element_id)
  actions.perform()
  time.sleep(sleeptime) 

def login_in(user,pwd):
  #open login page
  driver.get('https://login.taobao.com/member/login.jhtml')
  time.sleep(3)
  sb=driver.find_element_by_class_name("login-switch")
  commonclick(driver,sb)
  userbox=driver.find_element_by_id("TPL_username_1")
  pwdbox=driver.find_element_by_id("TPL_password_1")
  userbox.clear()
  userbox.send_keys(user)
  commonclick(driver,pwdbox) 
  pwdbox.send_keys(pwd)
  loadmore=driver.find_element_by_id("J_SubmitStatic")
  commonclick(driver,loadmore)
  time.sleep(20)
if __name__ == '__main__': 
  DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.loadImages'] = True 
  DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.userAgent'] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0 "

  driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
  driver.set_script_timeout(30)
  driver.set_page_load_timeout(30)

  login_in(user,password)

总结

以上就是本文关于python编程使用selenium模拟登陆淘宝实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python中的Socket 与 ScoketServer 通信及遇到问题解决方法

Socket有一个缓冲区,缓冲区是一个流,先进先出,发送和取出的可自定义大小的,如果取出的数据未取完缓冲区,则可能存在数据怠慢。其中【recv(1024)】表示从缓冲区里取最大为1024...

Python中return self的用法详解

在Python中,有些开源项目中的方法返回结果为self. 对于不熟悉这种用法的读者来说,这无疑使人困扰,本文的目的就是给出这种语法的一个解释,并且给出几个例子。 在Python中,re...

对python中执行DOS命令的3种方法总结

1. 使用os.system("cmd") 特点是执行的时候程序会打出cmd在Linux上执行的信息。 import os os.system("ls") 2. 使用Popen...

使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法

使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法

下面看下通过Pyinstaller打包Pygame库写的小游戏程序出现的问题解决方法 # -基于Python的Pygame库的GUI游戏 游戏内容是通过飞船发射子弹来射击外星人 空格键为...

Sanic框架Cookies操作示例

本文实例讲述了Sanic框架Cookies操作。分享给大家供大家参考,具体如下: 简介 Sanic是一个类似Flask的Python 3.5+ Web服务器,它的写入速度非常快。除了Fl...