Home > Enterprise >  Can I feed Python links from Excel to execute script on?
Can I feed Python links from Excel to execute script on?

Time:09-16

The current code that I have has an explicit URL written in it that tells it to navigate to a specific website and run the rest of the commands.

Is there a way that I can compile a large list of these links in Excel and have Python loop the code through them so that each run is based on a new link?

This is the snippet of code that uses the explicitly pasted website URL:

driver.get("https://www.website.com")
driver.find_element_by_css_selector('[class="k-widget k-dropdown ic-can-be-dirty"]').click()

The code goes from there. Because I want to run the code on many links on our website, I'd love to find a way to dynamically change this link via Excel source or a notepad file or anything else.

Updated Code per Suggestion:

with open(path_to_csv) as mycsv:
reader = csv.reader(mycsv)
for row in reader:
    urlstring = row[0]
    driver.get(urlstring)
    driver.find_element_by_css_selector('[class="k-widget k-dropdown ic-can-be-dirty"]').click()
    time.sleep(1)

CodePudding user response:

Yes, you can use Python to parse a .csv or text file and do things with the parsed strings.

If you're doing this with a .csv file that has nothing but a list of urls in the first column, the following loop should work. In an attempt to be platform-agnostic, the code below makes use of the pathlib library, but be aware that pathlib isn't required. You just need to make sure that the open() function receives a path string properly formatted for your platform/OS. One caveat is that I don't know what your driver is, but this assumes you want to explicitly import it.

import csv
from pathlib import Path
import driver

path_to_csv = Path("\Your\Path\To\CSV.csv")

with open(path_to_csv) as mycsv:
    reader = csv.reader(mycsv)
    for row in reader:
        urlstring = row[0]
        driver.get(urlstring)
        driver.find_element_by_css_selector('[class="k-widget k-dropdown ic-can-be-dirty"]').click()

If your .csv is more complicated than a single-column list of strings, you can make use of more advanced string parsing to only run your desired functions on rows of the file that contain urls, or run on urls from certain rows/columns but not others, etc.

There are other ways to do this too. You could make a Python list of the strings you read in and then loop over the list with your desired driver functions.

If your .csv is encoded in an abnormal format, like CSV UTF-8 or a full Excel file, you would need to specify that in the arguments of the open() function (more info about that on this post).

  • Related