1

Iam trying to check whether the product is 12 or not by using assert equal function.Please check the below script i have tried:

def test_search(self):
    driver=self.driver
    driver.get("http://magento-demo.lexiconn.com/")
    driver.maximize_window()
    driver.find_element_by_xpath(".//*[@id='search']").send_keys("Bed & Bath")
    driver.find_element_by_xpath(".//*[@id='search_mini_form']/div[1]/button").click()
    lis = driver.find_element_by_xpath("//h2[@class='product-name'] / a")
    self.assertEqual(12,len(lis))
4
  • Are you sure "//h2[@class='product-name'] / a" is valid xpath for your case? Please check manually with chrome console Commented Jul 2, 2018 at 5:26
  • Yes ..it's valid xpath only Commented Jul 2, 2018 at 5:31
  • What do you see on chrome console? How many elements it catches? Commented Jul 2, 2018 at 5:32
  • Actually this xpath "//h2[@class='product-name'] / a" explains the ancor tag "a" catches 12 products under site "magento-demo.lexiconn.com" .. Iam looking forward to check whether the 12 products is present or not... Commented Jul 2, 2018 at 5:40

2 Answers 2

23

Replace this :

lis = driver.find_element_by_xpath("//h2[@class='product-name']/a")  

To :

lis = driver.find_elements_by_xpath("//h2[@class='product-name']/a")  

Note that find_elements will return a list of web element where as find_element will only return one element if found.

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

1 Comment

When I tried with this xpath //h2[@class='product-name']/a , it showed me 9 elements at my end , please re-verify it.
0

Looks like you're using driver.find_element_by_xpath rather than driver.find_elements_by_xpath. You may also want to take the spaces out of

"//h2[@class='product-name'] / a"

I don't believe that's a valid xpath with spaces in there.

-- Edit - That's a valid xpath however switching to find_elements_by_xpath worked for me -

driver = webdriver.Chrome(chrome_options = options, executable_path = driver_path)
driver.get('http://magento-demo.lexiconn.com/')
driver.find_element_by_xpath(".//*[@id='search']").send_keys("Bed & Bath")
driver.find_element_by_xpath(".//*[@id='search_mini_form']/div[1]/button").click()
lis = driver.find_elements_by_xpath("//h2[@class='product-name']/a")

print(len(lis))

Outputs a 9

4 Comments

But did you switch to find_elements_by_class_name?? That's what fixed it in my code - I edited it back into original answer.
still facing the same problem
I didn't tried by using the class name ..Because these xpath "//h2[@class='product-name'] / a" manually written by me .
Actually this xpath "//h2[@class='product-name'] / a" explains the ancor tag "a" catches 12 products under site "magento-demo.lexiconn.com/"; .. Iam looking forward to check whether the 12 products is present or not... – vignesh 4 mins ago edit

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.