Francis
Francis

Reputation: 21

Selenium - AttributeError: object has no attribute 'find_element_by_css_selector'

I wanted to do a simple test script and have decided to use Amazon to try out my script. The following is my code:

import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains


class PurchaseEbook(unittest.TestCase):

    def test_setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.driver.get("https://www.amazon.com/")

    def test_selectOptionFromDDL(self):
        self.ddl_Dept = self.find_element_by_css_selector("#nav-link-shopall > span:nth-child(2)")
        self.ddl_Book = self.find_element_by_css_selector("span.nav-hasPanel:nth-child(9) > span:nth-child(1)")

        action = ActionChains(self)
        action.move_to_element(self.ddl_Dept)
        action.move_to_element(self.ddl_Book)
        action.click("div.nav-template:nth-child(8) > div:nth-child(4) > a:nth-    child(1) > span:nth-child(1)")
        action.perform()

    def test_serachKeyword(self):
        element = self.find_element_by_css_selector("#nav-search")
        element.send_keys("Simon Sinek")
        element.submit()
        element.clear()

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


if __name__ == '__main__':
    unittest.main()

Below is my error log:

ERROR [0.000931s]: test_selectOptionFromDDL (main.PurchaseEbook)

Traceback (most recent call last): File "amazon-test-script.py", line 16, in test_selectOptionFromDDL self.ddl_Dept = self.find_element_by_css_selector("#nav-link-shopall > span:nth-child(2)") AttributeError: 'PurchaseEbook' object has no attribute 'find_element_by_css_selector'

======================================================================

ERROR [0.000000s]: test_serachKeyword (main.PurchaseEbook)

Traceback (most recent call last): File "amazon-test-script.py", line 26, in test_serachKeyword element = self.find_element_by_css_selector("#nav-search") AttributeError: 'PurchaseEbook' object has no attribute 'find_element_by_css_selector'

======================================================================

ERROR [0.001004s]: test_tearDown (main.PurchaseEbook)

Traceback (most recent call last): File "amazon-test-script.py", line 32, in test_tearDown self.driver.quit() AttributeError: 'PurchaseEbook' object has no attribute 'driver'

Upvotes: 0

Views: 3303

Answers (1)

Goran
Goran

Reputation: 269

It should be:

self.ddl_Dept = self.driver.find_element_by_css_selector("#nav-link-shopall > span:nth-child(2)") 

Here's a breakdown:

self.driver: This usually refers to the browser driver object, often initialized using Selenium. For instance, it might be a Chrome or Firefox driver instance that's used to automate browser actions.

find_element_by_css_selector(): This is a method provided by Selenium's WebDriver to locate a web page element based on its CSS selector.

"#nav-link-shopall > span:nth-child(2)": This is the CSS selector being used.

  • #nav-link-shopall: This targets an element with the ID of nav-link-shopall.
  • ">:" This is a child combinator in CSS, which means it will select a direct child.
  • "span:nth-child(2)": This targets the second element that is a direct child of the element with ID nav-link-shopall. self.ddl_Dept: This is a Python instance variable (of the object represented by self). The line of code assigns the found element (from the CSS selector search) to this variable. This can then be used elsewhere in the code for various operations, like clicking the element, extracting its text, etc.

In simpler terms, the line of code is trying to find the second element directly under an element with ID nav-link-shopall on a web page and then store a reference to that element in the self.ddl_Dept variable.

Upvotes: 3

Related Questions