简体   繁体   English

pycharm- python 与 selenium web 驱动程序。 得到错误 object has no attribute driver

[英]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.

相关问题 Selenium Python 错误'对象没有属性驱动程序' - Selenium Python error 'object has no attribute driver' Python-Selenium Web驱动程序错误-self._driver.execute-AttributeError:“ unicode”对象没有属性“ id” - Python - Selenium Web Driver error - self._driver.execute - AttributeError: 'unicode' object has no attribute 'id' 带有 PyCharm 的 Python + opencv-'opencv' 没有属性 'imread' - Python + opencv with PyCharm- 'opencv' has no attribute 'imread' Python selenium web驱动程序错误 - Python selenium web driver error Python,网络驱动程序错误(Selenium) - Python, error with web driver (Selenium) python selenium Chrome Web驱动程序。 如何代理IP /在哪里购买 - python selenium Chrome Web Driver. How to Proxy IP/ Where to buy Selenium 'FirefoxWebElement' object 没有属性 '_driver' - Selenium 'FirefoxWebElement' object has no attribute '_driver' Python Selenium,Firefox 驱动程序。 关闭打开/保存文件弹出窗口 - Python Selenium, Firefox driver. Dismiss Open/Save file popup 为什么我会收到此错误:AttributeError: 'Fetch_Info' object has no attribute 'driver' 当我运行我的 selenium_web.py 脚本时 - Why do I get this error :AttributeError: 'Fetch_Info' object has no attribute 'driver' when I run my selenium_web.py script Selenium Web驱动程序| 查找对象 蟒蛇 - Selenium Web Driver | Find object | Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM