Program using selenium fails after building with cx_freeze - selenium

I'm developing an automatic web tester using Selenium (v2.37.2). Program works properly until I run the test built with cxfreeze (there is also tkinter gui).
there is the init function
def initDriver(self):
if self.browser == FIREFOX:
profile = webdriver.FirefoxProfile(profile_directory=self.profile);
self._driver = webdriver.Firefox(firefox_profile=profile)
elif self.browser == CHROME:
self._driver = webdriver.Chrome(self.executable, chrome_options=profile)
elif self.browser == IEXPLORER:
self._driver = webdriver.Ie(self.executable)
Now when I build it using Cx_freeze I get this error
method redirectToBlank(...) calls initDriver(..) as the first thingSo how I pack the .xpi file to the library.zip file - which option in setup.py I have to use? And do I even have to this?
And the second strange thing is, that the other browsers work fine, when I execute the .exe file in by clicking on its icon, but when I run it from command line, I get errors even for chrome and IE. (Sorry that the traceback isn't complete)
All paths are relative from the executed file (no matter from where you run it),
Thank you for any ideas to solve this problem.
(method redirectToBlank(...) calls initDriver(..) as the first thing)

First issue solved
It's problem with selenium - FirefoxProfile - class, which tries to load webdriver.xpi as a normal file, but selenium pack all libraries to a zip file, so selenium can't find it.
Even forcing cx_freeze in setup file to add webdriver.xpi to a proper directory in zip won't help.
It is necessary to edit FirefoxProfile (in firefox_profile module) class for example like this
def _install_extension(self, addon, unpack=True):
"""
Installs addon from a filepath, url
or directory of addons in the profile.
- path: url, path to .xpi, or directory of addons
- unpack: whether to unpack unless specified otherwise in the install.rdf
"""
if addon == WEBDRIVER_EXT:
# altered lines
import sdi.env
WEBDRIVER_SUBSTITUTE = "path/to/unpacked/webdrive.xpi"
addon = os.path.join(os.path.dirname(__file__), WEBDRIVER_SUBSTITUTE)
# Original lines:
# addon = os.path.join(os.path.dirname(__file__), WEBDRIVER_EXT)
< the rest of the method >
Issue 2
OSError: win error 6: the handle is invalid problem wasn't caused by either cxfreeze or selenium. I run the final exe file from git bash. There's the problem. For some reason git bash doesn't open stdin for the program and that's why it fails. When I run it in standard windows command line, everything is ok or if i run it from git bash like program.exe < empty_file

what i did was remove selenium form packages list.
and put it inside includefiles, then it works.
like this :
includefiles = [(seleniumPackage,'')]
...
options = {'build_exe': {'includes':includes,
'excludes':excludes,
'optimize':2,
'packages':packages,
'include_files':includefiles,
...

Related

Is there any selenium/python script for the Screen Recording?

I want to record the screen by using selenium in python, I searched for these, but I only get results for java, and for the screenshots. Please let me know if there is any script by which I can record the screen.
you need record the whone screenshot? try this library
and try this code:
from clicknium import clicknium as cc, locator, ui
cc.get_screenshot("D:\\test.jpg")
Try this, very popular framework for eg. java or python etc.
in cmd line : pip install allure-pytest
go here: https://docs.qameta.io/allure/
section 2.1. Installing a commandline
download the newest version for eg. windows xxx.zip
copy path of bin folder eg: D:\Drivers\allure-2.18.1\bin
and paste it to Environment Variables > find 'Path' and edit it> add new path with bin path.
to Your test.py file:
import allure
from allure_commons.types import AttachmentType
and add it at the end of Your test in eg.: assertion statement
example:
method_name = self.driver.find_element(By.XPATH, "xxx").text
if method_name == 'some text':
assert True
else:
allure.attach(self.driver.get_screenshot_as_png(), name="test_name",
attachment_type=AttachmentType.PNG)
assert False
In IDE terminal or cmd do this:
pytest -v -s --alluredir="D:\projectPath\raport" Path/to/your/tests
then tests will be executed
in terminal do this to read raport wfrom test:
allure serve "D:\raport\Path"
with this allure framework You will have reports of tests with screenshots:

gunicorn[15246]: Message: 'geckodriver' executable needs to be in PATH

I'm currently trying to run a python script using selenium and geckodriver on an Ubuntu 20.04 server. I'm using gunicorn and nginx to setup my flask application, but I've been trying for days now to use every answer on all similar articles, but still nothing is working for me as it is for others.
Anyone have any ideas what I'm doing wrong?
Firefox version – Mozilla Firefox 81.0
Selenium version - 3.141.0
Geckodriver version – 0.27.0
Code: (I've tried each of these 6 below)
def login(self):
try:
print("Logging in...")
# 1)
ff_options = FirefoxOptions()
ff_options.headless = True
self.driver = webdriver.Firefox(options=ff_options)
# 2)
ff_options = FirefoxOptions()
ff_options.headless = True
self.driver = webdriver.Firefox(executable_path="./geckodriver", options=ff_options)
# 3)
self.driver = webdriver.Firefox()
# 4)
self.driver = webdriver.Firefox('./geckodriver')
# 5)
binary = FirefoxBinary('/usr/local/bin/geckodriver')
browser = webdriver.Firefox(firefox_binary=binary)
# 6)
binary = FirefoxBinary('/usr/bin/firefox')
driver = webdriver.Firefox(firefox_binary=binary, executable_path="/home/ubuntu/cole/instagram_bot_v2")
self.driver.get('https://www.instagram.com/accounts/login')
Error I'm receiving:
Oct 12 07:00:11 ip-172-31-32-84 gunicorn[15245]: Message: 'geckodriver' executable needs to be in PATH.
The geckodriver executable itself I've tried placing in /usr/bin, /usr/local/bin and also just in my project directory, all are set to executable, but it doesn't seem to make a difference when running it, they all get the same path error.
Anyone have any ideas what I could try?
One thing that you can do is to move your geckodriver to the same directory where your program is saved. Then, you can start the geckodriver just by using webdriver.Firefox() instead of specifying the full path of the executable file. This would save you a lot of time.

nw.js chromedriver won't run from outside of the project folder

I am trying to set up automated tests on an nw.js based app using selenium-python with chromedriver and for practical reasons (frequent reinstallation...) I want to keep chromedriver separated from the rest of the files in another folder. My tests work only when the chromedriver is located at the same folder as the rest of the project (along with nw.exe). If I try to place it anywhere else and alter paths with 'binary_location', 'chrome_driver_binary' and 'add_argument' accordingly, I always end up with exceptions such as
selenium.common.exceptions.WebDriverException: Message: unknown error:
cannot find Chrome binary
or
selenium.common.exceptions.WebDriverException: Message: unknown error:
Failed to create a Chrome process
Nw.js documentation wasn't helpful as it only says the following:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("nwapp=/path/to/your/app")
driver = webdriver.Chrome(executable_path='/path/to/nwjs/chromedriver', chrome_options=chrome_options)
Thanks in advance for any ideas.
I was able to do this but I used ruby. It should be possible to translate to python.
Also I'm running Ubuntu 20.04. No idea if this will all work in Windows.
chrome_options.add_argument("nwapp=/path/to/your/app")
This line doesn't specify, but the nwapp should be a directory.
If you build your app using phoenix builder, then you probably have a line like this in package.json:
"package:linux": "npm run build:prod && build --tasks linux-x64 --mirror https://dl.nwjs.io/ .",
For me, this command creates a directory called packages/myapp-0.0.1-linux-x64
Inside of there you should see an executable "myapp" and the manifest "package.json"
The nwapp= line should point to this directory. It will use the manifest file to get the app name. In ruby that looks like this:
myapp_dir = File.join(root_dir, 'packages', 'myapp-0.0.1-linux-x64')
chrome_options.add_argument("nwapp=" + myapp_dir)
executable_path='/path/to/nwjs/chromedriver'
When you install nw.js with npm, it will create a directory tree: node_modules/nw/nwjs Inside that directory is the chromedriver executable.
The "executable_path" is the full path to chromedriver executable. In ruby, it looks like this:
chromedriver_path = File.join(root_dir, 'node_modules', 'nw', 'nwjs', 'chromedriver')
Selenium::WebDriver::Chrome::Service.driver_path = chromedriver_path
#driver = Selenium::WebDriver.for :chrome, options: chrome_options
Ruby does everything relative to where you invoke it, so I added a script in the root of my nw.js app and then added this line to package.json:
"e2e": "ruby ./e2e.rb"
Then this runs via npm run e2e

Download word document in local folder using Rselenium

I am using R selenium to navigate through a website and eventually click a button that opens a dialog window to download the file. However, I want the file to automatically save in a different local folder.
My question is similar to this question.
Instead of using remDr <- remoteDriver(browserName = "firefox") I have tried
remDr <- remoteDriver(extraCapabilities = makeFirefoxProfile(list(
"browser.helperApps.neverAsk.saveToDisk"="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
)), but this gave an error that some local folder does not exist.
Note that I have no prior knowledge of Selenium and am quite new to programming.
All help welcome!
EDIT: because it it concerns a .docx file the correct MIME type appears to be: application/vnd.openxmlformats-officedocument.wordprocessingml.document. So I tried the following:
remDr <- remoteDriver(extraCapabilities = makeFirefoxProfile(list(
"browser.helperApps.neverAsk.saveToDisk"="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
))
but this gave following error:
Error in file(tmpfile, "rb") : cannot open the connection
In addition: Warning messages:
1: running command '"zip" -r9Xjq "C:\Users\PiKr\AppData\Local\Temp\RtmpApt5uQ\file19006f62c60.zip" "C:\Users\PiKr\AppData\Local\Temp\RtmpApt5uQ/firefoxprofile/prefs.js" ' had status 127
2: In file(tmpfile, "rb") :
cannot open file 'C:\Users\PiKr\AppData\Local\Temp\RtmpApt5uQ\file19006f62c60.zip': No such file or directory
I guess you must install the Rtools from the website and install the rtools before your proceed running the above program. Zipping action needs Rtools to perform. (Referring to the error 1)
Add the path to environment variable, if it doesn't add automatically

running dart project says dart:html is not available for the standalone vm

I used Git to pull a project
I then start IntelliJ, and say: Open Project.
The project itself looks like it has 4 modules, Lab1, Lab2, Solution2, Solution2
I open the full project. In Lab1, i want to run to see how my web page looks, but when i say: Run main.dart the error kicked back is:
C:\code\dart-sdk\bin\dart.exe --ignore-unrecognized-flags --checked --enable-vm-service:51293 --trace_service_pause_events C:\code\workspace\tw_remoting_training\codelab_01\web\main.dart
Observatory listening on http://127.0.0.1:51293
The built-in library 'dart:html' is not available on the stand-alone VM.
'package:remoting_training/remoting_printer.dart': error: line 20 pos 1: library handler failed
import 'dart:html';
^
Process finished with exit code 254
Im not sure what is going on here though. As a secondary option, i will also try to Right-Click on index.html and click: Open with > Dartium but that shows a blank page. It shouldve done 4 async calls which printed strings to the screen.
Is there something I am missing? Is it not running because of it being a module in a project?
If your Dart script imports dart:html or a library that imports dart:html you can run that script only from the Dartium browser (by adding a script tag to HTML that points the that Dart script, or if you run it through pub build or dart2js in any browser), but it can't be run from the console.
With dart:io it's exactly the opposite, it can't be run in the browser.