How to run selenium python script in current window session? - python

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!

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 close webdriver by different python script

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.

Not able to use chrome with selenium and python

pretty brief question.....
when i use selenium with python and open google chrome it opens a new windows saying"Chrome is controlled by automated test software" and not in a normal window what should i do?
thanks

How to install Selenium (python) on a Apache Web Server?

I have up and running an Apache Server with Python 3.x installed already on it. Right now I am trying to run ON the server a little python program (let's say filename.py). But this python program uses the webdriver for Chrome from Selenium. Also it uses sleep from time (but I think this comes by default, so I figure it won't be a problem)
from selenium import webdriver
When I code this program for the first time on my computer, not only I had to write the line of code above but also to manually download the webdriver for Chrome and paste it on /usr/local/bin. Here is the link to the file in case you wonder: Webdriver for Chorme
Anyway, I do not know what the equivalences are to configure this on my server. Do you have any idea how to do it? Or any concepts I could learn related to installing packages on an Apache Server?
Simple solution:
You don't need to install the driver in usr/local/bin. You can have the .exe anywhere and you can specify that with an executable path, see here for an example.
Solution for running on a server
If you have python installed on the server, ideally >3.4 which comes with pip as default. Then install ChromeDriver on a standalone server, follow the instructions here
Note that, Selenium always need an instance of a browser to control.
Luckily, there are browsers out there that aren't that heavy as the usual browsers you know. You don't have to open IE / Firefox / Chrome / Opera. You can use HtmlUnitDriver which controls HTMLUnit - a headless Java browser that does not have any UI. Or a PhantomJsDriver which drives PhantomJS - another headless browser running on WebKit.
Those headless browsers are much less memory-heavy, usually are faster (since they don't have to render anything), they don't require a graphical interface to be available for the computer they run at and are therefore easily usable server-side.
Sample code of headless setup
op = webdriver.ChromeOptions()
op.add_argument('headless')
driver = webdriver.Chrome(options=op)
It's also worth reading on running Selenium RC, see here on that.

Selenium: How to work with already opened web application in Chrome

I'm looking for a solution that could help me out automating the already opened application in Chrome web browser using Selenium and Python web driver. The issue is that the application is super secured, and if it is opened in incognito mode as Selenium tries to do, it sends special code on my phone. This defeats the whole purpose. Can someone provide a hacky way or any other work around/open source tool to automate the application.
Selenium doesn't start Chrome in incognito mode, It just creates a new and fresh profile in the temp folder. You could force Selenium to use the default profile or you could launch Chrome with the debug port opened and the let Selenium connect to it. There is also a third way which is to preinstall the webdriver extension in Chrome. These are the only ways I've encountered to automate Chrome with Selenium.

Categories