Reputation: 21
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:
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'
======================================================================
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'
======================================================================
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
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.
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