1

I'm looking for a way to loop through URL's based on a list of values in Python Selenium.
I have a list like: 1000 1200 1250

A website is structured like: https://example.com/1000/events. I'd be able to reach this by: driver.get(https://example.com/1000/events) However, I'd ideally define the list above, and then trace the URL based on the list values. Ideally, make the script go from https://example.com/1000/events, and after that script is done, go to https://example.com/1200/events etc.

Anyone that is able to help me with this? Thanks in advance

1 Answer 1

1

Define a base_url like this :

base_url = 'https://example.com/{}/events'

Then if your list is for example :

my_list = [1000, 1200, 1500]

You can do :

for num in my_list:
    url = base_url.format(num)
    <do your stuff here using url variable>
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this! It seems to work, I'm just wondering, how do I make it go from the first of the list to the second of the list? For now it seems to grab 1 value out of my_list
what do you mean ? for num in my_list iterates over all elements of your list. Be sure however to not use the same name for url and base_url variables.
I think I'm not sure what you meant with:' <do your stuff here using url variable>' Do I put the list values there again?
for example, you could do driver.get(url)

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.