Give head to an already headless Selenium Firefox/Chrome session - python

I am writing a test bot for an app. Generally I need it to be in headless mode. But at some arbitrary buggy occasions it is a comfort to see the browser window and decide if what a wrong issue is happening there. So I need a way to connect a browser window to an active headless session. In an other word to literally convert it to a head-full one.
P.S. Every thing is in Python. Both Firefox and Chrome solutions(or any sort of guiding) are welcomed.

Related

Keep browser window open while changing chrome options (Selenium Python)

I am attempting to change the proxy settings on a browser without restarting it for the settings to take effect. Is this possible, and if so, how?
EDIT: Figured out a solution that works for me, however, it's not doing what's in the question and still requires a lot more time to complete requests. (Stopping the browser whenever it encounters Cloudflare, swapping proxies, moving on, etc. Stuff I'd quite like to avoid, as this task is extremely time-sensitive and consistency reliant.)
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument('--proxy-server=socks5://ip:ports')
driver = Chrome(options=option)
Login_page(driver).login()
I think no need to restart

Python Selenium automatic booking bot

I wanted to write a bot, which will automatically find canceled driving tests and rescheduled your test date to this date, in order to decrease the time of waiting.
I decided to use selenium on python to perform this. However, despite all my tries, I couldn’t get the browser to stop detecting the bot, so there is always CAPTHCA for this bot.
Can someone suggest a potential solution to this problem?
Selenium-powered browser windows are by default (required by law in some regions) marked automated, and websites that have bots disabled on their platforms can easily detect it and prevent any kind of automation. There are some workarounds but these workarounds are not very reliable as these get patched quite often.
You can try these resources if they still work:
Webpage Is Detecting Selenium Webdriver with Chromedriver as a bot
http://php8legs.com/en/php-web-scraper/51-how-to-avoid-selenium-webdriver-from-being-detected-as-bot-or-web-spider
Can a website detect when you are using Selenium with chromedriver?

Can we get Selenium to focus on an open browser window?

I have been using Selenium for a couple of years now. I learned a ton, thanks to all the amazing people here at SO. Now, I just encountered a new challenge/opportunity. It seems like Selenium always wants to open a new window, which is a new instance, as far as I can tell, and this new instance doesn't know that I am already logged into the system that I want to be logged into. Normally I would run some generic code like this.
import time
from selenium.webdriver import ActionChains
from selenium import webdriver
driver = webdriver.Chrome('C:\\Users\\chromedriver.exe')
driver.get ('https://corp-login_place')
driver.find_element_by_id('USERID').send_keys('myid')
driver.find_element_by_id ('PASSWORD').send_keys('mypw')
list = [145, 270, 207]
for i in list:
driver.find_element_by_id('InputKeys_LOCATION').send_keys(i)
driver.find_element_by_class('PSPUSHBUTTON').click()
button = driver.find_element_by_class_name("Button")
button.click()
time.sleep(5)
So, I think that automation script should work, but when I run the code, it always opens a new browser window, and asks me to login. At this point my login credential don't work. Very weird. Anyway, I'm already logged in, and everything works fine if I just use the mouse and keyboard. It seems like the open browser window, which is out of focus, works fine, but Selenium don't focus on ths open window, and it opens a new window, which doesn't allow me to login.
For some reason, the code doesn't work when I hit F9, but if I run the process manually, using the mouse and keyboard, everything works totally fine. I feel like
Any insight or illumination, as to what is happening here, would be greatly appreciated. Thanks to all!
What I got is that you have a manually opened browser window (which was not opened with Selenium) and you want to control this window with Selenium?
Apparently this is not supported, but there is some code here that maybe can help you.
Selenium always uses a new instance of the browser no matter how many other browser windows are open in your system. If your problem is related to logging into the website you want to test, then please share the website url.
Or else if you are talking about attaching a session of browser window already opened in your system then this article (http://tarunlalwani.com/post/reusing-existing-browser-session-selenium/) might help, albeit the whole purpose of automating is compromised.

Preventing Python webbrowser.open() from dominating screen and preventing other processes. Is it possible to open as minimised?

I am using webbroswer.open() in a loop to download multiple files at given intervals.
The issue I am having is that whenever the browser window opens, it becomes the primary window and thereby interrupts and disrupts my ability to use the computer. Downloading multiple files means this can last some time. The broswer continuously flashing open is obviously jarring.
Is there any way to instruct webbrowser to open the browser minimised by default or otherwise avoid this issue in some other ingenious way?
Much appreciated!
If you are open to using other modules I would urge you to look into selenium. This allows you to do many things, and one of them is to launch in headless mode (so as not to disturb you as it loads pages). The documentation is at:
https://selenium-python.readthedocs.io/
And you would be interested in the headless option
You would be advised though to make sure your script works without this enabled before you enable it though.
Sample code:
import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
my_options = Options()
my_options.headless = True # set to False for debugging!!
browser = webdriver.Chrome(options=my_options)
browser.get('http://www.google.com')
print('Done.')
You will need to download the proper drivers (just follow the instructions on the link I posted) for whatever browser you'd like. I picked Chrome, but they have Edge, Firefox, and Safari browsers as well!

Headless browser capable of popup handling?

I tried https://hub.docker.com/r/wernight/phantomjs/ and https://github.com/SeleniumHQ/docker-selenium/tree/master/StandaloneChrome Docker images with browsers (first one is truly headless and the second one incorporates real chrome running under xvfb) and seems like both of them do no support javascript popups (via window.open) - because there is always only one element in driver.window_handles.
May be I should try it without docker, but any related info will be super appreciated.
I use python with selenium webdriver.

Categories

Resources