谷歌+selenuim ide导出python代码 详细代码

一、录制python的自动化代码

1、首先需要安装selenium ide 

见此处的链接 有相关的教程

https://www.cnblogs.com/zz-1021/p/14412610.html

2、对于selenium简单的使用方法

(1)打开selenium ide插件

(2)选择-录制一个新的项目

(3)输入一个录制的项目的名称

 

 (4)输入需要自动化测试的网址

(5)点击start 就是录制开始

(6)进行录制:目前使用简单的 点击搜索框的输入框,输入文字,键上回车键进行检索

  (7) 点击export

 (8)可以选择自己需要的使用的语言,此处选择python

 (9)保存

 

二、以下截图为所录制的代码,里面有些地方需要进行修改。

 需要修改的地方如下

 

 (1)需要加上  unittest.TestCase 运行测试用例的驱动类

具体功能可以自行了解

(2)将method删掉

(3)点击运行,此处pycharm会自动找到test开头的函数,进行执行,需要调用setup_method()函数

(4)期间需要模拟真实用户的操作,例如有些地方可能需要等待一些时间,例如点击按钮或者输入字符,可以适当加上sleep.time(秒数)

以下为代码,直接运行即可,如果运行不成功 则在最末尾的顶格加上  

a = TestUntitled()

a.test_untitled()


# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
import unittest

class TestUntitled(unittest.TestCase):
def setup_method(self):
self.driver = webdriver.Chrome()
self.vars = {}

def teardown_method(self):
self.driver.quit()

def test_untitled(self):
self.setup_method()
time.sleep(1)
self.driver.get("https://www.baidu.com/")
self.driver.set_window_size(1363, 1003)
self.driver.find_element(By.ID, "kw").click()
self.driver.find_element(By.ID, "kw").send_keys("自动化测试")
self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)
time.sleep(5)




原文地址:https://www.cnblogs.com/zz-1021/p/14416337.html