Scroll to Bottom of Page Not Working (Python Selenium) - python

Why doesn't driver.execute_script("window.scrollTo(0, document.body.scrollHeight)") work on this apple music playlist? That line of code works fine on every other website I visit.
I tried doing action.send_keys_to_element(driver.find_element(By.TAG_NAME, 'html'), Keys.END) to no avail.
How else could I scroll to the bottom of the page?

Try to use any interactable node like below:
driver.find_element(By.CLASS_NAME, 'play-button').send_keys(Keys.END)

Related

Selenium not recognizing the xpath

On Python 3.9 and Selenium 4.00
Hi there, I'm currently trying to automate downloading a few things on Chrome. I got the login part and navigating to the page down and it works properly. I'm having issues with the next part which is clicking "export" then "export as csv". I hover over the HTML source code and it highlights the buttons I need to press so I hit "copy XPath" but selenium won't press it and I get this error.
Edit: I cannot share the site as it is locked behind a login and it is not my login to give out; end of edit.
Message: invalid selector: Unable to locate an element with the xpath expression //*[#id="report_nav_menu"]/ul/li[2]/a"
Here's my code
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('website')
driver.find_element(By.XPATH, '//*[#id="report_nav_menu"]/ul/li[2]/a').click()
time.sleep(1) # makes sure the page loads
driver.find_element(By.XPATH, '//*[#id="report_nav_menu"]/ul/li[2]/ul/li[6]/a').click()
time.sleep(1000) # to keep the browser open
This the is HTML source code:
Source code
The first highlight in the pic is for the Export button.
Need to click this first
The second highlight shows that it's for the CSV button.
Need to click this second
//class[#elname="zc-navmenuEl/button[2] seems to be an invalid XPath expression.
I can't see this locator used in the code you presented in the question.
Also you didn't share an URL of the page you are working with so I can't determine the correct element locator.

Having trouble scrolling down to element in Selenium, python

I'm trying to get Selenium to scroll down on a page and then click on a button (the arrows on the bottom right under the "Add+") , but I keep getting a NoSuchElementException error. Here's what I've tried, but still nothing is working.
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
element = driver.find_element_by_xpath('/html/body/div[7]/div/div/main/section[6]/main/div/main/div[2]/section[1]/p/button')
element.click()
Any help is appreciated.
You are using
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
which is basically, to scroll to the bottom of the page.
Please use this :
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
this will basically scroll into the element view and element will be available in Selenium view port.

How to get new page source after navigating Python Selenium

I am facing an issue.
I navigate on the page via Selenium Chrome. I have timeouts and WebDriverWait as I need a full page to get JSON out of it.
Then I click the navigation button with
driver.execute_script("arguments[0].click();", element)
as normal click never worked.
And it is navigating OK, I see Selenium is surfing normally. No problem.
But the driver.page_source remains for the first page that I got via 'get' method
All timeouts are the same as for the first page. And I see those new pages normally, but the page_source never updates.
What am I doing wrong?
After navigating to the new Page, you need to get the current URL by:
url = driver.current_url()
and then:
driver.get(url)
driver.getPageSource()

Selenium PhantomJS Scrolling Down

I tried to scrolling down with selenium, But i use webdriver PhantomJS. I tried selenium for test javascript instagram. As you know, on instagram there have button "Load More", after click button "Load More", we don't have to click again becouse it will auto refresh and it will show more image.
I tried like this :
driver.find_element_by_xpath("//a[#class='_8ioip _glw1t']").click()
time.sleep(5)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3.6)
Actually this code it's work, but not really work, i mean something weird happening at the "time.sleep". If i give 2, the result are few and if i give 3 the result are just pretty much, But if i give 5, the result same like i give 2.
The question is, How to make teh Scroll get all data
class of "Load more" button seems to have changed.
your code is correct, try with this xpath:
//a[#class='_8imhp _glz1g']
or try to locate the button by it's text content:
//*/div/a[text()[contains(.,'Load more')]]
and before click the button scroll down.. so your code should looks like:
driver.get('https://www.instagram.com/explore/tags/whatever/')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.find_element_by_xpath("//*[text()[contains(.,'Load more')]]").click()
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

scroll webpage using selenium webdriver in python

I want to scroll a webpage upto a certain length I am trying to scroll the webpage with this line of code
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
It is executing very well but this code scroll the webpage till last. But I want to scroll till 90% of the page. Any idea how to approach this kind of problem. A little advice will also be appreciated. Thanks!

Categories