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

标签 python html css selenium-webdriver

试图在有自己滚动条的框中滚动。尝试了很多方法,要么失败,要么不够好。
这是滚动条的 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....
网站上的 setor post 属性

最佳答案

你需要确保你引用了正确的元素,所以,也许这就是你尝试了各种方法但没有一个适合你的原因。一旦您确定您拥有正确的元素句柄,那么使用正确的元素选择进行操作就很容易了,我会使用 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)

关于python - 如何使用 Python Selenium 在网站上的内部滚动条上滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64190082/

相关文章:

python - 使用 PySide2 在 Qt-Designer 中嵌入 PyQtGraph

python - 如何让 Python 多线程管道使用 90% 的可用内存?

javascript - 在 Safari 中失去内容可编辑的焦点

html - 如何在 div 中强制/包装 <img>?

html - CSS 背景图片不会消失

javascript - 如何设置无法编辑 HTML 且 ID 不断变化的元素的占位符文本?

python - 如何在 python 中使用 append 和 pickle?

html - 在 React 的 onFocus 期间区分键盘和鼠标事件

css - 覆盖多个 LESS 文件中的变量

python - 对数组执行二重积分