How to Scroll a Web Page Using Selenium Webdriver in Python

How to “scroll down” some part using selenium in python?

Because the scroll is actually inside an element, all the javascript command with window. will not work. Also the element is not interactable so Key down is not suitable too.
I suggest using scrollTo javascript executor and set up a variable which will increase through your loop:

element = driver.find_element_by_xpath('//div[@id="container1"]')
time.sleep(10)

verical_ordinate = 100
for i in range(0, 50):
print(verical_ordinate)
driver.execute_script("arguments[0].scrollTop = arguments[1]", element, verical_ordinate)
verical_ordinate += 100
time.sleep(1)

I have tested with chrome so it should work.

Reference

https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop

How can I scroll down using selenium

Tried with the below code, it did scroll.

driver.get("https://webtoon.kakao.com/content/%EB%B0%94%EB%8B%88%EC%99%80-%EC%98%A4%EB%B9%A0%EB%93%A4/1781")
time.sleep(2)
options = driver.find_element_by_xpath("//div[@id='root']/main/div/div/div/div[1]/div[3]/div/div/div[1]/div/div[2]/div/div[1]/div/div/div/div")
driver.execute_script("arguments[0].scrollIntoView(true);",options)

How to scroll down in Python Selenium step by step

Agree with answer of @Rahul Chawla.

But adding one change. You can try this one

driver = webdriver.Chrome()

read_mores = driver.find_elements_by_xpath('//a[text()="Read More..."]')

for read_more in read_mores:
driver.execute_script("arguments[0].scrollIntoView();", read_more)
driver.execute_script("$(arguments[0]).click();", read_more)

Is there a way to scroll up a particular div element in a webpage using selenium with PYTHON?

Yes, there are multiple ways using which you could scroll to particular element

By using the move_to_element()

 element = driver.find_element_by_xpath("_element_")

action = ActionChains(driver)
action.move_to_element(element).perform()

By scrollIntoView()

 element = driver.find_element_by_xpath("_element_")
driver.execute_script("arguments[0].scrollIntoView(true);", element)

For reference check here



Related Topics



Leave a reply



Submit