Git Product home page Git Product logo

seleniumredis's Introduction

Selenium + Redis Example

This is an example of using Selenium with Redis and Flask. The purpose of this is to save session with Selenium. You may want to do this for a few reasons:

  • Save login session
    • Very useful for saving Captcha or ReCaptcha session
    • Of course more obviously: save time + resources not logging in every time
  • Selenium code can be tested immediately upon saving file. No need to restart Selenium (or webdriver), Flask or Redis.
  • Applications that have many Selenium calls can use one instance instead of creating many, saving resources
  • Scheduling tasks that can be completed with Selenium

How to run locally

  1. Git clone this repository
  2. Ensure that Redis is downloaded locally (see Brew instructions)
  3. cd into repo's root
  4. Run python3 -m venv env - this is to create the virtual environment
  5. Run source env/bin/activate - this is to enter the virtual environment (you'll have to run this command for every new terminal window you create)
  6. Run pip install -r requirements.txtto install the requirements/dependencies for this project
  7. Now, we can start the servers. You'll need 3 terminal windows which are in the virtual environment.
  8. First terminal window, run flask run(starts Flask server to handle requests and add tasks to queue)
  9. Second, run redis-server(starts Redis server)
  10. Third, run python red.py(starts Redis worker to listen and queue tasks based on requests sent) (Make sure you are in the virtual environment on your newly opened window or else you will get an error that says Redis module could not be found.)

From this point, you can use Postman or an application of your choice to hit the flask API endpoints.

GET http://127.0.0.1:5000/run - This will start Selenium instance
GET http://127.0.0.1:5000/run2 - This will open Github
GET http://127.0.0.1:5000/run3 - This will open StackOverflow

Try this: Send many (run2 and run3) requests within a few seconds. You will see that Redis is adding each request to the queue and ONLY when a task is finished, it will move onto the next task.

What's the point of this?

As described earlier, the problem this solves is not opening a new Selenium driver for each task it needs to do. And, task queuing is also handled so many requests can be sent to Selenium and it will do the tasks as it can.

Try a few things!

  1. Change the link from github to google. Upon saving your code, /run2 will immediately take you to google. No more stopping Selenium to effect code changes.
  2. Queue up a bunch of functions and watch in the red.py window how tasks are being done in queue order.

seleniumredis's People

Contributors

atawfique21 avatar

Stargazers

 avatar  avatar Sakib Hossain avatar

Watchers

James Cloos avatar  avatar

seleniumredis's Issues

Can't run the code in Windows10+Ubuntu

Hi Abir, I was just testing the code and I hitted a pothole.

I am using w10 to run flask/selenium + Ubuntu (from Windows Store) to run redis.

After install everything and run all the three consoles, I installed Postman to do the requests, and I got this:

flask

127.0.0.1 - - [30/Jun/2021 19:45:42] "←[37mGET /run HTTP/1.1←[0m" 200 -

red.py

19:45:42 ←[31mWorker rq:worker:afe5eec3cc5a4f5fbf2e7b0322f48512: found an unhandled exception, quitting...←[39;49;00m
Traceback (most recent call last):
  File "C:\Users\kiwi\Desktop\python\SeleniumRedis\lib\site-packages\rq\worker.py", line 549, in work
    self.execute_job(job, queue)
  File "C:\Users\kiwi\Desktop\python\SeleniumRedis\lib\site-packages\rq\worker.py", line 773, in execute_job
    self.fork_work_horse(job, queue)
  File "C:\Users\kiwi\Desktop\python\SeleniumRedis\lib\site-packages\rq\worker.py", line 698, in fork_work_horse
    child_pid = os.fork()
AttributeError: module 'os' has no attribute 'fork'

Of course I went straight to Google to see if I could fix this error, but it seems that is almost a dead-end in Windows.

Do you have any thoughts about how to fix this or we should use full ubuntu to try this setup?

Thanks a lot for the code and for the medium article. Very interesting. Good work!!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.