Reputation: 141
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
Upvotes: 6
Views: 12783
Reputation: 511
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,
)
Upvotes: 0
Reputation: 97
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()
Upvotes: 3
Reputation: 337
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)
Upvotes: 8