Selenium Python在IE中向下滚动页面使用window.scroll对我不起作用

时间:2016-02-18 16:46:55

标签: python python-2.7 selenium selenium-webdriver webdriver

我正在尝试使用IE中的滚动条向下滚动页面。我正在使用

self.driver.execute_script("window.scrollTo(0, 10000);")

当我运行我的测试并且它到达页面时它不会滚动。没有显示错误,页面只是不滚动。 我可以手动滚动滚动条页面。我的Selenium Python脚本不会滚动它。

我要滚动的代码段是:

def scroll_up_and_down_using_the_scrollbar(self): # Using the scrollbar on the Data Previews view page scroll up and down the page
    #self.driver.execute_script("window.scrollTo(0, 10)")
    # scroll down the page iteratively with a delay
    for _ in xrange(0, 10 + 1):
        self.driver.execute_script("window.scrollTo(0, 10000);")
        time.sleep(2)

如果我使用Firefox并检查滚动条,我可以看到一些HTML。 在使用滚动条之前,是否需要获取滚动条的元素?

HTML是:

<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; top: 16px; right: 0px; bottom: 16px; width: 16px;">
    <div class="GJPPK2LBI" tabindex="0" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
        <input type="text" tabindex="-1" role="presentation" style="opacity: 0; height: 1px; width: 1px; z-index: -1; overflow: hidden; position: absolute;"/>
            <img class="GJPPK2LBH" border="0" style="width: 16px; height: 11px; background: transparent url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAALCAYAAAB24g05AAABNklEQVR42o3SvU/CQBjHcf4N/klXXTRu4mIiTjroxgh9iSaUggP2JTHVFgYbaBls2rJIWjoh1J/PXUqFBIKXfPIkl963TXqVSrFOr25xXKsfxJ67uGugsrlOLm9QrYI2gVazyQmCUBAhiCJEIkkSkSHLMh6k3l/k6KzGDzPttsIpioJOp1NQoaoqut21LlrP5u5Av9/nNE3jdJ3ROdMwYJgGTNPE44u1O2BZFqw3C7b9Dtux4ThOaTAYcEOyFTiv35cB98OF67oYjcYYE29c8Dziw/fJZLIduG5IZeAzCBCQKAwRRiGiKCIxn/F0imlMaO4NzGYz8oUkSZCSJGUzRZqmmM/nJEOWZfsDi8WC+14uudWK8LlCnjM58p8cT9pGgP3T9T347+q9DrcvE9tgn3UIe/Pm4V+cw+BxAioP1AAAAABJRU5ErkJggg==") no-repeat scroll 0px 0px; top: 0px;" src="http://justin-pc.infoshare.local:8080/clearcore501/ClearCore/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";"/>
    </div>
</div>

我的TestCase调用滚动条方法:

class DataPreviewsPage_TestCase(BaseTestCase):

def test_a3_view_preview_crminvalid_odbc_and_test_scrollbar_up_and_down(self):
    print "*** Test view_preview_crminvalid_odbc_and_test_scrollbar_up_and_down ***"
    tool_bar = ToolbarPage(self.driver)
    time.sleep(1)
    data_previews_page = tool_bar.clickDataPreviews()
    time.sleep(2)
    data_previews_view_page = data_previews_page.click_view_link(Globals.datapreview_crminvalid_odbc_name)
    time.sleep(10)
    data_previews_view_page.scroll_up_and_down_using_the_scrollbar()

如何点击滚动条在Selenium Python中向下滚动页面?

谢谢, 里亚兹

1 个答案:

答案 0 :(得分:1)

并不完全回答为什么scrollTo()在您的情况下不起作用,而是分享了帮助我在不同浏览器中向上和向下滚动的内容。

要向下滚动,请找到footer元素(或页面底部的某个元素),然后使用scrollIntoView()滚动到该视图中:

footer = driver.find_element_by_tag_name("footer")
driver.execute_script("arguments[0].scrollIntoView()", footer);