Home > OS >  get() missing 1 required positional argument: 'url'
get() missing 1 required positional argument: 'url'

Time:10-28

i keep getting error of "get() missing 1 required positional argument: 'url'" when running following code

import selenium.webdriver as webdriver

def get_results(search_term):
    url = "https://www.google.com"
    browser = webdriver.Chrome
    browser.get(url)
    search_box = browser.find_element_by_class_name('gLFyf gsfi')
    search_box.send_keys(search_term)
    search_box.submit()
    try:
        links = browser.find_element_by_xpath('//ol[@]//h3//a')
    except:
        links = browser.find_element_by_xpath('//h3//a')
    results = []
    for link in links :
        href = link.get_attribute('href')
        print(href)
        results.append(href)
    browser.close()
    return results

get_results('dog')

the code is supposed to return search results of 'dog' from google, but gets stuck on

browser.get(url)

all help is appreciated

CodePudding user response:

This issue is in the assignment of browser, browser = webdriver.Chrome. It needs to be browser = webdriver.Chrome().

In your code you are not assigning an instance of the chrome webdriver to browser, but the class itself. Thus when you call def get(self, url), your url parameter gets assigned to self and the argument url is not supplied, hence the positional argument error.

CodePudding user response:

You should modify a bit of your code. Change the import statement:

from selenium import webdriver

Also, you need to create chrome driver instance and provide the path of chromedriver jar file.

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
  • Related