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
.