0

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_element_by_id("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_attribute("href")
        print(href)
        results.append(href)

    browser.close()

    return results

I'm getting error 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.NoSuchElementException: Message: Unable to locate element: [id="query"]  

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

  • Please post relevant html source. – Vishal Aggarwal Mar 29 '18 at 10:56
0

If you're talking about Wikipedia, then

  1. There is a typo in your URL
  2. Wikipedia homepage does not have en element with id="query"
0

For Python framework In the automation testing company we have Set up method for browser initialize and teardown for quitting the browser after the test got completed.

So below is the approach that can be useful:

import unittest
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver


class Test(unittest.TestCase):


    def setUp(self):
        geckodriver=r'D:/geckodriver-v0.19.1-win32/geckodriver.exe'
        capabilities = DesiredCapabilities.FIREFOX
        capabilities['marionette'] = True
        capabilities['acceptSslCerts'] = True
        profile = webdriver.FirefoxProfile()
        profile.set_preference('media.navigator.permission.disabled', True)
        profile.update_preferences()
        url = "https://www.wikipedia.org"
        global browser
        browser =  webdriver.Firefox(executable_path=geckodriver,firefox_profile=profile,capabilities = capabilities)
        browser.get(url)


    def testName(self):
        search_box = browser.find_element_by_id("searchInput")
        search_box.send_keys("ABCD")
        search_box.submit()


    def tearDown(self):
        browser.quit()

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
0

Unless I'm seeing things, you're hitting "www.wikipaedia.org" instead of "www.wikipedia.org" (you've got an extra "a"). On Wikipedia, the search field has an ID value of "searchInput".

Also I have to recommend upgrading to Python 3.7

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.