Python Selenium:无法在iframe内滚动

时间:2017-06-21 06:17:36

标签: javascript python selenium firefox iframe

unable to scroll the tab profile

嗨,我可以在标签之间切换,访问所有元素。我无法滚动此iframe。请帮忙。我使用的代码如下。

    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)

3 个答案:

答案 0 :(得分:0)

您可以使用此代码向下滚动框架。

frame.contentWindow.scrollTo(0, 300);

有关详细信息,您可以看到以下链接: - scroll an iframe from parent page

答案 1 :(得分:0)

我发现以下命令可以提供帮助。首先,假设已经切换到可以访问该元素的iframe,请存储该元素的位置。然后切换回默认内容并在窗口中滚动。然后,再次搜索iframe,切换到该iframe,然后重新加载Selenium中需要继续的所有其他动态变量。

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()

答案 2 :(得分:0)

尝试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

Python函数包装器:

# 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)

有关javascript函数scrollIntoView

的更多信息