Home > Software engineering >  How to find the xpath of the scroll bar/button?
How to find the xpath of the scroll bar/button?

Time:05-23

I'm using selenium to open up an instagram page and scrolling through a list of followers. However, I'm having issues with this line of code here: self.driver.find_element_by_xpath("/html/body/div[1]/section/nav/div[2]/div/div/div[3]/div/div[5]/span").click()

The scroll bar/button element's xpath isn't correct, and I don't know how to find the correct xpath.

Also, this was someone else's code that I'm trying to run LOL-I didn't write all the code from scratch.

CodePudding user response:

Selenium Scrolling

There are multiple ways to scroll down a webpage in Selenium in order to load new information. The simplest and most effective way I have found was using the Selenium.Keys method:

from selenium.webdriver.common.keys import Keys

body = driver.find_element_by_css_selector('body')
body.click()
body.send_keys(Keys.PAGE_DOWN)

More Information

I highly recommend you go through this guide if you have not already:

https://selenium-python.readthedocs.io/getting-started.html

CodePudding user response:

I would suggest 3 options here (examples are in Java):

  1. If you know exactly element what needs to become in viewport you can use following

    WebElement el = driver.findElement(By.xpath("xpath"));

    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView()",el);

  2. Use sendKeys method mentioned here already WebElement el = driver.findElement(By.xpath("//body")); el.sendKeys(Keys.PAGE_DOWN)

  3. The most tricky option, find element which has css property 'overflow' with value that makes scroll bar to appear e.g. overflow: 'auto' and using JavaScript you will be able to know exactly scrolling point and scroll in any direction as you like

    WebElement el = driver.findElement(By.xpath("xpath"));
    JavascriptExecutor js = (JavascriptExecutor) driver;
    int scrollAmountInPx = 10;
    js.executeScript("arguments[0].scrollTo(0, arguments[0].scrollTop " scrollAmountInPx ")",el);

  • Related