seleniumbot


Nameseleniumbot JSON
Version 0.0.6 PyPI version JSON
download
home_pagehttps://github.com/Mercurial5
SummarySeleniumBot
upload_time2023-05-31 17:12:17
maintainer
docs_urlNone
authorMercurial5
requires_python>=3.11
license
keywords seleniumbot
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # SeleniumBot

This library provides you tools that will help you create selenium bots.

## Installation

`pip install seleniumbot`

## Actions

Actions are classes that will tell bot what to do. There are two types of actions, `simple` actions
and `navigation` actions.

Actions also have some required properties. Here's the list of them:
- `name` - Action name.
- `coefficient` - The chance that this action will be executed.

### Simple Actions

Simple actions are the actions that do not create other actions.

### Navigation Actions

Navigation actions are the actions that after execution return set of new actions.

## Examples

### Simple Action

This is the example on how to create a simple action. Note, that this action receives the button
that it will click to during it's executing. This button should come from `Navigation` type action.

> `Note`: This action goes into new page, where old actions may be irrelevant. For this type
> of actions we need to set `flush_actions = True`

`click_video.py:`
```python
import time

from selenium.webdriver.remote.webelement import WebElement

from seleniumbot import SeleniumClient, Action


class ClickVideo(Action):
    name = 'ClickVideoAction'
    coefficient = 0.1
    flush_actions = True  
    

    button: WebElement

    def __init__(self, button: WebElement):
        self.button = button

    def _execute(self, client: SeleniumClient, last_action: Action | None):
        self.button.click()

        # YouTube has different page reload logic when clicking on videos,
        # so we need to add some sleep time
        time.sleep(10)

```


### Navigation Action

This is example of a `ScrollAction` which is `Navigation` action type, that produces new actions 
(`ClickVideoAction` in particular).

`scroll.py:`

```python
from selenium.webdriver.common.by import By

from click_video import ClickVideo
from seleniumbot import Action, SeleniumClient


class ScrollAction(Action):
    name = 'ScrollAction'
    coefficient = 0.75

    def _execute(self, client: SeleniumClient, last_action: Action | None) -> list[Action]:
        scroll_min, scroll_max = 400, 1500
        client.scroll(scroll_min, scroll_max)

        click_video_actions = self.__get_click_video_actions(client)

        actions = [*click_video_actions]
        return actions

    @staticmethod
    def __get_click_video_actions(client: SeleniumClient) -> list[ClickVideo]:
        video_id = 'thumbnail'
        videos = client.find_elements(By.ID, video_id)
        click_video_action = [ClickVideo(button=video) for video in videos]

        return click_video_action

```

### StartAction

In order to tell our bot where to start we need to create another `StartAction`.
`StartAction` is a special action that should be executed only once, at the beginning.

`start.py`

```python
from seleniumbot import Action, SeleniumClient


class StartAction(Action):
    name = 'StartAction'
    coefficient = 0  # put any number since this field is ignored for StartAction

    def _execute(self, client: SeleniumClient, last_action: Action | None):
        client.goto('https://www.youtube.com/')

```

### Putting it all together

`main.py:`

```python
from selenium.webdriver.chrome.options import Options

from scroll import ScrollAction
from seleniumbot import SeleniumBot
from start import StartAction


def main():
    # In order to test and see how bot behaves, we need to reassign options
    # that will not use --headless mode.
    options = Options()
    options.add_argument('window-size=1920,1080')
    
    
    bot = SeleniumBot(start_action=StartAction, options=options)
    bot.set_navigation_actions([ScrollAction])
    bot.start()


if __name__ == '__main__':
    main()


```

## Using proxy

In order to use proxy, just pass second type of options to SeleniumBot.

