admin管理员组

文章数量:1388080

Hi, I am able to switch between tabs, access all elements. I am unable to scroll in this iframe. Please help. Code I am using is as follows.

    iframe = self.browser.find_elements_by_tag_name('iframe')[0]
    self.browser.switch_to_frame(iframe)

    # Iterating through tabs
    for tab_name in soup.find_all('md-dummy-tab'):
        return_dict[tab_name.text] = []
        tab_names.append(tab_name.text)
        # clicking on tabs one by one
        self.force_click('xpath=/html/body/div/md-content/md-tabs/md-tabs-wrapper/md-tabs-canvas/md-pagination-wrapper/md-tab-item[%s]/span' % tab)
        tab += 1
        time.sleep(2)

        # Scrolling
        try:
            self.browser.execute_async_script("frame.scrollTo(0, 10000);")
        except:
            pass
        time.sleep(2)

Hi, I am able to switch between tabs, access all elements. I am unable to scroll in this iframe. Please help. Code I am using is as follows.

    iframe = self.browser.find_elements_by_tag_name('iframe')[0]
    self.browser.switch_to_frame(iframe)

    # Iterating through tabs
    for tab_name in soup.find_all('md-dummy-tab'):
        return_dict[tab_name.text] = []
        tab_names.append(tab_name.text)
        # clicking on tabs one by one
        self.force_click('xpath=/html/body/div/md-content/md-tabs/md-tabs-wrapper/md-tabs-canvas/md-pagination-wrapper/md-tab-item[%s]/span' % tab)
        tab += 1
        time.sleep(2)

        # Scrolling
        try:
            self.browser.execute_async_script("frame.scrollTo(0, 10000);")
        except:
            pass
        time.sleep(2)
Share Improve this question edited Jun 21, 2017 at 12:39 unnikrishnan.v asked Jun 21, 2017 at 6:17 unnikrishnan.vunnikrishnan.v 932 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

You can use this code to scroll-down in frame.

frame.contentWindow.scrollTo(0, 300);

For more info you can see this link :- scroll an iframe from parent page

I found the following mands to help. First, assuming one has already switched to an iframe where the element is accessible, store the location of that element. Then switch back to the default content and scroll in the window. Then, search again for the iframe, switch to that iframe, and reload any other dynamical variables in Selenium needed to continue.

length = prods[p].location["y"]
self.driver.switch_to.default_content()
self.driver.execute_script("window.scrollTo(0,"+str(length) + ");")
iframe = self.driver.find_elements_by_xpath('.//iframe[contains(@id,"frame")]')
self.driver.switch_to_frame(iframe[0])
prods = self.driver.find_elements_by_xpath('.//div[@class="products"]')
prods[p].click()

Try location_once_scrolled_into_view:

# assume `driver` is an instance of `WebDriver`
element = driver.find_element(By.CSS_SELECTOR, 'some_element')
# `location_once_scrolled_into_view` is a property that behaves like function
element.location_once_scrolled_into_view

A Python function wrapper:

# it's not necessary to switch into the iframe where your element is located before calling this function.
def scroll_into_view(driver, element=None, css_selector=None):
    if (not element) and (not css_selector):
        return
    if css_selector and (not element):
        element = driver.find_element_by_css_selector(css_selector)
    driver.execute_script('arguments[0].scrollIntoView({block: "center"})', element)

more about javascript function scrollIntoView

本文标签: javascriptPython Selenium Unable to scroll inside iframeStack Overflow