CodeGym /Courses /Python SELF EN /Automating Page Scrolling and Link Navigation

Automating Page Scrolling and Link Navigation

Python SELF EN
Level 36 , Lesson 1
Available

1. Automatic Page Scrolling

Imagine you're stuck in the ever-dreaded yet oddly familiar social media feed, where content just lazily loads as you scroll. Turns out, this magical scrolling has an API you can automate with Selenium.

The execute_script() Method

The execute_script() method in Selenium allows you to run JavaScript code on a page, making it a powerful tool when it comes to scrolling. Let's see how to use it for page scrolling.

Python

from selenium import webdriver
import time

# Initialize the browser driver
driver = webdriver.Chrome()

# Open the target page
driver.get('https://example.com/scrolling_page')

# Scroll the page down
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)  # Wait for the content to load

# Scroll the page up (for variety)
driver.execute_script("window.scrollTo(document.body.scrollHeight, 0);")

# End the session
driver.quit()

In this script, we're using window.scrollTo() to scroll the page. The arguments (0, document.body.scrollHeight) specify scrolling from the top to the bottom of the page. It's a simple and effective way to ensure all dynamic elements are fully loaded.

Why Does It Matter?

Using execute_script() to scroll the page lets you load content that might initially be hidden, such as on infinite scroll pages. This is especially handy for social media feeds and news sites where content loads dynamically.

2. Navigating Links

Now that we've nailed scrolling, it's time for the next task—navigating links automatically. This is a must-have skill for data scraping, especially when info is spread across multiple pages.

Basics of Link Navigation

To click on a link, we can use the click() method on the selected element. Here's a simple example:

Python

from selenium import webdriver

# Initialize the browser driver
driver = webdriver.Chrome()

# Open the target page
driver.get('https://example.com/links_page')

# Find a link by its text and click it
link = driver.find_element_by_link_text('Next Page')
link.click()

# End the session
driver.quit()

In this example, we locate a link by its text content. But what if the text isn't unique? In such cases, you can use more precise methods like find_element_by_xpath() or find_element_by_css_selector().

Python

# Find a link by XPath
link = driver.find_element_by_xpath('//a[@href="/next_page"]')
link.click()

Advantages of Automating Link Navigation

Navigating links with Selenium automates the process of collecting data from pages with pagination or when info is distributed across multiple subsections of a site. It's great for exploring search results or browsing product catalogs on e-commerce sites.

3. Combining Scrolling and Navigation

Now imagine you need to scroll the page to find a link or element, and then navigate to another page. This is a combined process that can also be automated.

Example of a Combined Script

Python

from selenium import webdriver
import time

# Initialize the browser driver
driver = webdriver.Chrome()

# Open the target page
driver.get('https://example.com/scroll_and_click')

# Scroll the page to load hidden elements
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)

# Find and click the desired link
link = driver.find_element_by_xpath('//a[text()="Load More"]')
link.click()

# End the session
driver.quit()

What Could Go Wrong?

When working with dynamic pages, sometimes elements might not load in time or fully. In such cases, you'll need to use wait methods to ensure the elements you want to interact with are ready.

Python

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Wait until the element becomes clickable
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//a[text()="Load More"]'))
)
element.click()

Using wait methods helps avoid errors caused by unavailable elements and ensures a more stable script execution.

1
Task
Python SELF EN, level 36, lesson 1
Locked
Scrolling a Page with Selenium
Scrolling a Page with Selenium
2
Task
Python SELF EN, level 36, lesson 1
Locked
Navigating Links on a Web Page
Navigating Links on a Web Page
3
Task
Python SELF EN, level 36, lesson 1
Locked
Combined Scroll and Link Click Automation
Combined Scroll and Link Click Automation
4
Task
Python SELF EN, level 36, lesson 1
Locked
Handling dynamic pages with wait conditions
Handling dynamic pages with wait conditions
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION