[英]pycharm- python with selenium web driver. Get error object has no attribute driver
I'm trying to run basic automation on tesco website on selenium web driver on python using PyCharm.我正在尝试使用 selenium web 驱动程序在 python 上的 tesco 网站上运行基本自动化,使用 Z4149CE0EE30A909A183F622C。 I am able to create 1 function and have everything within it and it works.
我能够创建 1 个 function 并在其中包含所有内容并且可以正常工作。 However when I split it all into different functions I get the following error message at:
但是,当我将其全部拆分为不同的功能时,我会在以下位置收到以下错误消息:
driver = self.driver
AttributeError: 'FirstTest' object has no attribute 'driver'
AttributeError:“FirstTest”object 没有属性“驱动程序”
My code so far:到目前为止我的代码:
import unittest
from selenium import webdriver
class FirstTest(unittest.TestCase):
def test_setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.get("http://tesco.com")
self.tearDown()
def test_websiteCheck(self):
driver = self.driver
assert "Tesco - Supermarkets | Online Groceries, Clubcard & Recipes" in driver.title
self.tearDown()
def test_loginPage(self):
driver = self.driver
signIn = self.driver.find_element_by_link_text("Sign in")
signIn.click()
username = self.driver.find_element_by_name("username")
username.send_keys("test1234@hotmail.com")
password = self.driver.find_element_by_name("password")
password.send_keys("test")
self.driver.find_element_by_xpath("/html/body/div/div/div[2]/div/div/div/div[2]/div/form/button").click()
def test_pickingGroceries(self):
self.driver = self.driver
self.driver.find_element_by_xpath("/html/body/div[1]/div/nav/div[1]/ul/li[1]/div/div[1]/a").click()
self.driver.find_element_by_xpath("/html/body/div[1]/div/nav/div[1]/ul/li[1]/div/div[1]/div/div[2]/div[1]/ul/li[2]/a/h3/span").click()
Any ideas on what i am doing wrong?关于我做错了什么的任何想法?
Did you call test_SetUp(self)
first?您是否先致电
test_SetUp(self)
?
You should create a def __init__(self):
function instead that will get run upon creating an object instance of the class您应该创建一个
def __init__(self):
function 而不是在创建 class 的 object 实例时运行
you are not calling test_SetUp function anywhere and not returning anything from test_SetUp function.My suggestion would be use init function instead of test_SetUp.Then in other functions you are creating driver=self.driver, so use driver.functions instead of self.driver again. you are not calling test_SetUp function anywhere and not returning anything from test_SetUp function.My suggestion would be use init function instead of test_SetUp.Then in other functions you are creating driver=self.driver, so use driver.functions instead of self.driver again .
from selenium import webdriver
import unittest
class FirstTest(unittest.TestCase):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
self.driver.get("http://tesco.com")
self.tearDown()
def test_websiteCheck(self):
driver = self.driver
assert "Tesco - Supermarkets | Online Groceries, Clubcard & Recipes" in
driver.title
self.tearDown()
def test_loginPage(self):
driver = self.driver
signIn = driver.find_element_by_link_text("Sign in")
signIn.click()
username = driver.find_element_by_name("username")
username.send_keys("test1234@hotmail.com")
password = driver.find_element_by_name("password")
password.send_keys("test")
def test_pickingGroceries(self):
driver = self.driver
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.