Edureka - Handle exceptions in Selenium Web Driver - no print out - java

I am a total noob at Automation Testing and i am trying to learn through Youtube how to do it.
https://youtu.be/FRn5J31eAMw?t=12405
On this course from Edureka there is an example where they are trying to handle an exception, where after they run the script the system shows them a message in the console which i cannot get.
package co.edureka.selenium.demo;
import java.util.NoSuchElementException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingExceptions {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
Thread.sleep(3000);
try {
driver.findElement(By.name("fake")).click();
}catch (NoSuchElementException e) {
System.out.println("element is not found");
System.out.println("Hello");
//throw(e);
}
System.out.println("Hello");
}
}
this is the script in Java they are running and at the end in the console they are getting this as a result
Edureka Console
But i am getting something totally different even though my code is exactly the same.
My Console
What am i doing wrong?

your are using "import java.util.NoSuchElementException;
" at top of file so please remove and use below one
org.openqa.selenium.NoSuchElementException

Related

Not able to select the first row after applying a filter in a grid while using Selenium with java [tricky situation]

I am getting this strange scenario.
Been searching for a long time without success, what is going on?
Let me explain you:
I am trying to create an automation in a webpage, i have to enter a
specific value in a dinamic table and after performing a "ENTER" event
it is necessary to select the first row shown:
After running my code i got this but the row shown is not been selected, it is stuck:
This is my code:
package first;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Script_codes {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","C:\\Users\\Steve\\Desktop\\Me\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://stage.nbm2.test/backend");
driver.findElement(By.id("username")).sendKeys("test");
driver.findElement(By.id("login")).sendKeys("test");
driver.findElement(By.xpath("//*[#id=\"login-form\"]/fieldset/div[3]/div[2]/button")).click();
driver.findElement(By.xpath("//*[#id=\"menu-magento-backend-stores\"]/a")).click();
driver.findElement(By.xpath("//*[#id=\"menu-magento-backend-stores\"]/div/ul/li[2]/ul/li[2]/div/ul/li[3]/a")).click();
driver.findElement(By.xpath("//*[#id=\"attributeGrid_filter_frontend_label\"]")).sendKeys("linea");
driver.findElement(By.xpath("//*[#id=\"attributeGrid_filter_frontend_label\"]")).sendKeys(Keys.ENTER);
driver.findElement(By.className("col-label col-frontend_label")).click();
}
}
I have tried many different ways but it seems not to be working, what can i do?
Expected output:
I would first try the following to see if timing is the issue:
package first;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Script_codes {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver","C:\\Users\\Steve\\Desktop\\Me\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://stage.nbm2.test/backend");
Thread.sleep(1000);
driver.findElement(By.id("username")).sendKeys("test");
Thread.sleep(1000);
driver.findElement(By.id("login")).sendKeys("test");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id=\"login-form\"]/fieldset/div[3]/div[2]/button")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id=\"menu-magento-backend-stores\"]/a")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id=\"menu-magento-backend-stores\"]/div/ul/li[2]/ul/li[2]/div/ul/li[3]/a")).click();
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id=\"attributeGrid_filter_frontend_label\"]")).sendKeys("linea");
Thread.sleep(1000);
driver.findElement(By.xpath("//*[#id=\"attributeGrid_filter_frontend_label\"]")).sendKeys(Keys.ENTER);
Thread.sleep(1000);
driver.findElement(By.className("col-label col-frontend_label")).click();
}
}
If this works, I expect it will be fairly simple to identify which step(s) needs extra time. For this wait, I suggest you implement the WebDriverWait class for better stability and execution time.
I was able to solved my problem by using:
driver.findElement(By.xpath("//*[#id=\"attributeGrid_table\"]/tbody/tr/td[1]")).click();
instead of:
driver.findElement(By.className("col-label col-frontend_label")).click();

opening chrome in selenium issue

I'm currently taking my first automated test class and the instructor has us creating a program in Eclipse after loading Selenium and create a step in the program to look at an executable to bring up chrome then designate a website to check. It looks like i am stuck in a loop?
Here is the program:
java program
Here is the result:
program result
any and all help would be appreciated. Thank you for your time.
I think this is what you want
This code is to open the default browser and go to a specific link
You can specify the path of any browser you want from the path in the code
import java.awt.Desktop;
import java.net.URI;
public class openBrowser {
public openBrowser() {
try {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(new URI("https://www.google.com"));
}
}catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[]args) {
new openBrowser();
}
}
For your code you can follow the following steps
Download ChromeDriver from here
Extract the zip file and follow the path ( because it is easy ) C:\\chromeDriver\\chromedriver.exe
include the ChromeDriver location in your PATH environment variable
Download the required Libraries from the following junit openqa
Add the Libraries to your project ( Build Path )
then this is your code
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
import org.junit.Test;
public class WebDriverDemo {
#Test
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromeDriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("ChromeDriver");
searchBox.submit();
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
Logger.getLogger(WebDriverDemo.class.getName()).log(Level.SEVERE, null, ex);
}
driver.quit();
}
}
During the implementation of the code in the eclipse, many problems occurred, so I advise you to implement the project on NetBeans
I use Java 8 and Windows 8.1

