0

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'

2
  • could you add relevant html or tell which element are you looking for? Commented Jun 8, 2018 at 7:48
  • 1
    You are missing .driver in all find element statement. as Goran answer. it shoudd be self.driver.fine_element not self.find_element. Commented Jun 8, 2018 at 8:55

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice catch +1 !!! A few words about the solution would have been pretty helpful for the future readers on how your answer addresses the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.