Home > Net >  Can I copy actions from selenium session to other sessions?
Can I copy actions from selenium session to other sessions?

Time:10-06

I would like to launch multiple selenium sessions and only one of them make visible. An user can interact with this webdriver window, and I want to retranslate all his actions on other sessions.

How can I do that on python?

CodePudding user response:

No. Selenium can control only those sessions, which have been initialized by its own. You cannot retranslate test actions for more that 1 session.

The only similar thing you can do is to create few (as much as you need) tests, which will contain the same copy-pasted steps inside itself and run each of them in a separate window.

CodePudding user response:

What you could do is that you set up the driver to inject javascript on each page load. There are solutions for this here in the answer section of this question.

You will need a javascript which is capable is detecting the actions you want to 'copy'. If you are interested only in click actions, a very simple js could do the job of capturing click events, there are a few examples in the answer section here. If you can ensure that the window sizes are the same, the X,Y coordinates from the event could be just enough. In the project where I did something similar I calculated the XPATH of the clicked element, and grabbed the value of all input fields. It is not a trivial task but a quite possible one. When you have the data to replicate the events, you need to send that back to python. See the answers to this question how to post data via javascript.

You will need a flask/bottle or something similar framework (or re-invent the wheel) to receive the data and send it to the other selenium instances. There are some good examples how to get the JSON data from the browser to flask in the answers of this question.

You may need to handle CORS problems when sending the data from the browser to flask. See the answers of this question how you can handle that.

If you have the data in flask, all you need to do is replicate the events in the other driver instances. If you work with coordinates, you can issue a click on the page via ActionChains, see answers of this question. Or if you have element id-s or xpath or something, you can find the element and click it. Writing a parser which translates the events into selenium actions is not the most difficult part of a project like this. :)

Well, simple as that. I'd recommend to try it, because it would improve your skills a lot. I don't have copy-paste ready code for this, but I gave all the ingredients you need to start.

  • Related