How to make WebDriver operate browser in background? - selenium-webdriver

I have a python script which uses Selenium WebDriver to start a Chrome, open an URL, enter simple captcha, check if some data available (time for visit government organization) and repeat this process in 5 minutes.
I want it work in background. Headless mode is not an option because site would show DDos Guard in that case.
I have tried to use driver.minimize_window() but the windows activates for short periods for number input and click of button which is annoying when I work on PC.
Is it possible to make it work completely in background without activate Chrome window?

You can packaging your script using PyInstaller, then run it with Windows Task Scheduler (assuming you are using Windows)
Install pyinstaller
pip install -U pyinstaller
2. Packaging your script (run in cmd)
pyinstaller --onefile --name=your-package-name yourscript.py
Check pyinstaller docs and make sure add --hidden-import or --collect-data flag if needed.
Setup your task scheduler:
Create basic task, choose task name, and description (if needed)
Create trigger
Set action to start a program then define your script name in Program/script and make sure put your script folder path in Start in
Then voila. You can custom the trigger more detail in task properties (creating multiple triggers, specific conditions, etc...)
You could try to start your exe file as process manually using cmd/powershell like this SO answer, and some kind of process/service manager but I have never tried it yet.

When I use selenium and headless option don't work with me, I always do this trick by changing the window size and just leave it.
So, set the window size in the driver itself, like this:
driver.set_window_size(1, 1)
Or, set it as an argument, like this:
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('window-size=1,1')
Or use both.
I hope this helps you.

Related

Restart Applescript causing infinite reboot loop

