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.
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';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); }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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
