Random_User
Random_User

Reputation: 71

Reload page asynchronously using Selenium webdriver

I'm using the latest Firefox webdriver and selenium. I'm trying to make an inventory checker bot, where it periodically refreshes a bunch of pages. Because I have a lot of links, in order to save on memory, I've implemented it so that each page is opened in a different firefox tab.

In order to refresh a tab, I need to switch to it and then refresh the current page. However, driver.refresh() takes ~10 seconds for some websites to refresh, and since I can't refresh any of the other pages in the meantime, it slows my workload considerably.

driver = webdriver.Firefox()

def refresh_page(self):
    logging.info('Starting to refresh page %s', self.url)
    self.switch_to()
    time.sleep(self.delay)
    # driver.find_element_by_tag_name('body').send_keys(Keys.F5)
    driver.refresh()
    logging.info('Finished refreshing page')

def switch_to(self):
    driver.switch_to.window(self.tab)

Is there any way to refresh a page asynchronously? It looks like other people have run into the same issue: Asynchronous refresh with Selenium + Python.

Sending the F5 key does not work for some reason. I have also tried calling driver.get(self.url) with no luck (Still synchronous). Another solution might be to find a way to synchronously reload all tabs at once, but I have not found a solution for that either.

Upvotes: 1

Views: 961

Answers (1)

Random_User
Random_User

Reputation: 71

I think I managed to find a workaround by executing a custom script:

driver = webdriver.Firefox()

def refresh_page(self):
    logging.info('Starting to refresh page %s', self.url)
    self.switch_to()
    time.sleep(self.delay)
    driver.execute_script("location.reload()")
    logging.info('Finished refreshing page')

def switch_to(self):
    driver.switch_to.window(self.tab)

But if anyone has any other suggestions, let me know. The other person who had this same issue (link in question) said this doesn't always work, so we'll have to wait and see

Upvotes: 1

Related Questions