如何使用Python Selenium在网站的内部滚动条上滚动?

2024-05-15 17:01:31 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试在具有自己的滚动条的框中滚动。尝试了许多方法,但都失败了或不够好

这是滚动条的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)

获取样式属性。 将其更改为字符串,例如:…顶部:100px。。。。 在网站上设置或发布属性


Tags: divbystyletopelementfindxpathclass
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)

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

您要查找的命令是单击并按住()

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

相关问题 更多 >