1

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)

Solution

Using @Doodlevib's suggestion, I managed to work out the code without use the Path module:

with open(path_to_csv, 'r') as mycsv:
    reader = csv.reader(mycsv)
    for row in reader:
        urlstring = row[0]
        time.sleep(2)
        driver.get(urlstring)

I also had to add the 'r' to indicate the code to read the file. I used a separate testing terminal and the print() command to ensure that the above snippet was spitting out the 1st row of the CSV.

Initially I had through it was a coding issue but I ruled that out after I saw that the CSV I was using was encoded at cp1252.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
datoro
  • 59
  • 10

1 Answers1

1

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).

DoodleVib
  • 159
  • 13
  • I really appreciate it. I'll give this a shot and get back to you if I run into any issues but this makes sense to me. – datoro Sep 16 '21 at 03:59
  • I'm not sure if I might be doing it wrong. On the CSV, does the address have to be linked at all or in quotes? Also, with the `driver.get(urlstring)` I am getting `InvalidArgumentException`. I am not sure what's causing this – datoro Sep 16 '21 at 04:19
  • Hi @datoro, A [CSV file](https://en.wikipedia.org/wiki/Comma-separated_values) doesn't store hyperlink info, a CSV file just saves plain text separated into chunks by the file's "delimiter" (usually a comma, hence "comma-separated-value"). So the urls in the CSV should not be hyperlinks. You probably don't want them in quotes; quotes are Python's shorthand for creating strings. It also sounds like you may have an Excel file (not a CSV), saved as `.xlsx` format or other, in which case the encoding may need to be parsed differently than the default for `open()` to avoid errors. – DoodleVib Sep 16 '21 at 04:29
  • I've confirmed that I have a CSV file and the text inside it is just plain text. I'm not sure if I have the wrong syntax or the wrong structure here since the inputs seem to be working fine. In the body of the question I have stored where I have placed your code in relation to the rest of mine. Thank you! – datoro Sep 16 '21 at 04:47
  • I can also confirm that this file's encoding is `cp1252` – datoro Sep 16 '21 at 04:53
  • 1
    I really appreciate your help. This morning after thinking through the logic of the code I went back and found a solution. I have posted the snippet of code that works for me. Thanks again for all your help! – datoro Sep 16 '21 at 18:30
  • 1
    Sure thing @datoro! I'm very glad to see it was helpful for solving your problem _and_ that you were able to learn a little bit more about Python along the way; a good exercise in debugging goes a long way. Thank _you_ for posting your "final product" code that worked for your intended purpose; that's a very good practice because it's helpful for anyone who uses your question as a resource in the future. – DoodleVib Sep 17 '21 at 08:06