0

I want to do it in a loops in python: for i in range (x) 1.open site 2. do my instructions 3.close page my code:

 PATH = "C:\Program Files (x86)\chromedriver.exe"
   driver = webdriver.Chrome(PATH)

   for i in range(1,100):

       driver.get("google.com")
       time.sleep(5)
       driver.quit()
       time.sleep(5)

and it works but only once, do all once and shows error. What should i do?

2 Answers 2

1

Your issue is that once you call quit() you terminated the session. Next call to the driver will fail as the session was destroyed. You need to create a new instance of the driver.

Using the context manager, use the with statement

for i in range(1, 100):
    with webdriver.Chrome(PATH) as driver:
        driver.get("https://google.com")
        time.sleep(5)

This will open and close the session without explicitly calling the methods.

Sign up to request clarification or add additional context in comments.

1 Comment

This should nail the OP
0

Try deleting driver.quit(). You don't need to close the driver window in every loop; you can just navigate to a new page using the same window. But if you do close one in every loop, then you also need to open one in every loop.

2 Comments

for i in range(1,100): driver = webdriver.Chrome(PATH) driver.get("google.com") time.sleep(5) driver.quit() time.sleep(5)
Perfect! Probably not necessary to sleep after window close.

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.