Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll on inner scrollbar on website using Python Selenium?

Trying to scroll within a box that has its own scrollbar. Tried numerous ways all have either failed or were not good enough.

heres the html of the scrollbar

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

when the "top" value goes up or lower allows for scrolling .the scrollbar ofcourse goes down aswell ..been trying to mimic this but to no avail

some attempts so far

    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)

Iv tried alot more methods but to no avail ..theres some more details from this link

Using scrollbar by editing attribute using Selenium Python

Thankyou in advance

Heres some way to see it

    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)

Get style attribute. Change it as a string to eg: ...top:100px.... setor post attribute on website

like image 415
Hamza Arshad Avatar asked Sep 08 '26 19:09

Hamza Arshad


2 Answers

You need to make sure you reference the right element, so, maybe this is the reason you've tried various methods and none are working for you. Once you're certain you have the right handle to your element, then manipulate using the right element selection is easy, I'd use javascript for that. Just plug this code:

 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)
like image 66
QRrabbit Avatar answered Sep 10 '26 10:09

QRrabbit


I have been trying to rectify this issue from past week, finally using Selenium IDE extension and recorded and played back the scroll part. Exporting the script in Python, below lines helped me solve it.

Command you are looking for is 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()
like image 28
Priyal Mangla Avatar answered Sep 10 '26 09:09

Priyal Mangla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!