org.openqa.selenium.NoSuchElementException: Unable to locate element error

I have written the following code in JAVA using Selenium web driver using both Internet Explorer and Firefox. Everytime I am getting the same error. Tried using both "id" and "xpath" method, but still it is failing. Tried adding some delay also, still does not work.
My JAVA code for Firefox:
package ieconnector;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class FireFoxConnector {
public static void main(String[] args) {
try{
GetBrowserProperty gbp = new GetBrowserProperty();
System.setProperty("webdriver.ie.driver",gbp.getIeConnection());
System.setProperty("webdriver.gecko.driver","D:\\softwares\\Selenium\\geckodriver-v0.21.0-win64\\geckodriver.exe");
WebDriver wb = new FirefoxDriver();
Capabilities caps = ((RemoteWebDriver) wb).getCapabilities();
System.out.println("Caps is "+caps);
wb.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//wb.navigate().to("https://somewebsite.com:22222/SSO/ui/SSOLogin.jsp");
wb.get("https://somewebsite.com:22222/SSO/ui/SSOLogin.jsp");
wb.manage().deleteAllCookies();
wb.manage().window().maximize();
//wb.findElement(By.id("usertxt")).sendKeys(("user").toUpperCase());
//wb.findElement(By.className("passtxt")).sendKeys("password");
//WebDriverWait wait = new WebDriverWait(wb,10);
//WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("usertxt")));
wb.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
//wb.findElement(By.id("usertxt")).sendKeys("USER");
wb.findElement(By.xpath("//*[#id='usertxt']")).sendKeys("USER");
System.out.println("Testing is successful");
} catch (Exception e) {
e.printStackTrace();
}
}
}
And the following is a screenshot of the HTML code in my developer tool in IE/Firefox.
As per the HTML you have shared to locate the User ID field you can use the following solution:
cssSelector:
wb.findElement(By.cssSelector("input.txtbox#usertxt")).sendKeys("USER");
xpath:
wb.findElement(By.xpath("//input[#class='txtbox' and #id='usertxt']")).sendKeys("USER");

Getting error while running my first selenium code

I just started learning Selenium.
This is my code:
package sele;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Abc {
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.close();
}
}
This is the exception:
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:277)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:247)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:242)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:238)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:127)
at sele.Abc.main(Abc.java:10)
I am not sure if it is the same in java but in C# you use:
driver.Quit();
at the end of your code. Some use
driver.Dispose();
This will work as well.
Look here for more Help.

automatic login to gmail error occuring through firefox

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Login {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("https://www.mail.google.com");
driver.findElement(By.id("Email")).sendKeys("abc#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("xyz");
driver.findElement(By.id("signIn")).click();
}
}
I am trying to write a program to login automatically into gmail.
I have tried everything
please help!!!!
This is giving me the error like this:
Exception in thread "main" java.lang.NoSuchMethodError:com.google.common.
base.Platform.precomputeCharMatcher
(Lcom/google/common/base/CharMatcher;)
Lcom/google/common/base/CharMatcher;
at com.google.common.base.CharMatcher.precomputed(CharMatcher.java:664)
at com.google.common.base.CharMatcher.(CharMatcher.java:71)
at com.google.common.base.Splitter.on(Splitter.java:127)
at org.openqa.selenium.remote.http.JsonHttpCommandCodec.
(JsonHttpCommandCodec.java:59)
at org.openqa.selenium.remote.HttpCommandExecutor.
(HttpCommandExecutor.java:85)
at org.openqa.selenium.remote.HttpCommandExecutor.
(HttpCommandExecutor.java:70)
at org.openqa.selenium.remote.HttpCommandExecutor.
(HttpCommandExecutor.java:58)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.
start(NewProfileExtensionConnection.java:87)
at org.openqa.selenium.firefox.FirefoxDriver.startClient
(FirefoxDriver.java:271)
at org.openqa.selenium.remote.RemoteWebDriver.
(RemoteWebDriver.java:119)
at org.openqa.selenium.firefox.FirefoxDriver.
(FirefoxDriver.java:218)
at org.openqa.selenium.firefox.FirefoxDriver.
(FirefoxDriver.java:211)
at org.openqa.selenium.firefox.FirefoxDriver.
(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.
(FirefoxDriver.java:120)
at com.st.Login.main(Login.java:17)
First of all: that URL doesn't go anywhere (at least in my side). You can achieve what you want as follows:
driver.get("https://accounts.google.com/")
driver.findElement(By.id("Email")).sendKeys("");
driver.findElement(By.id("next")).click();
driver.findElement(By.id("Passwd")).sendKeys("");
driver.findElement(By.id("signIn")).click();

Categories

Resources