How to scroll on inner scrollbar on website using Python Selenium?
Asked Answered
P

3

6

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

Prolactin answered 4/10, 2020 at 0:11 Comment(0)
B
8

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)
Bandanna answered 4/10, 2020 at 5:49 Comment(4)
it somehow doesnt throw any errors but doesnt do anything either lolProlactin
than your xpath_element you're selecting, is wrong. Try various methods if possible, can you get that element by ID (preferred way of accessing, since it is unique), or just try with a full-xpath. By the way, are you using Webdriver/Chrome?Bandanna
working great for me, thank you for this!Reheat
Have searched through many SO threads for something that works for me---THANK YOUMonty
O
3

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()
Olszewski answered 7/10, 2020 at 12:45 Comment(1)
Interesting. Thanks for this insight :)Grasp
M
0

Building off @QRabbit's answer. If you want to scroll immediately to the bottom of the inner window, you can just determine the scrollHeight of the element in question.

I wanted to scroll to the bottom of LI Sales Navigator search results and used the code below.

inner_window = driver.find_element(By.ID, "search-results-container")

driver.execute_script(
    'arguments[0].scrollTop = arguments[0].scrollTop + document.getElementById("search-results-container").scrollHeight;',
    inner_window,
)
Monty answered 15/9, 2022 at 19:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.