How to close webdriver by different python script - python

I want to know is there a way to create a webdriver by script1.py , but close the webdriver by script2.py . I don't use the time.sleep() because I set these scripts to execute after few month. And I'm afraid that the scripts will delay due to the network crash. Any ideas are welcome.
Thanks!

The Python Selenium framework, SeleniumBase, comes with a special pytest command-line option called, --reuse-session, which tells all your tests to reuse the same browser session, even if all the tests live in different Python files. (More info on SeleniumBase command-line options here.) When using --reuse-session mode, the first test run will spin up the web browser, and then the last test run will close it. This should give you what you're looking for, where a different Python script closes the browser than the one that originally opened it.

Related

Selenium --headless option behaviour?

I`m using Python Selenium to navigate TripAdvisor.com
After running my script for a while, I get banned by IP (or at least it seems that way)
However, when I retry or even run a somewhat similar script from another machine in the same network, the script is still working properly and does not fail due to HTTP 403 or something like that.
I've managed to deduce that --headless option for Chrome webdriver has something to do with it. Without it, the script fails as it is supposed to, showing Access Denied as on the screenshot.
So, my question is, what does the webdriver in --headless mod do differently as opposed to not using it?
UPD: I have rephrased the question, as commentors pointed out, nobody has insight into server logic

How to manage several selenium scripts running at once on VDS?

Currently, I have two python bots running on VDS, both of them are using selenium and running headless chrome to get dynamically generated content. While there was only one script, there was no problem, but now, it appears that the two scripts fight for the chrome process (or driver?) and only get it once the other one is done.
Have to mention, that in both scripts, Webdriver is instantiated and closed within a function, that itself is ran inside a Process of multiprocessing python module.
Running in virtual environment didn't do anything, each script has their own file of chrome driver in their respective directories, and by using ps -a I found that there are two different processes of chromedriver running and closing, so I am positive that scripts aren't using the same chrome.
Sometimes, the error says "session not started" and sometimes "window already closed".
My question is - how do I properly configure everything, so that the scripts don't interfere with each other?
For anyone having the same problem - double-triple-quadriple-check that the function, that you're passing in the Process, is the one instantiating Webdriver. I can't believe this problem is fixed just like that.

How to run selenium python script in current window session?

When I'm writing a selenium python script, I have to start a session with some command like
driver = webdriver.Firefox()
However, this opens a new browser window.
What I would like is to have the window that is already open be accessed by the script, much like it would be if I have started the selenium IDE add-on (that cannot run python scripts afaik).
Could anybody please tell me if there is a way to do that?
I've often wanted this functionality with Selenium and Python myself. Unfortunately, it's not part of Selenium's current features.
For more info, check out the answer threads here:
Can Selenium interact with an existing browser session?
(looks like someone came up with a hack solution, but I haven't tested it)
and here:
Can Selenium webdriver attach to already open browser window?
Good luck!

Automating launch of a python selenium script

I am trying to crontab a python/selenium script but obviously this won't work since Firefox requires a display, I went down the PyVirtualDisplay / xvfb route but this won't give me visual feedback, I don't want to run the script headless,I just want it to launch automatically like if I was entering the command line by hand in the terminal, just automatic . Would you know of a way to do it without going through the crontab/headless route ?
thank you ,
If you want to run your test in background but same time you can see browser when you want, try this:
Install CI and run it service by your user (try Jenkins, google will help you)
Write Ant Script, it will help to automate the run. (URL)
I'm using CI and Ant to run my test by schedule and when i want buy one click. Good luck maybe this will help you.

Why doesn’t input.send_keys() work in my Selenium WebDriver Python script when run as www-data?

I have a Python script that uses Selenium WebDriver (with PyVirtualDisplay as the display) to log into Flickr.
http://pastebin.com/dqmf4Ecw (you’ll need to add your own Flickr credentials)
When I run it as myself on my Debian server, it works fine. (I’m a sudoer, but I don’t use sudo when running the script.)
When I run it as the user www-data (which is what it’ll be running as eventually, because I want to trigger it from a Django website), I get two problems, one small, one big:
(Small): the webdriver.Firefox() call takes 30–45 seconds to return, compared to 2 seconds when run as myself
(Big): the script fails to log into Flickr. In order to log in, I find the username and password fields on the Flickr signin page (http://www.flickr.com/signin/), and use element.send_keys() to enter the username and password. Although Selenium seems to find the elements (i.e. no NoSuchElementException is thrown), the values do not get entered in the fields when the script is run as www-data (according to the screenshots I take using browser.save_screenshot), unlike when the script is run as myself.
Why does send_keys() not work when the script is run as www-data? (And is it related to the browser taking much longer to start?)
Maybe you have something different in your environment.
Try copy by example your ~/.bashrc in /home/www-data
If it's not sufficient, run this command both as your current user & as www-data:
strace -tt -f -s 1000 -o /tmp/trace ./script.py
And paste it (filter out your logins/passwords) somewhere.
We will see what's happens.
Sometimes, Firefox performs some nasty plugin compatibility check during startup. As each user can have a different set of browser plugins, this could be responsible for the difference in startup times. You could try to sync your Firefox profiles between users.
Then, are you sure that Firefox as user www-data has proper network/internet access? Can you confirm that the Flickr site loads properly via SeleniumHQ? "The script fails to log into Flickr" is too unprecise. Some more details about why it fails might reveal the problem instantaneously.
Edit: Sorry, I just understood that there shouldn't be a difference in profiles, because Selenium creates one. Nevertheless, my second point might be useful, so I won't delete this answer.
Some more things to ponder about:
Could you spawn firefox manually from www-data account once and make sure that Firefox is not updating itself before every execution of the script? I once faced this problem with Selenium RC on Windows and had to let the update finish before starting the script with the updated binary.
As a workaround, I guess you could you try running the script as www-data user but connecting remotely to a webdriver server running in your login (aka "grid" mode). Would that work for you?
I would suggest getting the latest chrome from google and trying input.send_keys() in that browser instead.
Sometimes some features of webdriver get broken with new releases.. If you are bent on testing with firefox, you might have better luck with an older/newer version of selenium webdriver.
I remember having a similar issue regarding send_keys() on a mac.. My issue was that send_keys() did not work in certain modal windows after I updated selenium webdriver.. I fixed it by reverting to an older webdriver that I knew to work. However, I was using Ruby and not Python to drive webdriver.
sometimes, there might also be a problem with getting the correct ENV variables in your shell if you use it as a different user. I would suggest trying to troubleshoot and see if all the shell ENV variables are set properly under www-data.

Categories