Master Selenium: Quick Tricks to Replace Date Pickers and Auto‑Scroll Dynamic Pages

This article demonstrates how to enhance Selenium web‑scraping scripts by using JavaScript to directly modify date picker values and to automatically scroll dynamic pages, providing concise code examples that overcome common challenges when crawling sites like 12306 and Tencent Video.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Selenium: Quick Tricks to Replace Date Pickers and Auto‑Scroll Dynamic Pages

1. Introduction

Selenium is a tool for testing web applications. It runs in the browser like a real user and is essential for crawling complex dynamic pages. It supports browsers such as IE, Firefox, Safari, Chrome, and Opera.

2. Scenario One: Replacing Date Picker Values

Using the 12306 website as an example, the usual method is to locate the date element and click it. After the calendar layout changes, the script may fail. By directly modifying the element’s value attribute with JavaScript, the problem is solved with only three lines of code.

document.getElementById('date_input').value = '2023-01-01';
12306 date picker click
12306 date picker click
Inspecting the date input element
Inspecting the date input element
JavaScript code to change value attribute
JavaScript code to change value attribute

3. Scenario Two: Automatic Page Scrolling

Some dynamic pages, such as the Tencent Video homepage, require scrolling to reveal all elements. A small JavaScript snippet can scroll the page in ten steps, making every element visible.

for (let i = 1; i <= 10; i++) { window.scrollTo(0, document.body.scrollHeight * i / 10); }
Tencent video homepage requiring scroll
Tencent video homepage requiring scroll
JavaScript code for automatic scrolling
JavaScript code for automatic scrolling

4. Conclusion

Embedding JavaScript in Selenium scripts can solve many crawling challenges. The two examples shown are just a start; consider JavaScript whenever Selenium alone is insufficient. All code is available on GitHub; reply with “selenium” to receive the repository link.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptPythonSeleniumDynamic Pages
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.