Python Forum
Simple Element Check Code in Selenium Not Working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Element Check Code in Selenium Not Working
#1
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?
Reply
#2
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
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Selenium suddenly fails to find element Pavel_47 3 6,207 Sep-04-2022, 11:06 AM
Last Post: Pavel_47
  Check element for a particular type Pavel_47 2 2,201 Jan-13-2021, 04:30 PM
Last Post: Pavel_47
  Selenium Python for Dropdown not working gj31980 1 2,612 Oct-27-2020, 02:02 AM
Last Post: gj31980
  Code example for entering input in a textbox with requests/selenium object peterjv26 1 1,677 Sep-26-2020, 04:34 PM
Last Post: Larz60+
  Hyperlink Click is not working in Selenium webdriver rajeev1729 0 1,996 May-02-2020, 11:21 AM
Last Post: rajeev1729
  python how to check short url working? Pyguys 10 4,567 Mar-18-2020, 01:42 AM
Last Post: Pyguys
  Clicking on element not triggering event in Selenium Python (Event Key is not in data dkaeloredo 2 4,235 Feb-16-2020, 05:50 AM
Last Post: dkaeloredo
  Selenium locating an element. JokerTux 3 2,639 Dec-28-2019, 08:50 AM
Last Post: snippsat
  Selenium returning web element instead of desired text newbie_programmer 1 5,143 Dec-11-2019, 06:37 AM
Last Post: Malt
  Python Selenium getting table element trengan 2 8,505 Dec-31-2018, 03:02 PM
Last Post: trengan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020