1

Hey so I have this line of code. It should visit google and refresh the page 15 times. Then close the driver and open it again another 15 times and then close the browser again and so on. It should keep doing that until the program is stopped.

from selenium import webdriver
import chromedriver_binary

driver = webdriver.Chrome()
count = 15

while count != 0:
    driver.get('https://google.com/')
    count -= 1
driver.close()

This is my code so far. Any help will be great.

3 Answers 3

1

To visit the url https://www.google.com/ and refresh the page 15 times. Then close the driver and open it again another 15 times and then close the browser again and so on you can use the following solution:

  • Code Block:

    from selenium import webdriver
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    while True:
        try:
            driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
            driver.get('https://www.google.com/')
            for _ in range(15):
                driver.refresh()
                print("Page Refreshed")
            driver.quit()
        except:
            break
    
  • Console Output:

    DevTools listening on ws://127.0.0.1:50307/devtools/browser/d97baf5f-2088-4acf-8f1a-a6568d82c649
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    [8700:3392:0823/020728.931:ERROR:broker_win.cc(55)] Error reading broker pipe: The pipe has been ended. (0x6D)
    
    DevTools listening on ws://127.0.0.1:50348/devtools/browser/9113e8a1-d82b-4a87-95c9-5e82917ceeb0
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    Page Refreshed
    
    DevTools listening on ws://127.0.0.1:50388/devtools/browser/2ce95680-edbc-4391-b516-0fb0ab136c54
    
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using driver.get('https://google.com/') to refresh the page, try using driver.refresh(). At the same time, instead of using a while loop, you should try to use a for loop. Here's how I would do it

from selenium import webdriver
import chromedriver_binary

driver = webdriver.Chrome()

for iters in range(15):
    driver.refresh()
driver.close()

Hope this helped!

2 Comments

That's helpful if I wanted to refresh but I don't I'm working on a project for work and cant share the code. But it's really similar to this except its to visit another webpage. Sorry if I wasn't clear about that.
Oh. I thought you had to refresh the page! No worries. Just change the while loop to a for loop. Beyond that, you did it correctly
0

You are not far from what you want to achieve, the on challenge is the terminal condition of you while loop, by writing while count != 0, you are asking your loop to run for all values of count from 15 to negative infinity but to skip 0. This should make you program run infinitely. You can better get it done this way:

from selenium import webdriver
import chromedriver_binary

driver = webdriver.Chrome()
count = 15
while count > 0:
    driver.get('https://google.com/')
    count -= 1
driver.close()

so your loop terminates once count becomes less than 1

1 Comment

But I want to loop multiple times. Won't this code just end after refreshing 15 times? I would like the code to restart the web driver after every 15 refreshes

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.