0

What I am to do is trying to iterate over all select values and grab some data. Data is loaded via AJAX so the page isn't reloading - the data in the table is just changing. As you can see I have tried to use explicit wait but that doesn't work - select value can be "4" but information in the table can still belong to the "3" value. I would like to know how can I be sure that the page is really updated?

There're some similar questions but I didn't find proper solution.

for i in range(N):
    try:
        driver.find_element_by_xpath("//select[@id='search-type']/option[@value='%s']"%i).click()
        driver.find_element_by_name("submit").click()   
    except:
        print i
        continue
    elem = driver.find_element_by_id("my_id")
    wait.until(lambda driver: driver.find_element_by_id('search-type').get_attribute("value")==str(i))
    if no_results(get_bs_source(driver.page_source)):
        print "No results %s"%i
    if is_limited(get_bs_source(driver.page_source)):
        print "LIMITED %s"%i
    data+=get_links_from_current_page(driver)

For sure I can use time.sleep(N) but that's not the way I would like to do this.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
ig-melnyk
  • 2,769
  • 2
  • 25
  • 35
  • What is causing the page to update? Is it a click or ? Once you figure that out, look for a way to detect the change? Is there a message posted somewhere that states that the update is done, is there a color change, etc.? – JeffC Sep 07 '15 at 13:57
  • Can you explain "the page is really updated"? What does that meant **for your app**? How can **you** tell? – SiKing Sep 07 '15 at 14:55
  • @SiKing after browser clicks on some button - the page data in the table gets updated. I can see it because data changes. I thought there're ways to check if there's some network activity, or something like that. – ig-melnyk Sep 07 '15 at 15:19
  • There are a couple of different approaches that might work. Do the elements that contain the data that changes get removed and readded to the DOM? That would be the easy case. If the value of the attribute is all that changes, you'll need to wait for a value change. – JimEvans Sep 07 '15 at 15:22
  • @JimEvans probably the first case. – ig-melnyk Sep 07 '15 at 16:10
  • Unfortunately your question does not have enough information for us to be able to give you a definite answer. I had something *slightly* similar once, and had to build a custom FluentWait. Have a look at this http://stackoverflow.com/q/26534383/3124333 to see how I ended up solving it. – SiKing Sep 08 '15 at 00:32

2 Answers2

0

Selenium is using document.ready state to determine if page has loaded or not. WebDriverWait implementing such kind of logic. You should not use document.readyState directly! In most of functions it's waiting, for example:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

browser = webdriver.Firefox()
browser.get("http://something.com")
delay = 10 # seconds

WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('IdOfMyElement')))

In this case it will execute document.ready 2 times: 1st time browser.get, second time while WebDriverWait.

Selenium implementation: https://github.com/SeleniumHQ/selenium/blob/01399fffecd5a20af6f31aed6cb0043c9c5cde65/java/client/src/com/thoughtworks/selenium/webdriven/commands/WaitForPageToLoad.java#L59-L73

Stan E
  • 3,396
  • 20
  • 31
  • worth mentioning the original source for this answer too: http://stackoverflow.com/questions/26566799/selenium-python-how-to-wait-until-the-page-is-loaded – jim tollan Sep 08 '15 at 07:00
  • Obviously, just a half if it, as I took the first code I found to describe it (the source code by itself is pretty useless) – Stan E Sep 08 '15 at 07:09
-1
var javaScriptExecutor = Instance as IJavaScriptExecutor;

bool isPageLoaded = javaScriptExecutor.ExecuteScript("return document.readyState;").Equals("complete");
Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
  • 1
    You should not do this, as this is already implemented in webdriver. https://github.com/SeleniumHQ/selenium/blob/01399fffecd5a20af6f31aed6cb0043c9c5cde65/java/client/src/com/thoughtworks/selenium/webdriven/commands/WaitForPageToLoad.java#L59-L73 – Stan E Sep 07 '15 at 17:17