Artemm
Artemm

Reputation: 122

How to wait only DOM to load with Selenium (without images)

There is a long list of the images on the page. There is an AJAX "Load more images" button in the application (at the bottom of the page) which is activated automatically when user scrolling page down. I want to scroll the page to the very end with Selenium and make sure that there is no button "Load more images" at the bottom of the page. The script (there could be more elegant solution but that's not a point)

browser.execute_script("""
            setInterval(function() {
                window.scrollBy(0,50000);
            }, 1000);
           ;""")
invisibleShowMoreCounter = 0;
while True:
        if "" ==  browser.find_element_by_id("show_more").text:
            invisibleShowMoreCounter = invisibleShowMoreCounter + 1
            if (invisibleShowMoreCounter  > 3): break
        time.sleep(1)

It scrolls the page to the bottom and then checks that there is no "show_more" element for 3 seconds (not sure if this cycle is needed at all though). So the thing is that it successfully scrolls the page down but after this, it stuck on browser.find_element_by_id("show_more") until the page fully loaded (including images) which takes a lot of time. So DOM is there but because there are a lot of images it takes about few minutes to load all of them. I'm not 100% sure that this is because page not fully loaded but it looks very likely (so it goes to the first iteration of the cycle and then just stay on find_element_by_id for few minutes and then go further and successfully finished. The question is if it is possible to command find* method not to wait for all images to finish downloading so the test will not take few minutes to pass?

Setting browser.implicitly_wait(1) (which seems kind of relevant to this) does not seem to help.

Upvotes: 2

Views: 1357

Answers (1)

user861594
user861594

Reputation: 5908

You can wait for browser/document status to interactive. That is wait for js condition

document.readyState='interactive'

Upvotes: 1

Related Questions