1

I'm trying to create a script that will open a URL in a CSV file and input the same information, close the browser and open the 2nd URL inputting the same data. I

import csv
import webbrowser

file = open('mycsvfile.csv','r')
reader = csv.reader(file)
for row reader:
   webbrowser.open_new(row[0])

I know this will open all the URLs in my csv file. Is there a way I can open a url, input my data. close the URL and repeat the process with the 2nd URL in the row?

1
  • use Selenium instead of webbrower. With webbrowser you can only open file in browser but you can't control this browser to input data. With Selenium you can control web browser and you can input data. Commented Jul 2, 2019 at 14:57

1 Answer 1

2

With the added help of selenium and looping through as normal, we're able to navigate to each url within our csv file. You could try the following :)

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from csv import reader


def main(handler):

    """ Start firefox instance """
    driver = webdriver.Firefox()

    for row in handler:
        """ Navigate to url """
        driver.get(str(row[0]))
        """ Input data """
        driver.find_element_by_id("someId").send_keys("someData")
        """ Close browser """
        driver.close()

    print("Completed!")

if __name__ == '__main__':

    """ Read from CSV """
    with open('mycsvfile.csv') as file:
        handler = reader(file)
        main(handler)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.