I am running this code:

from selenium import webdriver
from `selenium.webdriver.firefox.options` import Options

Setup:

options = Options()
options.add_argument("--headless")

def get_results(search_term):

    url = "https://www.wikipaedia.org"
    browser = webdriver.Firefox(firefox_options=options, executable_path=r"/usr/local/bin/geckodriver")
    browser.get(url)
    search_box = browser.find__by_("query")
    search_box.send_keys(search_term)
    search_box.submit()

    try:

        links = browser.find_elements_by_xpath("//ol[@class='web_regular_results']//h3//a")

    except:

        links = browser.find_elements_by_xpath("//h3//a")

    results = []

    for link in links:

        href = link.get_("href")
        print(href)
        results.append(href)

    browser.close()

    return results

I’m getting messages:

Traceback (most recent call last):      
  File "search_items.py", line 37, in <module>                                  
    get_results("dog")                  
  File "search_items.py", line 13, in get_results                               
    search_box = browser.find_element_by_id("query")                            
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 351, in find_element_by_id 
    return self.find_element(by=By.ID, value=id_)                               
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 955, in find_element       
    'value': value})['value']           
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute            
    self.error_handler.check_response(response)                                 
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response  
    raise exception_class(message, screen, stacktrace)                          
selenium.common.exceptions.: Message:  to  element: [id="query"]  

I have tried to solve but it is beyond me.
Any help would be greatly appreciated.



Source link https://sqa.stackexchange.com/questions/32771/getting-error-nosuchelementexception-unable-to-locate-element-with-id-attrib

LEAVE A REPLY

Please enter your comment!
Please enter your name here