why the refresh is not stop when get the element in python?

Viewed 59

I have a question. Why the refresh is not stop when I got the url.

The refresh is still running when I got the url.

Thank you all.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.common.exceptions import NoSuchElementException
PATH = C:// location of webdriver
driver = webdriver.Chrome(PATH)
url = '1'
driver.get(url)

print(url)

while True:
   if '2' in url:
   

try:
   driver.get('1')
   print('done')

except:
   break 

while True:
   if driver.get('1'):
       print('done')
       break 
   else:
       time.sleep(1)
       driver.refresh()
       print('try to reload')

 with webdriver.Chrome() as driver:
 driver.get('https://www.google.com/')

 while True:
 if driver.get('https://www.google.com/'):
       print('done')
       break 

 else:
    time.sleep(1)
    driver.refresh
    print('try to reload')

I made the example through google, I don't know why I get the website of google , it is still refreshing. Thanks

1 Answers

It doesn't work because driver.get(url) doesn't return anything and it shouldn't. driver is an instance, .get(url is an instance's method, driver.get(url) change this driver instance but it returns nothing. you've to check if there is something on the page that would show you that the page is loaded properly, you can use driver.current_url to get html.

driver = webdriver.Chrome(PATH)
    
driver.get('https://lxml.de/')
while True:    
    if driver.current_url:
        print('driver.current_url != None')
        break
    else:
       print("try to reload the website")
       time.sleep(2)
       driver.refresh()

driver.get('url') would better be placed out of while loop because there is already driver.refresh()

Related