```python
from scroll import ScrollAction
from selenium.webdriver.chrome.options import Options
from start import StartAction

from seleniumbot import SeleniumBot


def main():
    # In order to test and see how bot behaves, we need to reassign options
    # that will not use --headless mode.
    options = Options()
    options.add_argument('window-size=1920,1080')

    seleniumwire_options = {
        'proxy': {
            'https': 'https://user:password@host:port'
        }
    }

    bot = SeleniumBot(start_action=StartAction, options=options, seleniumwire_options=seleniumwire_options)
    bot.set_navigation_actions([ScrollAction])
    bot.start()


if __name__ == '__main__':
    main()

```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Mercurial5",
    "name": "seleniumbot",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": "",
    "keywords": "SeleniumBot",
    "author": "Mercurial5",
    "author_email": "dias.nespayev@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/88/87/0a13eb69683f51be616a1e7aa6b23c03acddddb667ce638a6d330dc825be/seleniumbot-0.0.6.tar.gz",
    "platform": null,
    "description": "# SeleniumBot\n\nThis library provides you tools that will help you create selenium bots.\n\n## Installation\n\n`pip install seleniumbot`\n\n## Actions\n\nActions are classes that will tell bot what to do. There are two types of actions, `simple` actions\nand `navigation` actions.\n\nActions also have some required properties. Here's the list of them:\n- `name` - Action name.\n- `coefficient` - The chance that this action will be executed.\n\n### Simple Actions\n\nSimple actions are the actions that do not create other actions.\n\n### Navigation Actions\n\nNavigation actions are the actions that after execution return set of new actions.\n\n## Examples\n\n### Simple Action\n\nThis is the example on how to create a simple action. Note, that this action receives the button\nthat it will click to during it's executing. This button should come from `Navigation` type action.\n\n> `Note`: This action goes into new page, where old actions may be irrelevant. For this type\n> of actions we need to set `flush_actions = True`\n\n`click_video.py:`\n```python\nimport time\n\nfrom selenium.webdriver.remote.webelement import WebElement\n\nfrom seleniumbot import SeleniumClient, Action\n\n\nclass ClickVideo(Action):\n    name = 'ClickVideoAction'\n    coefficient = 0.1\n    flush_actions = True  \n    \n\n    button: WebElement\n\n    def __init__(self, button: WebElement):\n        self.button = button\n\n    def _execute(self, client: SeleniumClient, last_action: Action | None):\n        self.button.click()\n\n        # YouTube has different page reload logic when clicking on videos,\n        # so we need to add some sleep time\n        time.sleep(10)\n\n```\n\n\n### Navigation Action\n\nThis is example of a `ScrollAction` which is `Navigation` action type, that produces new actions \n(`ClickVideoAction` in particular).\n\n`scroll.py:`\n\n```python\nfrom selenium.webdriver.common.by import By\n\nfrom click_video import ClickVideo\nfrom seleniumbot import Action, SeleniumClient\n\n\nclass ScrollAction(Action):\n    name = 'ScrollAction'\n    coefficient = 0.75\n\n    def _execute(self, client: SeleniumClient, last_action: Action | None) -> list[Action]:\n        scroll_min, scroll_max = 400, 1500\n        client.scroll(scroll_min, scroll_max)\n\n        click_video_actions = self.__get_click_video_actions(client)\n\n        actions = [*click_video_actions]\n        return actions\n\n    @staticmethod\n    def __get_click_video_actions(client: SeleniumClient) -> list[ClickVideo]:\n        video_id = 'thumbnail'\n        videos = client.find_elements(By.ID, video_id)\n        click_video_action = [ClickVideo(button=video) for video in videos]\n\n        return click_video_action\n\n```\n\n### StartAction\n\nIn order to tell our bot where to start we need to create another `StartAction`.\n`StartAction` is a special action that should be executed only once, at the beginning.\n\n`start.py`\n\n```python\nfrom seleniumbot import Action, SeleniumClient\n\n\nclass StartAction(Action):\n    name = 'StartAction'\n    coefficient = 0  # put any number since this field is ignored for StartAction\n\n    def _execute(self, client: SeleniumClient, last_action: Action | None):\n        client.goto('https://www.youtube.com/')\n\n```\n\n### Putting it all together\n\n`main.py:`\n\n```python\nfrom selenium.webdriver.chrome.options import Options\n\nfrom scroll import ScrollAction\nfrom seleniumbot import SeleniumBot\nfrom start import StartAction\n\n\ndef main():\n    # In order to test and see how bot behaves, we need to reassign options\n    # that will not use --headless mode.\n    options = Options()\n    options.add_argument('window-size=1920,1080')\n    \n    \n    bot = SeleniumBot(start_action=StartAction, options=options)\n    bot.set_navigation_actions([ScrollAction])\n    bot.start()\n\n\nif __name__ == '__main__':\n    main()\n\n\n```\n\n## Using proxy\n\nIn order to use proxy, just pass second type of options to SeleniumBot.\n\n```python\nfrom scroll import ScrollAction\nfrom selenium.webdriver.chrome.options import Options\nfrom start import StartAction\n\nfrom seleniumbot import SeleniumBot\n\n\ndef main():\n    # In order to test and see how bot behaves, we need to reassign options\n    # that will not use --headless mode.\n    options = Options()\n    options.add_argument('window-size=1920,1080')\n\n    seleniumwire_options = {\n        'proxy': {\n            'https': 'https://user:password@host:port'\n        }\n    }\n\n    bot = SeleniumBot(start_action=StartAction, options=options, seleniumwire_options=seleniumwire_options)\n    bot.set_navigation_actions([ScrollAction])\n    bot.start()\n\n\nif __name__ == '__main__':\n    main()\n\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "SeleniumBot",
    "version": "0.0.6",
    "project_urls": {
        "Homepage": "https://github.com/Mercurial5"
    },
    "split_keywords": [
        "seleniumbot"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "88870a13eb69683f51be616a1e7aa6b23c03acddddb667ce638a6d330dc825be",
                "md5": "392980e3b50dcb8f778414bccbe28130",
                "sha256": "19d0ff0db4afa2095ab8ea8f1c748ec690c1e62f7d7994781d1e6906f0109429"
            },
            "downloads": -1,
            "filename": "seleniumbot-0.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "392980e3b50dcb8f778414bccbe28130",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 6969,
            "upload_time": "2023-05-31T17:12:17",
            "upload_time_iso_8601": "2023-05-31T17:12:17.666950Z",
            "url": "https://files.pythonhosted.org/packages/88/87/0a13eb69683f51be616a1e7aa6b23c03acddddb667ce638a6d330dc825be/seleniumbot-0.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-31 17:12:17",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "seleniumbot"
}
        
Elapsed time: 0.16602s