• 手把手教你搭建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

    1. from selenium import webdriver
    2. import time
    3. import unittest
    4. class LoginPage(object):
    5. driver=webdriver.Firefox()
    6. driver.get("https://passport.hupu.com/pc/login?project=nba&from=pc")
    7. time.sleep(5)
    8. def test_login(self):
    9. self.driver.find_element_by_xpath("//*[@id='J_username']").click()
    10. self.driver.find_element_by_xpath("//*[@id='J_username']").clear()
    11. self.driver.find_element_by_xpath("//*[@id='J_username']").send_keys("18777777777")
    12. self.driver.find_element_by_xpath("//*[@id='J_pwd']").click()
    13. self.driver.find_element_by_xpath("//*[@id='J_pwd']").clear()
    14. self.driver.find_element_by_xpath("//*[@id='J_pwd']").send_keys("111111111")
    15. self.driver.find_element_by_xpath("//*[@id='SM_BTN_1']/div[1]/div[4]").click()
    16. time.sleep(6)
    17. def test_threads(self):
    18. self.driver.get("https://bbs.hupu.com/hcbig")
    19. time.sleep(6)
    20. self.driver.find_element_by_id("g_n").click()
    21. time.sleep(6)
    22. self.driver.find_element_by_id("atc_title").click()
    23. self.driver.find_element_by_id("atc_title").clear()
    24. self.driver.find_element_by_id("atc_title").send_keys("橙子发的文字贴")
    25. self.driver.find_element_by_xpath("//body").click()
    26. self.driver.find_element_by_xpath("//body").send_keys("橙子使用自动化测试发的文字贴")
    27. self.driver.find_element_by_id("submitview").click()
    28. login=LoginPage()
    29. login.login()
    30. time.sleep(4)
    31. login.fatie_4524()

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

  • 相关阅读:
    Spring boot实现定时器
    【光学】基于Matlab模拟光流场
    Linux服务器快速搭建pytorch
    数据库系统与应用复习——第四章数据库安全性与第五章数据库完整性
    VS 配置 OpenCV (亲测可用)
    【BOOST C++】教程3:变量和宏
    【C语言刷题】双指针原地修改数组(力扣原题分析)
    面试官问我TCP三次握手和四次挥手,我真的是
    脑机接口003 | 马斯克称已实现与云端的虚拟自己对话,相关概念股份大涨
    场景应用:设计一个论坛的评论回复功能
  • 原文地址:https://blog.csdn.net/caixiangting/article/details/125452254