Home > OS >  How can I adapt this selenium code so that it constantly refreshes in a loop for 'n' times
How can I adapt this selenium code so that it constantly refreshes in a loop for 'n' times

Time:04-29

I have the following code:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

options=Options()

driver=webdriver.Chrome(options=options)

driver.get("https://www.theguardian.com/uk")

time.sleep(2)
driver.refresh()

I'd like to be able to do the following with the above code:

1.Go to the url

2.Wait for page to load

3.Refresh page

4.Repeat steps 2 & 3 for 'n' number of times (let us say n=100)

CodePudding user response:

You can simply introduce a while loop have put counter i, and set a condition that if i == 100 (let's say), you want to come out of the loop.

Code:

i = 0
while True:
    time.sleep(2)
    driver.refresh()
    if i == 100:
        break
    else:
        continue
    i = i   1
  • Related