-1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()

# Navigate to the application home page
driver.get("http://www.google.com")

# get the search textbox
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()

# enter search keyword and submit
search_field.send_keys("Selenium WebDriver Interview questions")
search_field.submit()

# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
lists= driver.find_elements_by_class_name("_Rm")

# get the number of elements found
print ("Found " + str(len(lists)) + " searches:")

# iterate through each element and print the text that is
# name of the search

i=0
for listitem in lists:
   print (listitem.get_attribute("innerHTML"))
   i=i+1
   if(i>10):
      break

# close the browser window
driver.quit()

Error message:

Traceback (most recent call last):

  File "C:/Users/HP/Documents/selenium1.py", line 13, in <module>
    search_field = driver.find_element_by_id("lst-ib")
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\HP\AppData\Local\Programs\Python\Python37\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: [id="lst-ib"]
  • Considering that the google.com source code does not include an element with id="lst-ib", and the search input field does not have an id attribute, it's not surprising that you're getting the error Unable to locate element. I recommend starting by right-clicking the element you're targeting, then choosing Inspect Element. That will highlight the element code in the developer tools. You'll find that the search field's most useful attribute is name="q" – Kate Paulk Jun 27 at 18:46
  • Kate's comment is correct. Additionally, Google's DOM is not robot friendly. If you are doing this as an exercise, I would suggest going to DuckDuckGo. – João Farias Jun 27 at 20:20
0

I believe you need to introduce Explicit Waits and use correct locators.

For example:

  1. Change this line:

    search_field = driver.find_element_by_id("lst-ib")
    

    to this one:

    search_field = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@name='q']")))
    
  2. Similarly:

    remove this line:

    lists= driver.find_elements_by_class_name("_Rm")
    

    and use the following instead:

    wait(driver, 10).until(lambda driver: len(driver.find_elements_by_class_name("r")) > 0)
    lists = driver.find_elements_by_class_name("r")
    

You will need the following imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC

More information:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.