public static void main(String[] args) {
String browserName = "chrome";
WebDriver driver = null;
if (browserName.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
} else if (browserName.equalsIgnoreCase("edgebrowser")) {
driver = new EdgeDriver();
i write exactly but still am getting mismatch error
rename your class
add import org.openqa.selenium.WebDriver
add property webdriver.chrome.driver
Code:
package selenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;
public class Gayatri {
public static String chromedriverPath = System.getProperty("user.dir") + "\\resources\\chromedriver.exe";
public static void main(String[] args) {
String browserName = "chrome";
WebDriver driver = null;
if (browserName.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", chromedriverPath);
driver = new ChromeDriver();
} else if (browserName.equalsIgnoreCase("edgebrowser")) {
driver = new EdgeDriver();
}
driver.get("https://www.stackoverflow.com");
System.out.print(driver.getTitle());
driver.quit();
}
}
Output:
Starting ChromeDriver 100.0.4896.60 (6a5d10861ce8de5fce22564658033b43cb7de047-refs/branch-heads/4896#{#875}) on port 19334
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Kvě 02, 2022 8:15:46 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Stack Overflow - Where Developers Learn, Share, & Build Careers
Related
There is an webelement(Success or failure message) which gets displayed on screen only for 3 seconds. How can I assure that the element is displayed on screen for 3 seconds using selenium java OR python?
You can use java.time.LocalDateTime and java.time.temporal.ChronoUnit combine with org.openqa.selenium.support.ui.ExpectedConditions.
In this example the time a button is not enabled is measured:
package selenium;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DeviTimeTest {
public static String userDir = System.getProperty("user.dir");
public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";
public static WebDriver driver;
public static void main(String[] args) {
WebDriver driver = startChromeDriver();
driver.get("https://demoqa.com/dynamic-properties");
LocalDateTime start = LocalDateTime.now();
WebElement button = driver.findElement(By.id("enableAfter"));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(button));
LocalDateTime finish = LocalDateTime.now();
System.out.println("Button was disabled for " + ChronoUnit.MILLIS.between(start, finish) + " ms.");
driver.quit();
}
public static WebDriver startChromeDriver() {
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--start-maximized");
options.addArguments("--disable-notifications");
driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
Output:
Starting ChromeDriver 96.0.4664.45 (76e4c1bb2ab4671b8beba3444e61c0f17584b2fc-refs/branch-heads/4664#{#947}) on port 47769
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 06, 2021 8:00:55 DOP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Button was disabled for 4404 ms.
I'm working on a Java Project using Selenium. I have attempted to implement my test codes. Below here is the snippet of it.
AutoTest
public class AutoTest {
WebDriver driver = null;
#BeforeTest
public void setUp() {
String projectPath = System.getProperty("user.dir");
DesiredCapabilities handlSSLErr = DesiredCapabilities.chrome ();
handlSSLErr.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
//Configuration for WebDriver
System.setProperty("webdriver.chrome.driver", projectPath+"/drivers/chromedriver/chromedriver.exe");
driver = new ChromeDriver(handlSSLErr);
}
#Test
public void createTopUpRequest() {
//browse to UAT Server
driver.get("https://10.2.5.215:33000/viewTopUpRequest");
//enter credentials
LoginPage.usernameLogin(driver).sendKeys("ezltest2svc");
LoginPage.passwordLogin(driver).sendKeys("Password123!");
//Click on submit button
LoginPage.loginButton(driver).sendKeys(Keys.RETURN);
}
#AfterTest
public void closeBrowser() {
//driver.close();
}
}
As soon as it tries navigating to this portal: "https://10.2.5.215:33000/viewTopUpRequest", I get the NET::ERR_CERT_COMMON_NAME_INVALID exception. May I know how to bypass the security protocols?
I'm not sure if org.openqa.selenium.remote.DesiredCapabilities even provides such option.
I'm using org.openqa.selenium.chrome.ChromeOptions:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public static String userDir = System.getProperty("user.dir");
public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";
public static WebDriver startChromeDriver() {
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
This works as well:
public static WebDriver startChromeDriver() {
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.setAcceptInsecureCerts(true);
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}
Tested on https://badssl.com/
breaking my head over the following piece of code in Java, trying to solve a recaptcha for my selenium tests at work before implementing them.
The use case I use: https://www.google.com/recaptcha/api2/demo
WebDriver driver = Selenium.getInstance();
Selenium.goToWebPage("https://www.google.com/recaptcha/api2/demo");
WebDriverWait wait = new WebDriverWait(driver, 5);
List<WebElement> allIFrames = SeleniumUtil.getWebElementNicelyByTagName(driver, wait, "iframe");
if(allIFrames == null){
return;
}
Optional<WebElement> captchaIFrame = allIFrames.stream().filter(webElement -> webElement.getAttribute("src").contains("recaptcha/api2/anchor")).findFirst();
if(!captchaIFrame.isPresent()){
return;
}
WebDriver frame = driver.switchTo().frame(captchaIFrame.get());
//System.out.println(frame.getPageSource());
WebElement recaptchaTokenElement = SeleniumUtil.getWebElementNicelyByXPath(frame, wait, "//*[#type='hidden']");
System.out.println(recaptchaTokenElement);
So with the system.outs i'm trying to find my way to the captcha token, but every time it returns a null element. What the SeleniumUtil so far does is mainly:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
return driver.findElement(By.xpath(xpath));
And the same for By.CssSelector etc.
I am already in the right IFrame, because the page source is printen out correctly, but searching by #recaptcha-token (cssselector), recaptcha-token (id), //*#id="recaptcha-token" does not work.
Maybe someone can help me with this?
There is a iFrame where the driver needs to be switched to.
After few tries I've got:
standard chromedriver setup:
package selenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class WebDriverSetup {
public static String userDir = System.getProperty("user.dir");
public static String chromedriverPath = userDir + "\\resources\\chromedriver.exe";
public static WebDriver driver;
public static WebDriver startChromeDriver() {
System.setProperty("webdriver.chrome.driver", chromedriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
return driver;
}
}
And getting the token:
package selenium;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class GoogleRecaptchaDemo extends WebDriverSetup {
public static void main(String[] args) {
WebDriver driver = startChromeDriver();
driver.get("https://www.google.com/recaptcha/api2/demo");
List<WebElement> frames = driver.findElements(By.tagName("iframe"));
driver.switchTo().frame(frames.get(0));
WebElement rc = driver.findElement(By.id("recaptcha-anchor-label"));
rc.click();
WebElement recaptchaToken = driver.findElement(By.id("recaptcha-token"));
String token = recaptchaToken.getAttribute("value");
System.out.println(token);
driver.quit();
}
}
My output, last line is the token:
Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240#{#378}) on port 25872
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Pro 01, 2020 4:30:45 ODP. org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
03AGdBq24MumCzjDlQCrcb4N3BuIiHm6O7V5IKSNGP1wChYe9g1Vc1GR7_NsvKqKV1Jzb5yLUXCB-ko8satP3fgkXIZ1rbjz76FITaLRRZqRJteQdVzlyzYoUgsSWQ_xjCspDvKNl6uTRGP7EzhesblTBf7HyYmjDU26BZkji8NqMFypQUzUIWUE6CF1W_UeiGCwV4Cfr2UduHJPskDubiwT7jxhRrLKFF_nK8zruV9dStwynSGzrMjwtemZSZmj2ZVJPiTxiYIV2QRIPUYPQ7dXMezrqJDG2Vwn2M9AxHOacoFcBTDIiFYoENq11-jNDJTY0a8PWl6bixpqv1itwTJw4FYnz9vSAQUCztQf7WXPLFgxp8iHjqB-uSIY39ikLJfa_rkFOke90PkF7sRlsKoQjptp_uCpJJ6aktscF97lGWA7EXJfpuOxaCMRt3VkuDZ9mzA5RFiHnQOX9I8VFWWe03t71r69aoj5y1dNoFUVT29lMsZf-X3y00eUxlLiT4JZzRFPfbF1fd4bqdH3SzpvWROxHe-IGRFz7Z_C4Hf7oNCQSXfpus2DIm2INW-JRPgfqZ_XQTaxphinGcoJ3EMvWZo6a3qDFYUB06HduULNO_5GwZ_eHUxE1wvXxGHdq2uoJ4Bkzzfu01ZD2Q13gJ6Mslup5dZlqAQGNggJY9aJYbmnRKNnUCtSY5Y8xHMr3Lnt_07SuFc_UdEDSV_KdtwgpVWEFL8OgAFP_KZaFqdaeUq_CTnHpHiTcAhNosT7V_H4gKx0A4BgHnI5NVxsLU1p09OOZ0GLJMqeP174pl03EpQy9iqnv0_e-w7NUeO7ZFgETt_pvEbqR6VqjfDO35BNd4aItzer9IdiSfzjdIAR6c0Z3_36o1WQyLXQZv_xTuZXaRYoZUTtV38aG3z5uK-pyRnNDqMZGffy5DPUn4WrDMnPM9lfI5nRZwXS503KJas54w4d0fRrZWOyhxYWeiCYhFJMDDwpqHJAzDlIqwPbQX90vVum0zEaJd_TU0l9t5QioUAwb3BuEdTeaOI8gM2OcpBScsQYTioMivi8-eRYpPvJIP239DJz6Rp5Ptk_52kC4_2rAWuG_cocuco_cA61yE3yu2WCw_e1EauOM3Ug2nGeFKFxZZdGWPdNj0E3YA3N6f3IaJYWHrQAz4odFQZaD-HZ4qnRZlJlnMzV31_o3_NLl-_2CGSywE8kuJf8wAIIplncfDdHSIlUn7WhrIOrSOX83NdKaQYAlTqLI9CTSTjaA3AyhG5OcF2dukVeACOItczNBSJcZeenTMCo1Es986nt770A3tQYC7S9r2ESxlYJl-eWGo8SKuM9udx7ZuO5lKgW-V-EUg1I-YFSoguRmGzUK-liu2-afTBn4X-97VXuiv-tdcKdi9Bx4DtlhqzNGvVbQmIOND
I am getting below error while trying to execute my selenium code shown below:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("www.google.com");
}
}
Error - Exception in thread "main" java.lang.IllegalStateException:
The driver executable does not exist:
C:\Selenium\Introduction\C:\chromedriver at
com.google.common.base.Preconditions.checkState(Preconditions.java:585)
at
org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:146)
at
org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:141)
at
org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
at
org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
at
org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at
org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:123)
at Demo.main(Demo.java:11)
I have placed the chrome driver in the location mentioned and the version is also double checked.
This is very minor change with static mode, try with :
static WebDriver driver;
static String driverpath = "C:\\chromedriver.exe";
public static void main(String [] args)
{
System.setProperty("webdriver.chrome.driver", driverpath);
driver = new ChromeDriver();
}
As we are executing Java static main method, its requires to handle driver with Static variables.
Recently, bump into this issues with selenium firefox driver.
Thanks in advance
Set UP
os.name: 'Mac OS X',
os.arch: 'x86_64',
os.version: '10.12.6',
java.version: '1.8.0_131'
Firefox version 56.0.1 (64-bit)
Gecko Driver Latest 0.19.0
The error shows failed:
org.openqa.selenium.SessionNotCreatedException: Tried to run command
without establishing a connection
I tried different ways to tackle it but always come with the same error.
1. update all the selenium test driver to the latest
2. specify the directory export PATH = $PATH driverDir
My code
package automationFramework;
import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertEquals;
public class GeckoDriver {
private static WebDriver driver;
public static int random = 0;
private String baseURL;
// #BeforeClass : Executes only once for the Test-Class.
#BeforeClass
public static void setting_SystemProperties(){
System.out.println("System Properties seting Key value.");
}
// #Before : To execute once before ever Test.
#Before
public void test_Setup(){
System.out.println("Launching Browser");
if (random == 0) {
System.out.println("Start Chrome Browser Testing ");
System.setProperty("webdriver.gecko.driver", "/Users/Fannity/Desktop/Drivers/geckodriver"); // Chrome Driver Location.
driver = new FirefoxDriver();
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("Session ID : " + ((RemoteWebDriver) driver).getSessionId() );
}
#Test
public void selenium_ScreenShot() throws IOException {
baseURL = "https://www.google.com/";
driver.get(baseURL);
System.out.println("Selenium Screen shot.");
File screenshotFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File("/Users/Fannity/Desktop/JUNIT-Selenium.jpg"));
random += 1;
}
// #After : To execute once after ever Test.
#After
public void test_Cleaning(){
System.out.println("Closing Browser");
baseURL = null;
driver.close();
driver.quit();
}
// #AfterClass : Executes only once before Terminating the Test-Class.
#AfterClass
public static void clearing_SystemProperties(){
System.out.println("System Property Removing Key value.");
System.clearProperty("webdriver.gecko.driver");
}
}
ERROR
https://gist.github.com/Fenici/f82f885486de37ae110fda8d7430df6e
Your problem is here:
#After
public void test_Cleaning(){
System.out.println("Closing Browser");
baseURL = null;
driver.close();
driver.quit();
}
Try only with close().
Explanation here.
We generally get this , if we use driver.close() and driver.quit() together so it would be better if you remove driver.close();
public void test_Cleaning(){
System.out.println("Closing Browser");
baseURL = null;
driver.quit();
}