How to set default download path for chrome using selenium webdriver in ubuntu 20.04? - selenium

I am trying to save a zip file by using download as option inside a specific folder. I found a way by which I am able to download the file. But the problem where I am stuck is while setting default path in ubuntu it is taking frontslash /, whereas in windows it takes backslash \.
Below is my code which i am using to set the default path.
HashMap<String, Object> hashmap = new HashMap<>();
String defaultPath = System.getProperty("user.dir")+ "\\src\\main\\resources";
hashmap.put("download.default_directory", defaultPath);
options.setExperimentalOption("prefs", hashmap);
During execution the default path is set as below in Ubuntu 20.04 which causing the test case to fail as the file is downloaded in the default location.
/home/user/git/ui-automation\src\main\resources
Can someone help me to resolve this issue ?

You can use the Paths class to resolve file paths that are normalized to the underlying host system:
import java.nio.file.Paths
String userDir = System.getProperty("user.dir");
Strung defaultPath = Paths.get(userDir, "src", "main", "resources").toString();

Related

Add geckodriver to PATH on Windows 11

To complete a project using Selenium, I first need to add Geckodriver to PATH. I have read many articles about adding elements to PATH, but none of them end up working (I follow the steps and add a new element to PATH, but when I run my program I get the 'geckodriver' executable needs to be in PATH error.
1: I first download this version of Geckodriver from here: https://github.com/mozilla/geckodriver/releases
2: I extract the folder and add it to the PATH
3: When I run my program, I get the same error Message: 'geckodriver' executable needs to be in PATH.
Any help would be appreciated!
You do need to add an environment variable for the firefox driver.
String exePath = ".\\lib\\geckodriver.exe"; System.setProperty("webdriver.gecko.driver", exePath); driver = new FirefoxDriver();
You don't have to add a new environment variable, just move the file geckodriver.exe to your python folder, which in my case is
C:\Users\username\AppData\Local\Programs\Python\Python310
Then to start the firefox driver just run
from selenium import webdriver
driver = webdriver.Firefox()

Failed- Path too long error while downloading file in Chrome using selenium

I want to download file in my current working directory using selenium automation. But I am getting 'Path too long' error. The code I have written so far is:
os.chdir(os.path.dirname(__file__))
current_directory = os.getcwd()
windows_cwd = current_directory.replace('\\','\\\\')+'\\\\'
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': windows_cwd,
'download.directory_upgrade': True,
'safebrowsing.enabled': False,
'safebrowsing.disable_download_protection': True
}
chrome_options.add_experimental_option('prefs',prefs)
browser = webdriver.Chrome(options=chrome_options)
My current working directory is:
C:\Users\US177\PycharmProjects\Plugin
where the path is too long.
But it successfully downloads to
C:\Users\US177\Desktop
failed-long path
I'm not exactly sure what your question is based on the information provided, but I'm guessing it's along the lines of "Why is this happening?", so I will address that question.
The maximum length of a file name in Windows is 260 characters. The file is able to download to your desktop because the name of the file (when appended to your path) does not exceed this limit. When trying to download to PycharmProjects\Plugin\ folder, the path has become too long.
While setting your download path, try using double backslash (ie. path\\to\\directory).
See this Github issue about programatically downloading from chrome

On deploying latest Aspose 10.6.2 v getting NULLPOINTEREXCEPTION on save in UNIX env

I had migrated the jar to Aspose latest version(10.6.2) and successfully deployed on UNIX env. While saving the form in UNIX env I am getting NullpointerPointer exception.
My name is Tilal Ahmad and I am a developer evangelist at Aspose.
Please note on non-windows OSs subjected exception arises due to font folder path setting. You need to set font folder path according to your machine setting before using any other code, it will resolve the issue. For example on my Ubuntu server, I set font folder path as following.
// Set font folder path
String path = "/usr/share/fonts/truetype/msttcorefonts/";
// Adding a single font directory
// com.aspose.pdf.Document.addLocalFontPath(path);
// seting the user list for standard font directories
java.util.List list = com.aspose.pdf.Document.getLocalFontPaths();
list.add(path);
com.aspose.pdf.Document.setLocalFontPaths(list);
.....
.....

Program using selenium fails after building with cx_freeze

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,
...

How to load file from the Bundle in eclipse & My Eclipse?

This is very strange behavior I have noticed while developing Plugin.
I have a file in the bundle that has to be loaded for wizard.
I tried to load the file in eclipse using following code.
Bundle bundle = Platform.getBundle(MTPAppPlugin.getDefault()
.getBundle().getSymbolicName());
URL fileURL = bundle.getEntry(relativeFilePath);
File file = new File(FileLocator.resolve(fileURL).toURI());
But this didnt worked in MyEclipse.So I used other way for my eclipse
url = new URL("platform:/plugin/"
+ MTPAppPlugin.getDefault().getBundle().getSymbolicName()
+ relativeFilePath);
InputStream inputStream = url.openConnection().getInputStream();
in = new BufferedReader(new InputStreamReader(inputStream));
Now I need to know is there any common way to load the file for eclipse & My Eclipse?
The first one will not work if you have space in your folder path like C:\program files....
The first one seems to be correct. What was the error given in MyEclipse?