selenium - download file to /tmp not there - selenium

When I download a file in Ubuntu to /tmp using selenium, the file doesn't appear. However, the count postfix of the filename keeps increasing in Chrome, e.g. file (1).txt - meaning the browser finds the previously downloaded files.
Example App.java:
public class App
{
public static void main( String[] args ) throws InterruptedException {
final ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", Map.of("download.default_directory", "/tmp/"));
final ChromeDriver driver = new ChromeDriver(options);
driver.get("file:///home/.../download.html");
Thread.sleep(5000);
driver.quit();
}
}
download.html
<div>Hello</div>
<script>
const element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent("Hello World"));
element.setAttribute('download', "file.txt");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
</script>
After running this java code, the file cannot be found in /tmp or anywhere else in the filesystem: find . 2>/dev/null -name file.txt yields nothing. However, while the code is running, selenium keeps increasing the postfix of the filename:
Where is the downloaded file?

I guess the files are downloaded into your /home/username/Downloads folder, not to /tmp folder as you expecting.

Related

Keep file with Chromedriver in chrome://downloads

I'm trying to automate the download of an XML file with Google Chrome.
I'm using:
Google Chrome v73.0.3683.75 (64 bits)
Chromedriver v73
Selenium WebDriver v3.14.0
C#
The problem comes up when a message of harmful file appears:
As I'm using Chromedriver, I can not interact with this message, so I tried to accept the download from the chrome://downloads page.
Once I open the chrome://downloads page, I click on the Keep button, but an Alert comes up again to confirm the download.
This popup is not a popup Selenium and Chromedriver can Handle with the Dismiss()/Accept()/SendKeys()/... methods. When I try to SwitchTo() it, Chromedriver crashes.
I tried to directly send the keystrokes of {TAB} and {SPACE}/{RIGHT} and {ENTER}, but Chrome seems not to catch them...
The full code is:
String currentWindow = this.Drivers[Navegador].CurrentWindowHandle;
String popupHandle = "";
((IJavaScriptExecutor)this.Drivers[Navegador]).ExecuteScript("window.open('about:blank','_blank')");
ReadOnlyCollection<String> tabs = this.Drivers[Navegador].WindowHandles;
foreach (string handle in tabs){
if (handle != currentWindow){
popupHandle = handle;
break;
}
}
this.Drivers[Navegador].SwitchTo().Window(popupHandle);
this.Drivers[Navegador].Navigate().GoToUrl("chrome://downloads");
String script = "return document.querySelector('body > downloads-manager').shadowRoot.querySelector('#downloadsList > downloads-item').shadowRoot.querySelector('#dangerous > paper-button:nth-child(2)');";
//String script = "return document.querySelector('body > downloads-manager').shadowRoot.querySelector('#downloadsList > downloads-item:nth-child(2)').shadowRoot.querySelector('#url').textContent;";
IWebElement boton = (IWebElement) ((IJavaScriptExecutor) this.Drivers[Navegador]).ExecuteScript(script);
boton.Click();
Thread.Sleep(2000);
SendKeys.Send("{TAB}{SPACE}");
Thread.Sleep(1000);
this.Drivers[Navegador].Close();
this.Drivers[Navegador].SwitchTo().Window(currentWindow);
this.Drivers[Navegador].SwitchTo().DefaultContent();
result = true;
IMPORTANT NOTE:
I tried to launch Chrome with all the flags/options/experimental_options/user_preferences/... possible and it doesn't work. These options/arguments seem to be deprecated in the latest versions of Chrome or Chromedriver.
As discussed with OP, answering the question in Java.
Encountered the same problem few months back, so this is how it worked for me, might work for you as well.
Map<String, Object> chromePreferences = new Hashtable<String, Object>();
// Below preferences will disable popup dialog while downloading the file
chromePreferences.put("profile.default_content_settings.popups", 0);
chromePreferences.put("download.prompt_for_download", "false");
// Set the customised path for downloading the file, if required
chromePreferences.put("download.default_directory", "path");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("prefs", chromePreferences);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
//Now initiate ChromeDriver with the capabilities set above and continue with your automation
ChromeDriver chromeDriver = new ChromeDriver(cap);
I recently ran into this issue, and due to the deprecation of some of the methods in the ChromeDriver, the above solution didn't work.
After a lot of research I decided to switch to IE and explore an alternate option with the inspiration from this article - https://sqa.stackexchange.com/questions/3169/downloading-a-file-in-internet-explorer-through-selenium/3520 I came up with this solution in JAVA.
It is not as 'clean' but it worked for me.
public static void main(String[] args) throws NoAlertPresentException,InterruptedException {
System.setProperty("webdriver.ie.driver","C:\\selenium-java-3.141.59\\IEDriverServer.exe");
String url ="myfileurl";
WebDriver driver = new InternetExplorerDriver();
driver.get(url);
try {
Robot robot = new Robot();
Thread.sleep(2000);
//press alt+s key to save
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
Thread.sleep(2000);
}
catch (AWTException e) {
e.printStackTrace();
}
driver.close();
}

