(编辑:jimmy 日期: 2024/11/18 浏览:2)
selenium简介
selenium 是一个web的自动化测试工具,不少学习功能自动化的同学开始首选selenium ,相因为它相比QTP有诸多有点:
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模拟登陆淘宝实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!