import csv import unittest from time import sleep from ddt import ddt,data,unpack from selenium.webdriver.chrome import webdriver from selenium.webdriver.common.by import By def add(a,b): return a+b def getData(file_name): rows=[] file_data=open(file_name,"r",encoding='utf8') reader=csv.reader(file_data) next(reader) for row in reader: rows.append(row) return rows def login(driver,username,pwd): driver.get("https://www.baidu.com") # 3. 在谷歌浏览器中打开百度链接sleep(0.5) driver.find_element(By.ID, "kw").send_keys("学习通登录") sleep(0.5) driver.find_element(By.ID, "su").click() # #5.点击搜索按钮 sleep(5) driver.find_element(By.XPATH, '//*[@id="2"]/div/div[1]/h3/a').click() handles = driver.window_handles driver.switch_to.window(handles[-1]) sleep(5) driver.find_element(By.ID, "phone").send_keys(username) driver.find_element(By.ID, "pwd").send_keys(pwd) driver.find_element(By.ID, "loginBtn").click() title=driver.title return title @ddt class SimpleTest(unittest.TestCase): def setUp(self): print("set up") def tearDown(self): print("teardown") @data(*getData('week17.csv')) @unpack def test_login(self, username, pwd, title): print("执行test_add") accounttitle = login(self.driver,username,pwd,title) acttitle = title self.assertEqual(accounttitle,acttitle) if __name__=="__main__": unittest.main()
时间: 2024-03-12 17:47:32 浏览: 69
Selenium WebDriver 基础学习手册,java和python语言.zip
在代码中存在以下问题:
1. 在第一行代码中,需要在import csv和import unittest之间加上一个空格;
2. 在第二行代码中,需要在from time import sleep和from ddt import ddt,data,unpack之间加上一个空格;
3. 在第三行代码中,需要将selenium.webdriver.chrome改为selenium.webdriver.chrome.webdriver;
4. 在第四行代码中,需要将selenium.webdriver.common.by改为selenium.webdriver.common.by.By;
5. 在login函数的第三行和第六行之间需要加上一个空行;
6. 在test_login函数中,需要将login函数的第四个参数改为acttitle,以保证断言的正确性。
修改后的代码如下所示:
``` python
import csv
import unittest
from time import sleep
from ddt import ddt, data, unpack
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.by import By
def add(a, b):
return a + b
def getData(file_name):
rows = []
file_data = open(file_name, "r", encoding='utf8')
reader = csv.reader(file_data)
next(reader)
for row in reader:
rows.append(row)
return rows
def login(driver, username, pwd):
driver.get("https://www.baidu.com")
# 3. 在谷歌浏览器中打开百度链接
sleep(0.5)
driver.find_element(By.ID, "kw").send_keys("学习通登录")
sleep(0.5)
driver.find_element(By.ID, "su").click() # #5.点击搜索按钮
sleep(5)
driver.find_element(By.XPATH, '//*[@id="2"]/div/div[1]/h3/a') \
.click()
handles = driver.window_handles
driver.switch_to.window(handles[-1])
sleep(5)
driver.find_element(By.ID, "phone").send_keys(username)
driver.find_element(By.ID, "pwd").send_keys(pwd)
driver.find_element(By.ID, "loginBtn").click()
title = driver.title
return title
@ddt
class SimpleTest(unittest.TestCase):
def setUp(self):
self.driver = WebDriver()
print("set up")
def tearDown(self):
self.driver.quit()
print("teardown")
@data(*getData('week17.csv'))
@unpack
def test_login(self, username, pwd, acttitle):
print("执行test_login")
accounttitle = login(self.driver, username, pwd)
self.assertEqual(accounttitle, acttitle)
if __name__ == "__main__":
unittest.main()
```
希望能够帮到您!
阅读全文