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-8import timeimport datetimeimport sysimport osimport randomimport loggingfrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.desired_capabilities import DesiredCapabilitiesdef 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模拟登陆淘宝实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
新闻热点
疑难解答