Python Forum

Full Version: Simple Element Check Code in Selenium Not Working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
There's dynamically loaded iframe content 3/4 the way down, towards the bottom of the webpage. It only loads once I scroll close to it. I need this snippet to scroll down in small increments until the element exists.

Here's my code:

from selenium.common.exceptions import NoSuchElementException

loop_counter = 0

while loop_counter == 0:
    driver.execute_script("window.scrollTo(0, 500)")
    sleep(1)

    try:
        driver.find_element_by_xpath('//IFRAME[contains(@src, "https://myiframe.com/id?=")]')

    except NoSuchElementException:
        pass

    if driver.find_element_by_xpath('//IFRAME[contains(@src, "https://myiframe.com/id?=")]').is_displayed():
        loop_counter = 1
Any ideas where I'm screwing up?
Might be faster to just scroll to bottom
	def scroll_to_bottom():
	    SCROLL_PAUSE_TIME = 0.5
	    # Get scroll height
	    last_height = self.browser.execute_script("return document.body.scrollHeight")

	    while True:
		    # Scroll down to bottom
		    self.browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")

		    # Wait to load page
		    time.sleep(SCROLL_PAUSE_TIME)

		    # Calculate new scroll height and compare with last scroll height
		    new_height = self.browser.execute_script("return document.body.scrollHeight")
		    if new_height == last_height:
		        break
		    last_height = new_height