Geb test ignoring GebConfig.groovy file launched in IntelliJ

Running in IntelliJ IDEA.
GebConfig.groovy is in /src/test/resources.
I am using the Chrome driver.
When I type
System.setProperty("webdriver.chrome.driver", "my/path")
inside my spec file, and I right click and select run, the test works, meaning it opens Chrome and loads the page.
When I don't do that in the spec file, but just leave it in the GebConfig.groovy file, I get the error message "the page to the driver executable must be set".
There's an air-gap, so I can't copy-paste; I'll type as much as I can here:
GebConfig.groovy:
import org.openqa.selenium.chrome.ChromeDriver
...
environments {
chrome {
System.setProperty("webdriver.chrome.driver", "my/path")
driver = {new ChromeDriver()}
}
}
The spec file is really simple, like the example on GitHub
import LoginPage
import geb.spock.GebReportSpec
class LoginSpec extends GebReportSpec
{
// Works when I put this here, but I should not have to do this!
System.setProperty("webdriver.chrome.driver", "my/path")
def "user can log in" () {
when: "log in as me"
def loginPage = to LoginPage
loginPage.login("me")
then:
....
}
}
To fix your problem if you want to keep the path in the geb config, setting the path outside of the environment section like so should work:
import org.openqa.selenium.chrome.ChromeDriver
System.setProperty("webdriver.chrome.driver", "my/path")
//You can also set the driver up here as a default and running with an environment set will override it
driver = {new ChromeDriver()}
environments {
chrome {
driver = {new ChromeDriver()}
}
}
Personally I would avoid adding the driver path to the geb config and create a run configuration in intelliJ for running locally.
Right click the spec file > Click "Create 'nameOfMySpec'".
Now add your driver path to the VM parameters:
-Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path
It's also worth considering a shell script that could then also be called via Jenkins etc:
mvn test -Dgeb.env=chrome -Dwebdriver.chrome.driver=my/path

Selenium on Jenkins is skipping all the tests

