栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

手把手教你搭建python+selenium自动化环境

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

手把手教你搭建python+selenium自动化环境

快速+简单搭建环境。如果有问题,欢迎进群讨论留言。

第一步:安装python解释器。官网地址:https://www.python.org/

自动化测试最好下载3.7的。

下载完成后打开这个文件,然后傻瓜式安装。

 

安装好后,win+r打开命令行窗口,输入python。如果显示下图,就说明安装成功。 

第二步:安装pycharm编译器。傻瓜式安装就可以了。

http://www.jetbrains.com/pycharm/download/#section=windows.

 第三步:安装selenium库。pip install selenium

第四步:下载浏览器的驱动,我一般用的都是chrome比较具有代表性。

Chrome:
https://sites.google.com/a/chromium.org/chromedriver/downloads(需要翻墙)

Firefox:
https://github.com/mozilla/geckodriver/releases。

下载好后,将驱动放在python文件夹的scripts根目录中。

第五步:打开pycharm写第一个自动化demo验证环境是否有问题

 

第一个demo

from selenium import webdriver
import time
import unittest
class LoginPage(object):
    driver=webdriver.Firefox()
    driver.get("https://passport.hupu.com/pc/login?project=nba&from=pc")
    time.sleep(5)
    def test_login(self):
        self.driver.find_element_by_xpath("//*[@id='J_username']").click()
        self.driver.find_element_by_xpath("//*[@id='J_username']").clear()
        self.driver.find_element_by_xpath("//*[@id='J_username']").send_keys("18777777777")
        self.driver.find_element_by_xpath("//*[@id='J_pwd']").click()
        self.driver.find_element_by_xpath("//*[@id='J_pwd']").clear()
        self.driver.find_element_by_xpath("//*[@id='J_pwd']").send_keys("111111111")
        self.driver.find_element_by_xpath("//*[@id='SM_BTN_1']/div[1]/div[4]").click()
        time.sleep(6)
    def test_threads(self):
        self.driver.get("https://bbs.hupu.com/hcbig")
        time.sleep(6)
        self.driver.find_element_by_id("g_n").click()
        time.sleep(6)
        self.driver.find_element_by_id("atc_title").click()
        self.driver.find_element_by_id("atc_title").clear()
        self.driver.find_element_by_id("atc_title").send_keys("橙子发的文字贴")
        self.driver.find_element_by_xpath("//body").click()
        self.driver.find_element_by_xpath("//body").send_keys("橙子使用自动化测试发的文字贴")
        self.driver.find_element_by_id("submitview").click()
login=LoginPage()
login.login()
time.sleep(4)
login.fatie_4524()

只要能启动成功启动浏览器,并且能看到鼠标自动操作,就代表环境没有问题了。 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号