【问题标题】:How to scroll on inner scrollbar on website using Python Selenium?如何使用 Python Selenium 在网站上滚动内部滚动条?
【发布时间】:2021-01-19 05:23:46
【问题描述】:

尝试在具有自己滚动条的框内滚动。尝试了很多方法,要么都失败了,要么不够好。

这是滚动条的html

<div id="mCSB_2_dragger_vertical" class="mCSB_dragger" style="position: absolute; min-height: 30px; display: block; height: 340px; max-height: 679.6px; top: 0px;" xpath="1"><div class="mCSB_dragger_bar" style="line-height: 30px;"></div></div>

当“顶部”值上升或下降时允许滚动。滚动条当然也会下降。一直试图模仿这一点,但无济于事

目前的一些尝试

    scrollbar =driver1.find_element(By.XPATH,"//aside[@class='sidebar mCustomScrollbar _mCS_2']//div[@class='mCSB_container']")

    A = ActionChains(driver1).move_to_element(scrollbar)
    A.perform()
    A = ActionChains(driver1)
    A.perform()
    W = driver1.find_element(By.CSS_SELECTOR,".visible-list > .row:nth-child(4)> .investment-item")
    W.location_once_scrolled_into_view

    scrollbar =driver1.find_element(By.XPATH,"//aside[@class='sidebar mCustomScrollbar _mCS_2']//div[@class='mCSB_container']").get_attribute("style")
    newscrollbar = str(scrollbar).replace("top: 0px","top: -100px")
    driver1.find_element(By.XPATH,"//aside[@class='sidebar mCustomScrollbar _mCS_2']//div[@class='mCSB_container']").__setattr__("style",newscrollbar)

    scrollbar =driver1.find_element(By.XPATH,"//aside[@class='sidebar mCustomScrollbar _mCS_2']//div[@class='mCSB_container']").get_attribute("style")
    newscrollbar = str(scrollbar).replace("top: 0px","top: -100px")
    A = driver1.create_web_element(newscrollbar)
    driver1.find_element(By.XPATH,"//aside[@class='sidebar mCustomScrollbar _mCS_2']//div[@class='mCSB_container']").__setattr__("style",A)

我尝试了更多方法,但无济于事..此链接提供了更多详细信息

Using scrollbar by editing attribute using Selenium Python

提前谢谢你

这里有一些方法可以看到它

    A = driver1.find_element(By.XPATH,"//div[@id='mCSB_2_dragger_vertical']").get_attribute("style")
    print(A) #willReturnEg:position: absolute; min-height: 30px; display: block; height: 537px; max-height: 855px; top: 0px;
    Newstyle = str(A).replace("top: 0px;","top: 80px;")
    driver1.__setattr__("style",Newstyle)

获取样式属性。 将其更改为字符串,例如:...top:100px...。 设置网站上的帖子属性

【问题讨论】:

    标签: python html css selenium-webdriver


    【解决方案1】:

    我上周一直在尝试纠正这个问题,最后使用 Selenium IDE 扩展并记录和播放滚动部分。在 Python 中导出脚本,下面几行帮助我解决了它。

    您要查找的命令是 click_and_hold()

        element =driver.find_element_by_xpath(final_xpath)
    #final_xpath=xpath of scroll bar arrow button, mostly an img, or something like isc_B7end
        actions = ActionChains(driver)
        actions.move_to_element(element).click_and_hold().perform()
    

    【讨论】:

    • 有趣。感谢您的见解:)
    【解决方案2】:

    您需要确保引用正确的元素,所以,也许这就是您尝试了各种方法但没有一种方法适合您的原因。一旦您确定您拥有正确的元素句柄,那么使用正确的元素选择进行操作就很容易,我会使用 javascript 。只需插入此代码:

     xpath_element = "//aside[@class='sidebar mCustomScrollbar _mCS_2']//div[@class='mCSB_container']"
     fBody = driver1.find_element_by_xpath(xpath_element)
     scroll = 0
     while scroll < 3:  # this will scroll 3 times
         driver1.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;',
                                     fBody)
          scroll += 1
          # add appropriate wait here, of course. 1-2 seconds each
          sleep(2)
    

    【讨论】:

    • 它不会抛出任何错误,但也不会做任何事情,哈哈
    • 您选择的 xpath_element 是错误的。如果可能,尝试各种方法,您可以通过 ID 获取该元素(首选访问方式,因为它是唯一的),或者只是尝试使用完整的 xpath。顺便问一下,你用的是 Webdriver/Chrome 吗?
    猜你喜欢
    • 1970-01-01
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2012-04-04
    • 2020-03-04
    相关资源
    最近更新 更多