1

I am trying to use the selenium package in python to load up dynamical websites to be saved. But I am having mixed success. I noticed that there is a difference between the pages successfully saved and those not. In the HTML source of the successful ones, I see

<script language="javascript" type="text/javascript">
var PageIsReady = true;
</script>

whereas for the others, the var PageIsReady is false. Is there a way I can trigger off the saving after the variable has turned true?

This stackoverflow question shows how to do the timing out but it looks for the presence of a tag while I want it triggered off the value of a script variable.

Spinor8
  • 1,587
  • 4
  • 21
  • 48

2 Answers2

5

A nice solution to this problem could be to implement a custom wait condition which will check if a variable will get defined or not. The condition would have to look something like this (beware, not thoroughly tested):

class js_variable_evals_to_true(object):
    def __init__(self, variable):
        self.variable = variable
    def __call__(self, driver):
        return driver.execute_script("return {0};".format(self.variable))

Usage:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

chrome = webdriver.Chrome()
chrome.get("http://google.com")
try:
    element = WebDriverWait(chrome, 10).until(js_variable_evals_to_true("toolbar.visible")
finally:
    chrome.quit()

Check the source code in the Selenium documentation to learn how to implement custom wait conditions.

Georg Grab
  • 2,271
  • 1
  • 18
  • 28
1

You don't have to handle value of PageIsReady, just wait until <script> with exact text content appears in DOM:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//script[normalize-space()="var PageIsReady = true;"]')))
Andersson
  • 51,635
  • 17
  • 77
  • 129