Getting Around Webdriver's Lack of Interactions API in Safari - python

I am needing to use the ENTER key in Safari. Turns out Webdriver does not have the Interactions API in the Safari driver. I saw some code from a question about this with a java solution using Robot, and was wondering if there is a purely Python way to do a similar thing.

Darth,
Mac osascript has libraries for Python. Be sure to 'import os' to gain access to the Mac osascript functionality.
Here is the command that I am using:
cmd = """
osascript -e 'tell application "System Events" to keystroke return'
"""
os.system(cmd)
This does a brute force return. If you're trying to interact with system resources such as a Finder dialog, or something like that, make sure you give it time to appear and go away once you interact with it. You can find out what windows are active (as well as setting Safari or other browsers 'active', if it hasn't come back to front) using Webdriver / Python.
Another thing that I have to do is to use a return call after clicking on buttons within Safari. Clicks are a little busted, so I will click on something to select it (Webdriver gets that far), then do an osascript 'return' to commit the click.
I hope this helps.
Best wishes,
-Vek
If this answer appears on ANY other site than stackoverflow.com, it is without my authorization and should be reported

Related

Python application using eel "unable to connect" to localhost on startup

I am trying to create a python application while using eel to create a user interface in html. My operating system is Ubuntu Linux and I'm using Firefox to display the web interface.
The problem I'm having is every time I run the python code, Firefox opens a blank page saying "Unable to connect" followed by "Firefox can't establish a connection to the server at localhost:8000". However, if I click the "Try Again" button once, twice, or three times, my interface is displayed.
Once open, I can navigate to different pages but I also noticed that once I navigate to a different page, some of my javascript stops working (specifically a window.close() function). I don't know if this is related but I thought I would mention it just in case.
Any advice on the matter would be greatly appreciated.
Thank you.
I changed my browser from firefox to chromium and now my interface loads on startup the first time. I know some documentation says it can be used with firefox, and it can, but it seems to be kind of buggy and works better with other browsers.
However, I'm still having trouble with my javascript not running but that will be another question.

Interact with Windows modal dialog box using Python

I am using selenium to open a web browser (specifically Chrome) in order to login to an account. When I run my script, there is a modal dialog box that lets the user know there is an error with administrative permissions. Once the user clicks "Okay", the rest of the script runs without issue.
The problem is I need this script to run automatically. Is there a way to use Python to click the "Okay" button? The alert is not within Chrome, so .switch_to_alert() is not helpful. I've found this question asked previously but the response makes it clear it can't be done with selenium, but can it be done another way?
I've also found this source on using ctypes to handle, but I'm unable to install ctypes.
Any tips/help would be appreciated!
I did further research and it appears that a system modal dialog box cannot be accessed with Python.
I was able to prevent the pop up from occurring at all by getting an older version of the chromedriver. Instead of the most updated (ChromeDriver 2.30), I reverted back to ChromeDriver 2.27 and my program worked without issue.

How to handle windows authentication popup in Robot Framework RIDE?

Windows authentication popup of my application appears similar to this app https://www.engprod-charter.net/ I had used below code to handle window authentication popup in selenium using python.
from selenium import webdriver
import time
import win32com.client
driver=webdriver.Firefox()
driver.maximize_window()
driver.get('https://www.engprod-charter.net/')
shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("username")
shell.Sendkeys("{TAB}")
shell.Sendkeys("password#123")
time.sleep(5)
shell.Sendkeys("{ENTER}")
time.sleep(2)
driver.quit()
But since I am using python 2.7.12 for Robot Framework, it seems win32com.client is not supported there. I want to know what library I have to use in Robot Framework to handle this scenario.
I am not asking complete solution as this is not the correct approach in stackoverflow, just want a hint what should I try to handle my scenario. Thanks
You can achieve your scenario using AutoItLibrary which handles window inputs and similar things. Basically controlling the entire GUI not just the browser.
First install AutoItLibrary for Robot Framework
Make sure you have python installed for 32 bit(say python 2.7.6)
Install pywin32-217.win32-py2.7.exe for 32 bit from here
Download AutoItLibrary and unzip the folder
Open command prompt as Administrator and go to downloaded AautoItLibrary folder folder(cd...\AutoItLibrary-1.1.post1) and then write this command and hit enter python setup.py install
Now include AutoItLibrary in Robot Framework
Let us know if this works, I would be interested to know
This can be easily done using Robot Class in java, which can be run in Robotframework test when required.
i.e. A .jar which will function as handler for the windows authentication pop up.
In RobotFramework you would just need to run a .jar which will handle your pop up.
Just in case you are not used to Java, I can help you with a sample code.

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