Java: 8
Selenium: 3.14
Browser: Firefox 62.0.2
Geckodriver: v0.22.0
The Selenium execution of my project on Jenkins is skipping all tests:
Jenkins log
This is the dependencies of selenium on my build.gradle file:
['org.seleniumhq.selenium:selenium-java:3.14.0'],
['org.seleniumhq.selenium:selenium-server:3.14.0'],
['org.seleniumhq.selenium:selenium-api:3.14.0'],
['org.seleniumhq.selenium:selenium-support:3.14.0'],
['org.seleniumhq.selenium:selenium-remote-driver:3.14.0'],
['org.seleniumhq.selenium:selenium-firefox-driver:3.14.0'],
['org.seleniumhq.selenium:selenium-chrome-driver:3.14.0']
And here is where I set the geckodriver path. I'm using only firefox:
#Before
public void openResources() {
if( webDriver == null ){
String geckodriver = seleniumProperties.getString("selenium.caminhoGeckodriver");
try {
String browser = seleniumProperties.getString("selenium.browser");
if (!StringUtils.isEmpty(browser) && browser.toLowerCase().equals("chrome")) {
String path = seleniumProperties.getString("selenium.browser.path");
System.setProperty("webdriver.chrome.driver", path);
webDriver = new ChromeDriver();
} else {
System.setProperty("webdriver.gecko.driver", geckodriver);
webDriver = new FirefoxDriver();
}
} catch (MissingResourceException e) {
System.setProperty("webdriver.gecko.driver", geckodriver);
webDriver = new FirefoxDriver();
}
}
webDriver.manage().timeouts().implicitlyWait(TIMEOUT, TimeUnit.SECONDS);
webDriver.manage().timeouts().pageLoadTimeout(TIMEOUT, TimeUnit.SECONDS);
webDriver.manage().timeouts().setScriptTimeout(TIMEOUT, TimeUnit.SECONDS);
efetuarLogin();
}
The geckodriver path is set right on my seleniumProperties. The firefox version installed on the environment is 62.0.2.
On Eclipse, the tests are not skipped.
UPDATE:
That's the piece of code where is defined the task runSelenium (check the image with the Jenkins log that I posted):
task runSelenium(type: Test) {
include( '**/myProjectSuiteSelenium.class')
maxHeapSize = "1524m"
jvmArgs "-XX:MaxPermSize=512m", "-XX:-UseSplitVerifier"
}
test.finalizedBy runSelenium
include( '**/myProjectSuiteSelenium.class')
Remove this include and replace it with something that matches your actual tests.
I see "selenium.*" in the Jenkins logs.
I figured out what was the problem.
The problem was that the Jenkins wasn't logging the real error. My seleniumProperties was referencing the wrong file with the selenium properties. So it wasn't getting the right geckodriver file.
Once I change to the right file, the tests were not skipped anymore.

Getting "The path to the driver executable must be set by the webdriver.chrome.driver system property"though set correct path

My code is very simple
code:
WebDriver wd =new ChromeDriver();
System.setProperty("webdriver.chrome.driver",
"D:\\List_of_Jar\\chromedriver.exe");
String baseUrl = "https://www.google.com";wd.get(baseUrl);
have downloaded and added jar as "Java-3.4.0" from selenium hq site.
Download Google Chrome Driver-2.29 from the same website and located it in "D:\List_of_Jar" path.
When I run the above code I getting an error as
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
Getting version error though did proper configuration. so kindly help me for fixing the issue.
Details:
OS: Windows XP.
Java : JDK1.8 and JRE1.8.
Selenium : version 3.4
Driver path should be set before browser launch as given below.
System.setProperty("webdriver.chrome.driver","D:\List_of_Jar\chromedriver.exe");
WebDriver wd =new ChromeDriver();
String baseUrl = "https://www.google.com";
wd.get(baseUrl);"
You are setting chrome driver path incorrectly. Property must be set before WebDriver initialization.
Set property like this -
System.setProperty("webdriver.chrome.driver","D:\\List_of_Jar\\chromedriver.exe")
WebDriver wd =new ChromeDriver();
String baseUrl = "https://www.google.com";
wd.get(baseUrl);"
If you are using IntelliJ IDE, then on IntelliJ without setting up within the 'Run > Edit configurations > VM Options' i will just meet this error:
Failed scenarios:
C:/Users/DATestAdmin/IdeaProjects/TestLogin/src/test/resources/login.feature:4 # Scenario: Successfully logging in
1 Scenarios (1 failed)
3 Steps (3 skipped)
0m0.194s
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;
So once i've added the path to my chromedriver locally in 'Run > Edit configurations > VM Options':
-Dwebdriver.chrome.driver="C:\\Users\\This\\Is\\Where\\ChromeDriverIs\\chromedriver_win32.exe"
I'm now able to launch my Chrome browser successfully.
I totally agree with Murthi, but better is to set relative path to the driver, NOT the absolute.
Relative path looks like:
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver.exe");
Abosulte: is the path to the driver in your PC.
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
Why?
It is a good practice to have driver inside your project, not just in your computer. Just find or create folder f.e. resources, inside resources create folder called f.e. drivers and import your driver/drivers exe files there.
The below lines works fine
public class TestNGFile {
public String baseUrl = "https://google.com";
String driverPath = "C:\\\\Users\\\\Documents\\\\selenium\\\\chromedriver_win32\\\\chromedriver.exe";
#Test
public void verifyHomepageTitle() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Documents\\selenium\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("webdriver.gecko.driver", driverPath);
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTitle = "Google";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
driver.close();
Try:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "I:\\Bhasker-ShiroCode\\work\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
}
}
To avoid Error:
webdriver.chrome.driver ( should be in small letters )
have to give correct chromedriver.exe ( correct path )
Import all Selenium jars under class Path
I was getting the same error, since chrome driver was not installed on my machine.
Install the chrome driver. Follow:
https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
You should use Chocolatey as the Selenium wiki dictates. It will work straight away.
Windows users with Chocolatey installed: choco install chromedriver
https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
I also encountered the same problem. Following fix, made my application run smoothly.
Firstly, the required version of chrome driver could be found from below link.
http://chromedriver.storage.googleapis.com/index.html
It is best to use always the latest version. After downloading, set the path of chrome driver in System.setProperty("webdriver.chrome.driver","{Your path Chrome Driver}");
Follow the code fragment.
System.out.println("Creating Chrome Driver");
// Set Chrome Driver
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("{Your URL}");
System.out.println("Wait a bit for the page to render");
TimeUnit.SECONDS.sleep(5);
System.out.println("Taking Screenshot");
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D:\\Images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
I faced the same issue.
"The path to the driver executable must be set by the webdriver.chrome.driver system property."
downloaded the driver and have set in system property.
https://www.youtube.com/watch?v=Ny_8ikCbmcQ

