0
votes

I'm using python Selenium webdriver to open a pdf online and I want to scroll it. Which function should I use?

I already try to use the function driver.execute_script("window.scrollTo(0, 1000)") [as suggested in this stack overflow question: How can I scroll a web page using selenium webdriver in python?]

This is my code:

driver.get("http://www.pdf995.com/samples/")

element = driver.find_element_by_xpath("//a[@href='pdf.pdf']")
element.click() 

driver.execute_script("window.scrollTo(0, 1000)") 

I expect to scroll down (as normally it does e.g. when I tape driver.get("https://www.google.it/search?client=opera&q=google&sourceid=opera&ie=UTF-8&oe=UTF-8") but actually the page remains fixed.

Does anyone have an advice for me?

1

1 Answers

0
votes

First of all you can try using a CSS Selector instead of the XPath, just to make easier to locate through JS, for this case, both are pretty similar

PDF_CSS_LOCATOR = "a[href='pdf.pdf']"
driver.get("http://www.pdf995.com/samples/")
element = driver.find_element_by_css_selector(PDF_CSS_LOCATOR)
element.click()

Then, you should interact with it using

driver.execute_script(f"document.querySelector({PDF_CSS_LOCATOR}).scrollTo(0, 1000)")