Skip to main content

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.

🚀 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

✅ 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).

jQuery Integration

  • You can use the tab.jq or tab.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 or tab.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 and tab.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())

🔧 Installation

pip install seleniumtabs

Or with Poetry:

poetry add seleniumtabs

📋 Requirements

  • Python 3.13+
  • Chrome or Firefox browser
  • Selenium WebDriver

🔍 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

🤝 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

seleniumtabs-2.0.0.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

seleniumtabs-2.0.0-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file seleniumtabs-2.0.0.tar.gz.

File metadata

  • Download URL: seleniumtabs-2.0.0.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.6.87.1-microsoft-standard-WSL2

File hashes

Hashes for seleniumtabs-2.0.0.tar.gz
Algorithm Hash digest
SHA256 98c886f2ef145464ffa985451843311af4e179cb3a9c37c342c2fe424ac912ec
MD5 97619e46ce6cf5233d32ebdaf8bbc3c4
BLAKE2b-256 771a0209e8ccc8cfbff2dae9ba18b1ad36a631b1318724537dcbe7484898194e

See more details on using hashes here.

File details

Details for the file seleniumtabs-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: seleniumtabs-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.12 Linux/6.6.87.1-microsoft-standard-WSL2

File hashes

Hashes for seleniumtabs-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d9789895ea9e2f837af5982c6fa65b5dce77d5024683194e9ae6dd6c30f0a8e
MD5 d32b20ebc65c8bb0537f91426b89a924
BLAKE2b-256 8194f770babb3aae3b756b4168d162d14b742ec7e2ef65b12eae7936e26c7e94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page