Python Selenium AttributeError: object has no attribute

412 Views Asked by At

The problem is although i can see the method I'm calling, the error says I don't have a method the way I call. Here is the error:

Traceback (most recent call last):
  File "C:\Users\htuna\selenium-playground\src\query\ncts_query.py", line 21, in <module>
    query.query_press_key()
  File "C:\Users\htuna\selenium-playground\src\query\ncts_query.py", line 13, in query_press_key
    self.__chrome.press_key("/html/body/form/div[4]/div[2]/div[1]/div/div[3]/div/div/div[2]/table[1]/tbody/tr/td/table/tbody/tr[4]/td[2]/input",
    ^^^^^^^^^^^^^
AttributeError: 'Query' object has no attribute '_Query__chrome'

I can use method from Chrome() classes in Ncts() classes however I can't use the same method under Query() class. Here is my code:

from src.query.ncts_query_data import QueryData
from src.login.ncts_login import Ncts
from src.browser.chrome import Chrome

class Query:

    def _init_(self, queryData: QueryData, ncts: Ncts, chrome: Chrome):
        self.__queryData = queryData
        self.__ncts = ncts
        self.__chrome = chrome

    def query_press_key(self):
        self.__chrome.press_key("/html/body/form/div[4]/div[2]/div[1]/div/div[3]/div/div/div[2]/table[1]/tbody/tr/td/table/tbody/tr[4]/td[2]/input",
                                "DENEME")

query = Query()
ncts = Ncts()

ncts.login()
ncts.open_guarentee_query()
query.query_press_key()
1

There are 1 best solutions below

2
On

There are 2 issues with your class definition.

  1. It should be __init__ not _init_. (Double underscore)
  2. You cannot access directly __chrome since you wrote it with double underscore (__). It means that it should not be accessed outside of class. It is similar to a private variable but not as private with other languages.

To fix your code here is a sample.

from src.query.ncts_query_data import QueryData
from src.login.ncts_login import Ncts
from src.browser.chrome import Chrome

class Query:

    def __init__(self, queryData: QueryData, ncts: Ncts, chrome: Chrome):
        self.__queryData = queryData
        self.__ncts = ncts
        self.chrome = chrome

    def query_press_key(self):
        self.chrome.press_key("/html/body/form/div[4]/div[2]/div[1]/div/div[3]/div/div/div[2]/table[1]/tbody/tr/td/table/tbody/tr[4]/td[2]/input",
                                "DENEME")