Python Selenium:无法在iframe中滚动 [英] Python Selenium: Unable to scroll inside iframe

查看:67
本文介绍了Python Selenium:无法在iframe中滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够在标签之间切换,访问所有元素.我无法在此iframe中滚动.请帮忙.我正在使用的代码如下.

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)

推荐答案

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

I found the following commands 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()

这篇关于Python Selenium:无法在iframe中滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