Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to run the following code:
from selenium import webdriver
# create a new Firefox session
driver = webdriver.Firefox(executable_path=r'C:\Program Files\geckodriver.exe')
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("http://demo.magentocommerce.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()
# get all the anchor elements which have product names displayed
# currently on result page using find_elements_by_xpath method
products = driver.find_elements_by_xpath("//h2[@class=‘productname’]/a")
# get the number of anchor elements found
print ("Found" + str(len(products)) + "products:")
# iterate through each anchor element and print the text that is # name of the product
for product in products:
print (product.text)
# close the browser window

driver.quit()

What I have tried:

But, getting the following errors:
Traceback (most recent call last):
File "C:/Users/nava.malakar/PycharmProjects/setests/searchproducts.py", line 12, in <module>
search_field = driver.find_element_by_name("q")
File "C:\Users\nava.malakar\PycharmProjects\setests\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 487, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Users\nava.malakar\PycharmProjects\setests\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 955, in find_element
'value': value})['value']
File "C:\Users\nava.malakar\PycharmProjects\setests\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\Users\nava.malakar\PycharmProjects\setests\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="q"]


Process finished with exit code 1


- Updated the geckodriver path in the environment variables also, but still i am getting all these errros and not able to run the program
Posted
Updated 26-Apr-18 21:42pm

1 solution

The execption occurs on the line
Python
search_field = driver.find_element_by_name("q")
See 4. Locating Elements — Selenium Python Bindings 2 documentation[^]:
Quote:
4.2. Locating by Name

Use this when you know name attribute of an element. With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.
So you have to catch the execption if you can't be sure that such an element exists. See 8. Errors and Exceptions — Python 3.6.5 documentation[^].

Untested example:
Python
from selenium.common.exceptions import NoSuchElementException
# ...
hasElementQ = True
while True:
    try:
        search_field = driver.find_element_by_name("q")
        break
        except NoSuchElementException:
            # Handle it here or set a variable
            hasElelemntQ = False
# Can check the variable here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900