How to run Selenium WebDriver test cases in Chrome

I tried this
WebDriver driver = new ChromeDriver();
But I'm getting the error as
Failed tests: setUp(com.TEST): The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see code here . The latest version can be downloaded from this link
How can I make Chrome test the Selenium WebDriver test cases?
You need to download the executable driver from:
ChromeDriver Download
Then use the following before creating the driver object (already shown in the correct order):
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
This was extracted from the most useful guide from the ChromeDriver Documentation.
Download the updated version of the Google Chrome driver from Chrome Driver.
Please read the release note as well here.
If the Chrome browser is updated, then you need to download the new Chrome driver from the above link, because it would be compatible with the new browser version.
public class chrome
{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}
You should download the chromeDriver in a folder, and add this folder in your PATH environment variable.
You'll have to restart your console to make it work.
If you're using Homebrew on a macOS machine, you can use the command:
brew tap homebrew/cask && brew cask install chromedriver
It should work fine after that with no other configuration.
You need to install the Chrome driver. You can install this package using NuGet as shown below:
You can use the below code to run test cases in Chrome using Selenium WebDriver:
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeTest {
/**
* #param args
* #throws InterruptedException
* #throws IOException
*/
public static void main(String[] args) throws InterruptedException, IOException {
// Telling the system where to find the Chrome driver
System.setProperty(
"webdriver.chrome.driver",
"E:/chromedriver_win32/chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
// Open google.com
webDriver.navigate().to("http://www.google.com");
String html = webDriver.getPageSource();
// Printing result here.
System.out.println(html);
webDriver.close();
webDriver.quit();
}
}
Find the latest version of chromedriver here.
Once downloaded, unzip it at the root of your Python installation, e.g., C:/Program Files/Python-3.5, and that's it.
You don't even need to specify the path anywhere and/or add chromedriver to your path or the like.
I just did it on a clean Python installation and that works.
Download the latest version of the Chrome driver and use this code:
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(10000);
driver.get("http://stackoverflow.com");
On Ubuntu, you can simply install the chromium-chromedriver package:
apt install chromium-chromedriver
Be aware that this also installs an outdated Selenium version. To install the latest Selenium:
pip install selenium
All the previous answers are correct. Following is the little deep dive into the problem and solution.
The driver constructor in Selenium for example
WebDriver driver = new ChromeDriver();
searches for the driver executable, in this case the Google Chrome driver searches for a Chrome driver executable. In case the service is unable to find the executable, the exception is thrown.
This is where the exception comes from (note the check state method)
/**
*
* #param exeName Name of the executable file to look for in PATH
* #param exeProperty Name of a system property that specifies the path to the executable file
* #param exeDocs The link to the driver documentation page
* #param exeDownload The link to the driver download page
*
* #return The driver executable as a {#link File} object
* #throws IllegalStateException If the executable not found or cannot be executed
*/
protected static File findExecutable(
String exeName,
String exeProperty,
String exeDocs,
String exeDownload) {
String defaultPath = new ExecutableFinder().find(exeName);
String exePath = System.getProperty(exeProperty, defaultPath);
checkState(exePath != null,
"The path to the driver executable must be set by the %s system property;"
+ " for more information, see %s. "
+ "The latest version can be downloaded from %s",
exeProperty, exeDocs, exeDownload);
File exe = new File(exePath);
checkExecutable(exe);
return exe;
}
The following is the check state method which throws the exception:
/**
* Ensures the truth of an expression involving the state of the calling instance, but not
* involving any parameters to the calling method.
*
* <p>See {#link #checkState(boolean, String, Object...)} for details.
*/
public static void checkState(
boolean b,
#Nullable String errorMessageTemplate,
#Nullable Object p1,
#Nullable Object p2,
#Nullable Object p3) {
if (!b) {
throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3));
}
}
SOLUTION: set the system property before creating driver object as follows.
System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
The following is the code snippet (for Chrome and Firefox) where the driver service searches for the driver executable:
Chrome:
#Override
protected File findDefaultExecutable() {
return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
"https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver",
"http://chromedriver.storage.googleapis.com/index.html");
}
Firefox:
#Override
protected File findDefaultExecutable() {
return findExecutable(
"geckodriver", GECKO_DRIVER_EXE_PROPERTY,
"https://github.com/mozilla/geckodriver",
"https://github.com/mozilla/geckodriver/releases");
}
where CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver"
and GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver"
Similar is the case for other browsers, and the following is the snapshot of the list of the available browser implementation:
To run Selenium WebDriver test cases in Chrome, follow these steps:
First of all, set the property and Chrome driver path:
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
Initialize the Chrome Driver's object:
WebDriver driver = new ChromeDriver();
Pass the URL into the get method of WebDriver:
driver.get("http://www.google.com");
I included the binary into my projects resources directory like so:
src\main\resources\chrome\chromedriver_win32.zip
src\main\resources\chrome\chromedriver_mac64.zip
src\main\resources\chrome\chromedriver_linux64.zip
Code:
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public WebDriver getWebDriver() throws IOException {
File tempDir = Files.createTempDirectory("chromedriver").toFile();
tempDir.deleteOnExit();
File chromeDriverExecutable;
final String zipResource;
if (SystemUtils.IS_OS_WINDOWS) {
zipResource = "chromedriver_win32.zip";
} else if (SystemUtils.IS_OS_LINUX) {
zipResource = "chromedriver_linux64.zip";
} else if (SystemUtils.IS_OS_MAC) {
zipResource = "chrome/chromedriver_mac64.zip";
} else {
throw new RuntimeException("Unsuppoerted OS");
}
try (InputStream is = getClass().getResourceAsStream("/chrome/" + zipResource)) {
try (ZipInputStream zis = new ZipInputStream(is)) {
ZipEntry entry;
entry = zis.getNextEntry();
chromeDriverExecutable = new File(tempDir, entry.getName());
chromeDriverExecutable.deleteOnExit();
try (OutputStream out = new FileOutputStream(chromeDriverExecutable)) {
IOUtils.copy(zis, out);
}
}
}
System.setProperty("webdriver.chrome.driver", chromeDriverExecutable.getAbsolutePath());
return new ChromeDriver();
}
Download the EXE file of chromedriver and extract it in the current project location.
Here is the link, where we can download the latest version of chromedriver:
https://sites.google.com/a/chromium.org/chromedriver/
Here is the simple code for the launch browser and navigate to a URL.
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://any_url.com");