Home > Mobile >  JavascriptExecutor is erroring out
JavascriptExecutor is erroring out

Time:10-06

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

CodePudding user response:

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.

CodePudding user response:

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;")
  • Related