I tried to set up Voice Control to restart my mac by running the applescript:
tell application "System Events"
restart
end tell
I set it up for voice control. I'd say "Restart the Mac"
However this put my mac into an infinite restart loop!
Had to trash the script.
Is there a solution to this problem with a different applescript that doesn't go into a loop? It would be great to simply say "Restart the Mac" and then presto it restarts.
I use Voice Control every day, without error, to restart my computer.
These are the steps I took.
Paste this following AppleScript code into a new Script Editor document and save it as "Restart Computer.scpt" (Don't save it to any "Startup Items" Folder)
tell application "System Events" to restart
Make sure you grant access in System Preferences for System Events.app to be allowed to control your computer.
With Voice Control currently active, select the new "Restart Computer.scpt" file in Finder, then speak the command "Make This Speakable".
You Should then see a pop-up window like this...
Just insert the voice command you want to use (I use "Restart Computer") and click Save.
Note: Before doing all of this, you should remove any previous custom commands you have set for restarting the computer, from your Commands list in System Preferences.
As an added bonus, this following AppleScript code will reveal the file which stores all of your custom voice commands, in Finder. It's a good idea to backup this file from time to time. Also copying this file to a different computer will allow you to use the custom commands on that computer.
set customDictationCommands to (path to preferences folder as text) & ¬
"com.apple.speech.recognition.AppleSpeechRecognition.CustomCommands.plist"
tell application "Finder" to reveal alias customDictationCommands
I don't think AppleScript is the best tool for this. In System Preferences→Accessibility→Voice Control you can enable Apple's built-in voice command system, which should work out-of-the-box. They don't have a built-in Restart command so you'll have to create one, but that's easy enough. Click the 'Commands...' button at the bottom right, then click the '+' button at the bottom left, and you can choose any of several ways of implementing it.
With restarting script saved as application the simple restart command will enter into infinite reboot loop. This is because of unsaved state of application.
To solve this problem, use following script application instead:
tell application "System Events" to restart with state saving preference

background process launched by TFS is killed when switching to next step

I have some trouble keeping alive a background process when launched by TFS.
Usually I use a batch that launch a java server (new window), as long as I keep this window open it works properly.
C:\Users\TFSService\mbs-iot-sdk\osgi\bin\vms\jdk\server.bat
In order to make my process automatic, I include this in TFS. In the step I call a batch that contains the following:
cd C:\Users\TFSService\mbs-iot-sdk\osgi\bin\vms\jdk // necessary to find the batch
start C:\Users\TFSService\mbs-iot-sdk\osgi\bin\vms\jdk\server.bat
In my task manager, I can see in background tasks that java is launched (no new window is opened), exactly as it behaves when launching directly the batch. But after a few seconds, when TFS switches to the next step, it stops.
Then the next step carries on but fails as it requires the server to be launched.
Is there a particular way of doing it in TFS ?
thank you
Alexandre
It's suggest to launch the .bat file from a relative path not directly use cd to hard code the path.
Also recommend you to use Run Batch File task not Run Command Line task to launch the .bat file.
According to your description, seems you are using a run command line task in your build pipeline. Then run the command under the working directory c:\Build_work\5\s, the command cd to C:\Users\TFSService\mbs-iot-sdk\osgi\bin\vms\jdk\ on the build agent, find the server.bat, run the server.bat.
First check if the .bat file is located at the path you are specifying on the build agent. Not sure if the bat file have to run under C:\Users\TFSService\mbs-iot-sdk\osgi\bin\vms\jdk\, guess you are also hard code the path in your server.bat file. Suggest you change all the path to relative path, you could use some built-in variable in TFS.
As for your workaround in comment, seems you want to chain builds in TFS. The official docs literally say "not yet" and have a uservoice in planed. However you could use some workaround, such as create or use other's customize extension (use rest api) to call another build. Detail ways please refer huserben's answer in this question: How to chain builds in TFS 2015?
Note sure you have to go deep into this area for your original issue. Just add some related info in case you are interested or need.
Well,
Just in case someone else goes through the same kind of issue, I found a workaround:
I wish to mix different command line steps, some of them launching Python scripts:
I have one step for launching the server that is required for my testing tool, one step for my testing tool and one Python step for differential testing
I realized that I could embed everything in a Python script.
It can handle server launching process in a separate window (with subprocess), launch my Python part and launch another process for my validation tool.
I have to test the whole chain but, at least, I solved my problem of launching a background process and detach it from TFS

How to handle Windows authentication dialogue using selenium webdriver?

I have requirement of opening share point URL and it looks for credential through windows authentication. So I read some where to use AutoIt with seleniun webdriver but how we can call this AutoIt script because my web driver script will wait until window dialogue is get off. So cursor will not go to next line to execute AutoIT script and finally we will not be able run our script.
Not sure if you are using Java or C#. In C#, I would go with MSUIA. Write a method to deal with the Windows dialogues and run it on a separate thread. This method should act as a listener to the Desktop tree and whenever encounters a control that it needs to work on, does its job. Have the method scan the Desktop tree once every 5 seconds or so and put it to sleep if no object is found.
In Java, using AutoIt should do the trick, not that it won't in C#
http://www.toolsqa.com/selenium-webdriver/autoit-selenium-webdriver/

Batch file to install software silently (or without user interaction)

I am trying to make batch file for installing software silently. Is there a way to make it automatically select Next and Finish during the installing process?
We need more details to answere your question. what is exactly sw?? is it shockwave player, in this case follow these instructions http://kb2.adobe.com/cps/195/tn_19572.html by adding the /s argument.
Usually with most programs you can add the command /silent or /verysilent when installing them through the command line. I know this works with UltraVNC. If it is your own program then you would have to add that option using Inno Setup or something like that.
If the application does not support silent installation, you will need to look at something like AuotIt. You can use it to automate pressing buttons and entering keystrokes.

How do I launch a WPF app from command.com. I'm getting a FontCache error

I know this is not ideal, but my constraint is that I have a legacy application written in Clipper.
I want to launch a new, WinForms/WPF application from inside the application (to ease transition). This legacy application written in Clipper launches using:
SwpRunCmd("C:\MyApp\MyBat.bat",0)
The batch file contains something like this command:
C:\PROGRA~1\INTERN~1\iexplore "http://QASVR/MyApp/AppWin/MyCompany.MyApp.AppWin.application#MyCompany.MyApp.AppWin.application"
It is launching a WinForms/WPF app that is we deploy via ClickOnce. Everything has been going well until we introduced WPF into the application. We were able to easily launch from the legacy application.
Since we have introduced WPF, however, we have the following behavior. If we launch via the Clipper application first, we get an exception when launching the application. The error text is:
The type initializer for 'System.Windows.FrameworkElement' threw an exception.
at System.Windows.FrameworkElement..ctor()
at System.Windows.Controls.Panel..ctor()
at System.Windows.Controls.DockPanel..ctor()
at System.Windows.Forms.Integration.AvalonAdapter..ctor(ElementHost hostControl)
at System.Windows.Forms.Integration.ElementHost..ctor()
at MyCompany.MyApp.AppWin.Main.InitializeComponent()
at MyCompany.MyApp.AppWin.Main..ctor(String[] args)
at MyCompany.MyApp.AppWin.Program.Main(String[] args)
The type initializer for 'System.Windows.Documents.TextElement' threw an exception.
at System.Windows.FrameworkElement..cctor()
The type initializer for 'System.Windows.Media.FontFamily' threw an exception.
at System.Windows.Media.FontFamily..ctor(String familyName)
at System.Windows.SystemFonts.get_MessageFontFamily()
at System.Windows.Documents.TextElement..cctor()
The type initializer for 'MS.Internal.FontCache.Util' threw an exception.
at MS.Internal.FontCache.Util.get_WindowsFontsUriObject()
at System.Windows.Media.FontFamily.PreCreateDefaultFamilyCollection()
at System.Windows.Media.FontFamily..cctor()
Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
at System.Uri..ctor(String uriString, UriKind uriKind)
at MS.Internal.FontCache.Util..cctor()
If we launch the application via the URL (in IE) or via the icon on the desktop first, we do not get the exception and application launches as expected.
The neat thing is that whatever we launch with first determines whether the app will launch at all. So, if we launch with legacy first, it breaks right away and we can't get the app to run even if we launch with the otherwise successful URL or icon. To get it to work, we have to logout and log back in and start it from the URL or icon.
If we first use the URL or the icon, we have no problem launching from the legacy application from that point forward (until we logout and come back in).
One other piece of information is that we are able to simulate the problem in the following fashion. If we enter a command prompt using "cmd.exe" and execute a statement to launch from a URL, we are successful. If, however, we enter a command prompt using "command.com" and we execute that same statement, we experience the breaking behavior.
We assume it is because the legacy application in Clipper uses the equivalent of command.com to create the shell to spawn the other app. We have tried a bunch of hacks like having command.com run cmd.exe or psexec and then executing, but nothing seems to work.
We have some ideas for workarounds (like making the app launch on startup so we force the successful launch from a URL, making all subsequent launches successful), but they all are sub-optimal even though we have a great deal of control over our workstations.
To reduce the chance that this is related to permissions, we have given the launching account administrative rights (as well as non-administrative rights in case that made a difference).
Any ideas would be greatly-appreciate. Like I said, we have some work arounds, but I would love to avoid them.
Thanks!
It sounds like the Presentation Font Cache service has trouble starting when the app is launched in this way.
If you have control over the client environment, you could try setting the Windows Presentation Font Cache startup to automatic instead of manual.
This is a shot in the dark made with incomplete information:
command.com and cmd.exe are quite different. AFAIK, command.com exists for legacy compatibility, so applications you run from it will run differently. I can't test anything to complete my post because I believe that command.com runs in 16-bit mode and 64bit versions of Windows (on which I'm running) don't support that mode anymore so no more command.com for me.
That being said, there should be no difference when trying to run 32-bit applications (including managed applications).
I'm not aware of what are the limitations of your environment, but some things you may try are:
Rename you .bat into .cmd to make sure it starts with cmd.exe rather than command.com
Make your .bat start the program using the start console command
Have a non-WPF program to invoke your WPF one with a more sane environment
The problem is that the windir environmental variable is not set when using command.com.
So, in your case, adding the line set windir=C:\Windows to the beginning of the bat file will solve the problem (assuming that you have your Windows instalation in C:\Windows.
An additional issue might be that the host application is running command.com in compatibility mode. The best is to list all the environmental variables after running cmd.exe (using the set command) and comparing it to the output of the set command that you set in your bat file

Resources