5

I'm trying to do a simple Python Selenium automation where the script will click a link which opens a dialog box on top of the page (Instagram profile).

The said dialog box will display the list of followers but unfortunately the UL that contains the list will only display the first 12 followers (or LI). It is powered by AJAX to "load more" followers.

Anyway, to simulate loading more followers, i tried this code:

driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/ul').send_keys(Keys.END)

or

driver.find_element_by_tag_name('body').send_keys(Keys.END)

unfortunately, it doesn't work. I was wondering if there is a correct way to do this (scrolling down focused on the active div or any page element)?

Image below is provided to show the html structure of the said page.

enter image description here

Appreciate your help on this, thank you very much!

Ben Daggers
  • 1,000
  • 1
  • 16
  • 51

2 Answers2

5

Can you try something like this?. This will scroll to the div element that you have mentioned.

Python Code

element = driver.find_element_by_xpath("xpath_of_div_element")
driver.execute_script("arguments[0].scrollIntoView(true);", element);

Java sample:

WebElement element = driver.findElement(By.id("id_of_div_element"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Yosuva Arulanthu
  • 1,444
  • 4
  • 18
  • 32
2

Use this

liElement = driver.find_element_by_xpath("//div[@role='dialog']//ul//li[text()='required_li_element_visible_text']")
driver.execute_script("arguments[0].scrollIntoView(true);", liElement);
Pritam Maske
  • 2,670
  • 2
  • 22
  • 29
  • i got this error: raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@role='dialog']//ul//li[text()='required_li_element_visible_text']"} (Session info: chrome=77.0.3865.90) – Ben Daggers Oct 13 '19 at 05:23
  • never mind, fixed the minor issue. Thanks for your help Sir, have a great day! – Ben Daggers Oct 13 '19 at 05:34