Ashish
Ashish

Reputation: 479

JavascriptExecutor is erroring out

I am trying to load a page ins selenium. After that, I need to scroll to the end of the page.

For that, I am trying this

import org.openqa.selenium.JavascriptExecutor
driver.get(<My URL>)
JavascriptExecutor js = (JavascriptExecutor) driver
js.executeScript("window.scrollBy(0,document.body.scrollHeight)")

But it is not working and showing syntax error

enter image description here

Upvotes: 1

Views: 101

Answers (2)

cruisepandey
cruisepandey

Reputation: 29362

to scroll all the way down, you can invoke execute_script like below:

driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")

Upvotes: 0

Tal Angel
Tal Angel

Reputation: 1782

No, you don't use your code to inject Javascript into the webpage. Selenium can click on elements like a human user clicks the mouse. Selenium can also mimic keyboard typing, and that is what you need to do here:

from selenium.webdriver.common.keys import Keys
body = driver.find_element_by_tag_name('body')
body.send_keys(Keys.END)

If Keys.END does not take you all the way down - use it inside of a loop, or use Keys.PAGE_DOWN.

Upvotes: 1

Related Questions