【问题标题】:Python Selenium: Unable to scroll inside iframePython Selenium:无法滚动Iframe
【发布时间】:2017-11-23 21:21:10
【问题描述】:

嗨,我可以在标签之间切换,访问所有元素。我无法在此 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)

【问题讨论】:

    标签: javascript python selenium firefox iframe


    【解决方案1】:

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

    frame.contentWindow.scrollTo(0, 300);
    

    有关更多信息,您可以查看此链接:- scroll an iframe from parent page

    【讨论】:

    • 在python中,我们有等价的方法吗?
    • 你可以在 python 中使用 JavaScript 来运行这个 JavaScript 代码 driver.execute_script("document.you'rlocator('comment-user').contentWindow.scrollTo(0, 300)")
    • self.browser.execute_script('%s.contentWindow.scrollTo(0, 300);' % iframe) "iframe" 是局部变量。错误。 selenium.common.exceptions.WebDriverException:消息:预期的表达式,得到'
    • 您需要滚动到 iframe 中的特定位置吗?在 iframe actions = ActionChains(driver) actions.move_to_element(menu) 中移动并将焦点移到元素上怎么样
    • 我实际上是在尝试检索框架中的表数据。表有 27 行,但在获取页面源并解析它时,我只得到前 20 行数据。所以我需要滚动到表格末尾并获取页面源代码。
    【解决方案2】:

    我发现以下命令可以提供帮助。首先,假设已经切换到元素可访问的 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()
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2017-11-26
        • 2016-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多