My distro doesn't provide a 'selenium' package
$ apt search selenium p libtest-www-selenium-perl - Perl test framework using Selenium Remote Control i A python3-selenium - Python3 bindings for Selenium p qunit-selenium - Run QUnit tests through Selenium WebDriver p ruby-selenium-webdriver.
I've tried this and this approaches and the browser does get invoked successfully. This is my code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
//import selenium.webdriver;
//import selenium.webdriver.support.ui.WebDriverWait;
//import selenium.webdriver.common.by.By;
//import selenium.webdriver.support.expected_conditions;
import static org.openqa.selenium.By.*;
//import static sun.security.util.KnownOIDs.EC;
public class Login {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "./geckodriver");
WebDriver dr = new FirefoxDriver();
dr.get("https://google.com/");
dr.manage().window().maximize();
//Click on "Join now"
WebElement join = dr.findElement(xpath("//a[#class='newUser green']"));
//WebDriverWait(driver, 20).until(EC.element_to_be_clickable((xpath("//a[#class='newUser green']")))).click();
join.click();
}
}
My problem is uncommenting any of the imports breaks because java has no idea what a selenium is. How do I add it? Linux Mint.
Go to https://chromedriver.chromium.org/downloads page
Depends on your Chrome version, Download Linux chrome driver zip package
Unip package and locate under any folder that is in system PATH or add your folder to PATH
Go to https://www.selenium.dev/downloads/ and download Selenium Java package and locate it under your project's library package(depends on your project/IDE settings)
Welcome. There is a lot going on in your quesrion: intelij; Java class paths; import statements.
I recommend tackling it one at a time.
copy the java example on selenium.dev doco page.
Run commandline javac.exe directly javac command examples (code Java.net) look for the 'Compile a source file which depends on multiple libraries' example. NOTE: the import statements are class references... Don't use the filename (is different to python).
Search Web for how to configure classpath for intelij.
Related
I am unable to execute my first selenium code. Tried changing webdriver path several times but that didn't work.
Code trials:
package Learningday1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstScript {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\amann\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://selenium.dev");
System.out.println(driver.getTitle());
driver.quit();
}
}
Error:
Exception in thread "main" java.lang.IllegalStateException: The driver executable must exist: C:\Users\amann\eclipse-workspace\Selenium learning 2.0\???C:\Users\amann\Downloads\chromedriver_win32\chromedriver.exe
at org.openqa.selenium.internal.Require$FileStateChecker.isFile(Require.java:342)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:147)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:142)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:39)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:233)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:437)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:128)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:49)
at Learningday1.FirstScript.main(FirstScript.java:10)
Snapshot:
There is some ambiguity between your code trials and the error stacktrace.
In your code you mentioed the ChromeDriver as:
C:\\Users\\amann\\Downloads\\chromedriver_win32\\chromedriver.exe
Where as the error stacktrace mentions the driver executable doesn't exists at:
C:\Users\amann\eclipse-workspace\Selenium learning 2.0???C:\Users\amann\Downloads\chromedriver_win32\chromedriver.exe
Solution
Ensure that you have unzipped the ChromeDriver and placed with the sub-directory:
C:\\Users\\amann\\Downloads\\chromedriver_win32
and you need to mention the absolute path within the System.setProperty() line as:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\amann\\Downloads\\chromedriver_win32\\chromedriver.exe");
Update
You have 2 projects with almost similar names, a maven project and another a Java project. The name starting with Selenium learning.
Try to avoid same name for multiple projects and blank spaces within the project names. Example SeleniumTest, SeleniumProgram, etc.
PS: You may opt to delete the current project and create a new one.
References
You can find a relevant detailed discussion in:
java.lang.IllegalStateException: The driver executable does not exist: while trying to execute tests through Selenium, ChromeDriver and Chrome
I have installed jdk-12.0.2 64bit on Windows 7 64bit Service Pack 1,
I am using Eclipse IDE for Enterprise Java Developers.
Version: 2019-06 (4.12.0)
Build id:20190614-1200.
I created a new java project having a new package and a new java class.
When I added the following Jars in Project Build Path:
selenium-server-standalone-3.141.59.jar
selenium-3.141.59-nodeps.jar
selenium-3.141.59-nodeps-sources.jar (with and without this jar)
all jar files of lib folder
Then import entry
import org.openqa.selenium.WebDriver; shows following error
The package org.openqa.selenium is accessible from more than one module: okio, org.openqa.selenium.core, selenium
other import entry
import org.openqa.selenium.chrome.ChromeDriver; shows following error
The type org.openqa.selenium.chrome.ChromeDriver is not accessible
When I removed selenium-server-standalone-3.141.59.jar
from project build path then got the following exception.
Error occurred during initialization of boot layer
java.lang.module.FindException: Unable to derive module descriptor for D:\SS\Selenium-3.0.0\lib\xalan-2.7.2.jar
Caused by: java.lang.module.InvalidModuleDescriptorException: Provider class org.apache.bsf.BSFManager not in module
I have already worked with Selenium 3.0 using JDK 1.8. with (Chrome, IE, Gecho) browsers.
Here with jdk-12.0.2, I have also tried selenium 3.0, with alternative addition and removal of jars.
Also tried by adding/removing of jars from Selenium-Server-xxx versions.
Following is the Java class code:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class loginPage {
WebDriver wd;
public void invokeBrowser() {
System.setProperty("webdriver.chrome.driver", "D:\\SS\\Selenium-3.141.59\\chromedriver.exe");
wd = new ChromeDriver();
wd.manage().deleteAllCookies();
wd.manage().window().fullscreen();
wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wd.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
wd.get("http://myurl/"); }
public static void main(String[] args) {
loginPage mlp = new loginPage();
mlp.invokeBrowser(); }
}
I have written a JAVA program which will trigger Chrome driver and open google.com the program works fine in eclipse so I created a JAR file.
Even the JAR file is working fine independently. It will open google.com in google chrome then its closing the chrome driver.
But I am trying to trigger this JAR through PHP now for some reason this is not working.
The PHP is not triggering the JAR file at all.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Chrome_Test {
public static void main(String[] args) {
// Telling the system where to find the Chrome driver
System.getProperty("user.dir" + "\\chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
// Open google.com
webDriver.navigate().to("http://www.google.com");
// Printing result here.
WebDriverWait wait = new WebDriverWait(webDriver, 10);
webDriver.close();
webDriver.quit();
}
}
My PHP Code:
<?php
exec("java -jar TEST10.jar");
?>
TEST10 is the name of the JAR containing the above Java program.
I want to fill web form in bot way. I added the libraryclient-combined 3.0.0 beta 3 to the document .My firefox version should be most updated .
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("https://mail.google.com");
driver.findElement(By.id("Email")).sendKeys("yourEmailId");
driver.findElement(By.id("Passwd")).sendKeys("yourPassword");
driver.findElement(By.id("signIn")).click();
}
}
However , error comes out .
In my understanding , the seleniums 3.0 jar version should be along with geckodriver . Then ,i try to install geckodriver v10.0 here.
https://github.com/mozilla/geckodriver/releases
When I execute the geckodriver-v0.10.0-win64.zip , the installer can't be installed -only black window comes out .
What's wrong ?
REMARK: x64 window 10 version
you can use marionette driver which i recently used. You need to download and rename it to wires.exe. you can download from the following link
https://github.com/mozilla/geckodriver/releases
You need to add selenium-2.53.0 jar files.
below is the code you need to write.
System.setProperty("webdriver.gecko.driver", "G:\\ravik\\Ravi-Training\\Selenium\\Marionette for firefox\\wires.exe");
WebDriver driver = new MarionetteDriver();
driver.get("https://www.google.co.in/webhp?hl=en&sa=X&ved=0ahUKEwjdgc21jJHOAhVCvY8KHZ4aCdcQPAgD");
System.out.println("marionette working fine....");
To all those that are still confused:
What's wrong ?
Well, nothing is wrong, you just need to understand that geckodriver.exe is the driver itself, not an installer that will eventually install the driver on your machine.
So there are two steps you need to do in order to use (or let's say install) the driver:
Unzip the folder on your machine (which you already did) and
Add the driver executable path to test properties (the thing that Ravi pointed out)
Additional note: I use Intellij IDEA to run my tests, so I simply edit the test run configuration and add this line:
-Dwebdriver.gecko.driver="Path\to\my\geckodriver.exe"
I want to extract the last frame from the swf file using Java. However, after trying several ways to to do that, one way it worked is by using selenium to launch a browser, load the swf, wait for the last frame and take the snapshot. It works fine on my mac. The code is as below
package flash;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class snaphot_swf {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.get("file:///Users/test/Downloads/Car-speakers-590x90.swf");
Thread.sleep(10000);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("/Users/test/Documents/screenshots/screenshot.png"));
driver.close();
System.out.print("snapshot taken");
}
}
I now want to do the same on a head less browser in Cent OS 6.4
I did the following:
//install firefox
yum install firefox
//install flash plugin
rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-x86_64-1.0-1.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux
yum check-update
yum install flash-plugin nspluginwrapper alsa-plugins-pulseaudio libcurl
//install Xvfb
yum info xorg-x11-server-Xvfb
//starting Xvfb
/usr/bin/Xvfb :1 -screen 0 1024x768x24 &
export DISPLAY=:1
Now, when I run my Java code for a png file by loading it in the firefox browser and taking snapshot, it works perfectly well. However, when I try to load the swf file I get a blank snapshot.
How can I load the swf in Firefox and obtain a snapshot