skeitel
skeitel

Reputation: 271

How to click on every link from a list of links? - Python, Selenium

I am trying to click on one link from a list of links, wait one second (not added yet) and then click on the next link. Using selenium and bs4. This is my code:

links = ['https://www.bing.com', 'https://www.google.com', 'https://www.yahoo.com']
for url in links:
    url = "'"+url+"'"
    print(url,'\n')
    browser = webdriver.Firefox
    browser.get(url)

and this is the error I am getting:

Traceback (most recent call last):
  File "C:/Users/SK/PycharmProjects/untitled/linkedin_bot.py", line 38, in <module>
    browser.get(url)
TypeError: get() missing 1 required positional argument: 'url'

What exactly am I doing wrong and how to solve it? Thanks

Upvotes: 1

Views: 2755

Answers (2)

Ben
Ben

Reputation: 36

The main problem is that the "url" variable name is the same as the "url" in the for loop. Try renaming the variable.

Here is the amended code. I have used Chrome as the driver, but you can use Firefox or any other driver. Remember to change the executable path of the driver to match your situation.

from selenium import webdriver

import time

browser = webdriver.Chrome(executable_path='C:\Chrome\chromedriver.exe')

links = ['https://www.bing.com', 'https://www.google.com', 'https://www.yahoo.com']
for url in links:
    urls = "'"+url+"'"
    print(urls,'\n')
    browser.get(url)
    time.sleep(1)

Upvotes: 0

zondo
zondo

Reputation: 20336

You need to change browser = webdriver.Firefox to browser = webdriver.Firefox(). As it is, when you call browser.get(url), it is expecting a webdriver.Firefox instance as the first argument and a url as the second argument. When you include the parentheses, you are creating an instance of webdriver.Firefox and Python automatically gives it as the first argument.

Upvotes: 1

Related Questions