2

I use Selenium and I need to endlessly wait for the page to load (because it takes a couple of hours to load) and then parse data from it. How can I do this?

I use some code like this:

wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "modal-title")))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rootinshik
  • 21
  • 1

1 Answers1

2

You can use WebDriverWait() to achieve that. For example, the following code waits for five hours or until it finds the element.

hours_to_wait = 5

element = WebDriverWait(driver, hours_to_wait *60*60).until(
EC.visibility_of_all_elements_located((By.XPATH, 'hereIsYourXpath')))

Or:

element = WebDriverWait(driver, hours_to_wait *60*60).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )

Or in your case:

element = WebDriverWait(driver, hours_to_wait *60*60).until(
        EC.presence_of_element_located((By.CLASS_NAME, "modal-title"))
    )

You need to import these:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marios
  • 26,333
  • 8
  • 32
  • 52