Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
inquilabee authored Sep 14, 2023
1 parent 374e701 commit 555b0bd
Showing 1 changed file with 72 additions and 149 deletions.
221 changes: 72 additions & 149 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,179 +1,102 @@
### Simple Selenium
# Simple Selenium: Enhanced Tab Management for Selenium

Selenium with Tab Management

<small> With all the already available flexibility and features of Selenium </small>
Simplify your web automation tasks with **Simple Selenium**, a Python package that takes Selenium to the next level with streamlined tab management and additional features.

### Installation

Install from PyPI
Install **Simple Selenium** from PyPI with ease:

```bash
pip install simpleselenium
```

### Core Idea

A `browser` has many `tabs`.

Action/activity on `Tab` object
- Check if the tab is alive (i.e. it has not been closed)
- Switch to a tab
- See/obtain page source, title and headings
- inject JQuery and select elements
- work on a specific tab
- click elements
- scroll (up or down)
- infinite scroll
In **Simple Selenium**, a `browser` contains multiple `tabs`, enabling you to interact with web pages more efficiently. You can perform various actions on both `Tab` and `Browser` objects, making web automation a breeze.

**Actions/Activities on `Tab` Objects:**
- Check tab status (open/closed)
- Switch to a specific tab
- Access page source, title, and headings
- Inject jQuery and select elements
- Perform actions on a specific tab (e.g., click, scroll)
- CSS selection made easy
- ... many more

Action/activity on `Browser` object
- Open a new tab with url
- Get a list of open tabs
- Get active tab
- Switch to a tab of the browser (first, last or any one).
- close a tab
- Close the browser
**Actions/Activities on `Browser` Objects:**
- Open new tabs with URLs
- Retrieve a list of open tabs
- Switch to a specific tab (first, last, or any)
- Close a tab
- Close the entire browser

### Working with driver objects
### Working with Driver Objects

`driver` object available on any `Tab` object.
A `driver` object is available on any `Tab` object, allowing you to access the browser/driver object and use Selenium methods when needed.

### Features

Some basic features are being listed below (also see the `Usage` section below):

- easy management of different tabs
- switching to a tab is super easy
- know if a tab is active (current tab) or alive
- closing a tab is easy as `browser.close_tab(tab_object)`
- Several (built-in) functions
- `tab.infinite_scroll()`
- `tab.scroll()`
- `tab.scroll_to_bottom()`
- `tab.click(element_on_page)`
- `tab.switch()` to focus on tab i.e. make it the active tab
- Can't find a way to use usual selenium methods? Use `Tab.driver` object to access the browser/driver object and use
accordingly
**Simple Selenium** offers a range of features to enhance your web automation tasks:
- Effortless tab management
- Seamless tab switching
- Real-time tab status tracking
- Convenient tab closure
- Built-in functions for scrolling, clicking, and more
- Access to the underlying Selenium `driver` object for advanced usage

### Usage

The best way to getting started with the package is to use the `Browser` object to start a browser and call `open`
method off it which returns a Tab object.
To get started with **Simple Selenium**, create a `Browser` object and open tabs using the `open` method:

#### Browser

```python

from simpleselenium import Browser, Tab

# The `name` argument is one of "Chrome" or "FireFox".
# The project has been configured to auto-download drivers.


with Browser(name="Chrome", implicit_wait=10) as browser:
google: Tab = browser.open("https://google.com")
yahoo = browser.open("https://yahoo.com")
bing = browser.open("https://bing.com")
duck_duck = browser.open("https://duckduckgo.com/")

# Scroll on the page

# yahoo.scroll_to_bottom()
# yahoo.scroll_down(times=2)
# yahoo.scroll_up(times=2)
# yahoo.scroll(times=2, wait=5)

# Working with tabs -- loop through it, access using index and so on

assert len(browser.tabs) == 4, err_msg # noqa
assert google in browser.tabs, err_msg # noqa
assert browser.tabs[0] == google, err_msg # noqa

for tab in browser.tabs:
print(tab)

print(browser.tabs)
print(browser.current_tab)

# Selecting elements with JQuery

print(yahoo.jq("a"))
print(yahoo.jquery("a")) # same as above

print(yahoo.jquery.find(".streams"))

for item in yahoo.jquery.execute("""return $(".stream-items a");"""):
result = yahoo.jquery.query(
script="""
return $(arguments[0]).text();
""",
element=item,
)

print(result)

# Selecting using CSS Selectors (no JQuery needed)

for item in yahoo.css(".stream-items"):
for a in item.css("a"):
print(a, a.text)

assert yahoo == browser.current_tab, err_msg # noqa

print(browser.first_tab)
assert google == browser.first_tab, err_msg # noqa

print(browser.last_tab)
assert duck_duck == browser.last_tab, err_msg # noqa

print(browser.last_tab.switch())
assert browser.current_tab == duck_duck, err_msg # noqa

# Some `Tab` attributes/properties

print(google.title)
print(google.url)

print(google.page_source)
print(google.page_html)

print(google.page_height)
print(google.user_agent)
print(google.is_active)
print(google.is_alive)

# Closing a tab

browser.close_tab(bing)
print(browser.tabs)

print(browser.current_tab)

# Switching to a tab

yahoo.switch()

print(browser.current_tab)

google.switch()

print(browser.current_tab)

browser.close_tab(yahoo)

print(yahoo.is_alive)
print(yahoo.is_active)

# Accessing the driver object

print(google.driver.title, google.title)
assert google.driver.title == google.title, err_msg # noqa

# Query using the powerful pyquery library

d = yahoo.pyquery # noqa
# Create a `Browser` object, specifying the browser name (e.g., "Chrome").
# The project automatically downloads drivers.

with Browser(name="Chrome", implicit_wait=10) as browser:
google: Tab = browser.open("https://google.com")
yahoo = browser.open("https://yahoo.com")
bing = browser.open("https://bing.com")
duck_duck = browser.open("https://duckduckgo.com/")

# Scroll on the page (example)
yahoo.scroll_to_bottom()

# Working with tabs
assert len(browser.tabs) == 4
assert google in browser.tabs
assert browser.tabs[0] == google

for tab in browser.tabs:
print(tab)

print(browser.tabs)
print(browser.current_tab)

# Selecting elements with JQuery
print(yahoo.jq("a"))

# Selecting using CSS Selectors (no JQuery needed)
for item in yahoo.css(".stream-items"):
for a in item.css("a"):
print(a, a.text)

# Some `Tab` attributes/properties
print(google.title)
print(google.url)
print(google.page_source)

# Closing a tab
browser.close_tab(bing)

# Switching to a tab
yahoo.switch()
google.switch()

# Accessing the driver object
print(google.driver.title, google.title)
```

### TODO
Expand Down

0 comments on commit 555b0bd

Please sign in to comment.