Selenium with tab management in Python
Project description
Selenium Tabs
A Python library that makes browser automation with Selenium more intuitive by providing a tab-centric interface. Manage multiple browser tabs with ease, perform common operations, and interact with web elements using a clean, Pythonic API.
And guess what! You don't need to manually download drivers for the browser—it's all taken care of by the library. Install and start right away.
Note: All testing has been done on Chrome.
🚀 Top Features
- Tab Management: Open, close, switch, and query tab state with ease.
- Element Selection: Use CSS selectors, jQuery, and PyQuery for powerful element querying.
- Page Interaction: Scroll, click, and wait for elements with robust error handling.
- Advanced Features: Direct driver access, task scheduling, and more.
- Automatic Driver Management: No need to manually download or manage browser drivers.
🔧 Installation
pip install seleniumtabs
Or with Poetry:
poetry add seleniumtabs
📋 Requirements
- Python 3.13+
- Chrome or Firefox browser
🚀 Quick Start
from seleniumtabs import Browser
# Create a browser instance and open multiple tabs
with Browser(name="Chrome", implicit_wait=10) as browser:
# Open multiple websites in different tabs
google = browser.open("https://google.com")
yahoo = browser.open("https://yahoo.com")
# Work with tabs
print(f"Current tab: {browser.current_tab}")
print(f"First tab: {browser.first_tab}")
print(f"Last tab: {browser.last_tab}")
# Switch between tabs
yahoo.switch()
google.switch()
# Close a tab
yahoo.close()
✨ Key Features
1. Tab Management
with Browser(name="Chrome") as browser:
# Open tabs
tab1 = browser.open("https://example.com")
tab2 = browser.open("https://example.org")
# Access tab properties
print(tab1.title) # Page title
print(tab1.url) # Current URL
print(tab1.domain) # Domain name
# Check tab state
print(tab1.is_active) # Is this the active tab?
print(tab1.is_alive) # Is the tab still open?
2. Element Selection
with Browser(name="Chrome") as browser:
tab = browser.open("https://example.com")
# CSS Selectors
elements = tab.css("div.class-name")
for element in elements:
print(element.text)
# Chaining selectors
main_content = tab.css("main")[0]
links = main_content.css("a")
# Get element attributes
for link in links:
print(link.get_attribute("href"))
3. Page Interaction
with Browser(name="Chrome") as browser:
tab = browser.open("https://example.com")
# Scrolling
tab.scroll_down(times=3) # Scroll down 3 times
tab.scroll_up(times=2) # Scroll up 2 times
tab.scroll_to_bottom() # Scroll to page bottom
# Clicking elements
element = tab.css("button")[0]
tab.click(element)
# Wait for elements
tab.wait_for_presence("div", "class-name", wait=10)
tab.wait_for_visibility("button", "submit", wait=5)
4. Advanced Features
with Browser(name="Chrome") as browser:
tab = browser.open("https://example.com")
# jQuery-like operations
jq_elements = tab.jq("div.class-name")
# PyQuery for HTML parsing
pq = tab.pyquery
text_content = pq.text()
# Run JavaScript
result = tab.run_js("return document.title")
# Schedule tasks
def refresh_page(tab):
tab.refresh()
tab.schedule_task(refresh_page, period=60) # Refresh every 60 seconds
Direct Driver Access
-
If a feature is not directly available in the
Tab
object, you can access the underlying Selenium WebDriver directly viatab.driver
. -
Example:
# Access driver methods directly tab.driver.execute_script("return document.title")
-
Fallback Behavior: If you call a non-existent method on the
Tab
object, an attempt is made to querytab.driver.method_name
via__getattribute__
.
jQuery Integration
- You can use the
tab.jq
ortab.jquery
property to run jQuery-like selectors and operations directly on the live browser DOM. - Tested features:
- Selecting elements with jQuery syntax.
- Chaining jQuery selectors.
- Accessing and manipulating attributes and text.
- Interacting with elements (e.g., click, scroll).
Example:
jq_elements = tab.jq("div.class-name")
for el in jq_elements:
print(el.text)
PyQuery Integration
- You can use the
tab.pyquery
ortab.pq
property to parse and query the current page's HTML snapshot using PyQuery (lxml-based). - Tested features:
.items()
for iterating over selections and sub-selections..find()
,.parent()
,.children()
for DOM navigation..attr()
,.text()
,.html()
for attribute and content access.- Consistency between
tab.pyquery
andtab.pq
. - Integration with real-world pages (e.g., Yahoo).
Example:
pq = tab.pyquery
for link in pq("nav a").items():
print(link.attr("href"))
print(pq("title").text())
🔍 Browser Options
with Browser(
name="Chrome", # Browser name ("Chrome" or "FireFox")
implicit_wait=10, # Default wait time for elements
headless=False, # Run browser in headless mode
full_screen=True, # Start browser in full screen
page_load_timeout=60 # Page load timeout in seconds
) as browser:
# Your code here
✅ Tested Features
This library is thoroughly tested for:
- Tab Management: Opening, closing, switching, and querying tab state.
- Element Selection: CSS selector queries, chaining, and attribute access.
- Page Interaction: Scrolling, clicking, and waiting for elements.
- Page Loading: Full and partial page load handling, including timeouts.
- Tab Refresh: Full and partial refresh with robust error handling.
- Task Scheduling: Periodic tab actions (e.g., auto-refresh).
- Integration with jQuery and PyQuery (see below).
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file seleniumtabs-2.1.1.tar.gz
.
File metadata
- Download URL: seleniumtabs-2.1.1.tar.gz
- Upload date:
- Size: 24.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
e4fed8a463e9df9011dabe4d7736ce60cad2d2f9390a7280e2ee63cd9fc22f6b
|
|
MD5 |
9711c9ed6f31ac61d438c4c788ba08e4
|
|
BLAKE2b-256 |
4ed4eff5168a6f523c08941637528e4b2e76d286886d86509daac9ac7ece90fa
|
File details
Details for the file seleniumtabs-2.1.1-py3-none-any.whl
.
File metadata
- Download URL: seleniumtabs-2.1.1-py3-none-any.whl
- Upload date:
- Size: 26.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.6.87.2-microsoft-standard-WSL2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
eb2ddb4f8244599c9c3d5f7e465854b4cf0c372d762da422d69cf767502de245
|
|
MD5 |
a2c53be28c3b8f9c74040ace0097ee0f
|
|
BLAKE2b-256 |
6ba0a13bf1ea6b5c5c1b3de36328c72afba27b000d46a83523cb38d6e31d97cb
|