6

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!

2 Answers 2

7

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This isnt working for me. I have to way down to the div and no it doesnt go.
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);

2 Comments

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)
never mind, fixed the minor issue. Thanks for your help Sir, have a great day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.