Error while running Selenium WebDriver - java

While running the Selenium WebDriver code, I get the following error message.
Microsoft Windows Malicious Software Removal Tool - ... wants to restore your Chrome settings to their original defaults
This is the code I am using.
package snaptrude;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyProject {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.chrome.driver","C:\\Users\\Kunal\\Desktop\\Selenium\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "http://demo.guru99.com/test/newtours/";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page with the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Fire fox
driver.close();
}
}

Related

All though Xpath is showing all elements in selector hub from expected div ,selenium isn't capturing completely

I want to capture all the headers of a table using the selenium using the. I am using the
Xpath to capture the webelement using Xpath.
Xpath:
"//div[#class='dataTables_scrollHeadInner']//tr[#id='report-data-table-header-0']/th"
and it shows all the elements of the div i.e (9 th's),but when I capture it using selenium I only get 7 elements.
All the xpaths are mentioned are required the and the commented block of code is function call of homePage.view_report_values(textString).
Source Code:
package Onsight.Framework;
import java.util.ArrayList;
import java.util.List;
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 com.github.javafaker.Faker;
import avdhut.onsight.commonutils.BaseComponenets;
import avdhut.onsight.pageobject.HomePage;
import avdhut.onsight.pageobject.LoginPage;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Webtable {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
String urlString = "https://oinsightsvm1.westindia.cloudapp.azure.com:444/ctsOInsightsBMS/res/createReport.html";
String userEmailString = "User66";
String userPassword = "Avdhut#5201";
String titleString;
String textString="Clarita";
Faker faker = new Faker();
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
// options.addArguments("--ignore-ssl- errors=yes");
options.addArguments("--ignore-certificate-errors");
WebDriver driver = new ChromeDriver(options);
driver.get(urlString);
LoginPage loginPage = new LoginPage(driver);
loginPage.login_username(userEmailString);
loginPage.click0nsubmit();
loginPage.EnterPassword(userPassword);
loginPage.click0nsubmit();
HomePage homePage = new HomePage(driver);
homePage.view_report_values(textString);
}
}
//tr[class='odd'] em[class='fa fa-table']
Page Object file:
[![public class HomePage extends BaseComponenets {
WebDriver driver;
By createReportBy=By.cssSelector("button\[data-target*='#addReportModal'\]");
By baseTableBy=By.xpath("//table\[#id='query-table'\]");
By report_name_rowsBy=By.xpath("//tbody/tr");
By viewBy=By.xpath("//button\[contains(#title,'View')\]");
By view_dataBy=By.xpath("//button\[contains(#title,'View data')\]");
By dataBy=By.xpath("//table\[#id='reportDataTable-0'\]/tbody/tr ");
By teable_headingBy=By.xpath("//div\[#class='dataTables_scrollHeadInner'\]//tr\[#id='report-data-table-header-0'\]/th");
By next_paginateBy=By.xpath("//li\[#id='reportDataTable-0_next'\]");
By paginateBy=By.xpath("//div\[#id='reportDataTable-0_paginate'\]/ul\[#class='pagination'\]/li\[#class='paginate_button page-item '\]");
By no_of_pagesBy=By.xpath("//div\[#id='reportDataTable-0_info'\]");
public HomePage(WebDriver driver) {
super(driver);
// TODO Auto-generated constructor stub
this.driver=driver;
}
public void CreateReport_Button() {
IsClickable(createReportBy);
WebElement create_Report=driver.findElement(createReportBy);
create_Report.click();
}
public void get_headers() {
IsVisible(teable_headingBy);
List<WebElement> table_headerElement=driver.findElements(teable_headingBy);
for (WebElement header : table_headerElement) {
System.out.println(header.getText());
}
}
public void view_report_values(String report_title) throws InterruptedException {
IsVisible(baseTableBy);
WebElement baseTable=driver.findElement(baseTableBy);
List<WebElement> table_roWebElement=baseTable.findElements(report_name_rowsBy);
List<String> list=new ArrayList<String>();
for (WebElement webElement : table_roWebElement) {
if (webElement.getText().contains(report_title)) {
System.out.println(true);
IsClickable(viewBy);
System.out.println(webElement.getText());
webElement.findElement(viewBy).click();
IsClickable(view_dataBy);
driver.findElement(view_dataBy).click();
IsVisible(next_paginateBy);
List<WebElement> pagebar=driver.findElements(paginateBy);
for(WebElement page:pagebar) {
list.add(page.getText());
}
// name(list);
if (driver.findElement(next_paginateBy).isDisplayed()) {
for (int i = 1; i <= 30; i++) {
if (i==1) {
System.out.println("first_page");
get_headers();
}
// Thread.sleep(10000);
}
} else {
}
//
}
}][1]][1]
Console O/P:
TIMESTAMP
SOURCE NAME
SOURCE STATE
ACK STATE
MESSAGE TEXT
LAST NOTES
After that I googled and then found some articles and then In order to test I used console of google chrome
document.getElementsByClassName('dataTables_scrollBody').scrollLeft += 250", "
It gave me an error , then, I used:
document.getElementsByClassName('dataTables_scrollBody').scrollLeft +=250
Then,
I got the message :
NaN
This is the table I want to scroll also the classname mentioned in belongs to same div of the table in image
After login, add the below code and try:
driver.findElement(By.xpath(".//button[#class='toast-close-button']")).click();
driver.findElement(By.xpath("(.//td[text()='random']//parent::tr//td/button)[1]")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("(.//div[#class='modal-content']//button[#type='button' and #title='View data'])[1]")).click();
Thread.sleep(2000);
JavascriptExecutor js=(JavascriptExecutor)driver;
List<WebElement> headers = driver.findElements(By.xpath(".//div[#class='dataTables_scrollHead']//th"));
System.out.println("Total no. of headers: " + headers.size());
System.out.println("Headers: ");
System.err.println("========");
for (WebElement header : headers) {
js.executeScript("arguments[0].scrollIntoView(true);", header);
System.out.println(header.getText());
}
Output:
Total no. of headers: 9
Headers:
========
TIMESTAMP
SOURCE NAME
SOURCE STATE
ACK STATE
MESSAGE TEXT
LAST NOTES
ALL NOTES
ALARM VALUE
LOW LIMIT
I answered your previous question also, check that, if it works for you, mark that as answer.

Why is selenium web driver doesn't find the textfield on this website?

What should happen is that these textfields for login should be filled and the the login button should be pressed (some sort of auto login)
Here is the link to the webpage: Telekom Email Login Page
This are the three methods I'm using:
public String exportDriver() throws IOException {
final InputStream IEDriverStream = getClass().getResourceAsStream("/Driver/IEDriverServer.exe");
final File ieDriverServer = FileWebOpener.stream2file(IEDriverStream, "IEDriverServer", ".exe");
System.setProperty("webdriver.ie.driver", ieDriverServer.getAbsolutePath());
return ieDriverServer.getAbsolutePath();
}
public void goToWebsite(String url) {
driver = new InternetExplorerDriver();
driver.get(url);
}
public void setUsernameAndPassword(String username, String password, int Website) throws InterruptedException {
try{ new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys(username);
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys(password);
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
} catch(Exception e){
e.printStackTrace();
}
}
This is the exception I get
I believe you first need to :
click on the username field.
send username keys.
click on the password field.
send password keys.
click on the submit button
try- findElement(By.Class("line_normalized clear relative").click);
this find the username textfield class and clicks on it if Im not mistaken, syntax may be wrong but the idea is the same.
same goes for the password field, find the class/id name of the field & click on it before sending keys.
I have tested it with C# and get the right behaviors.
Below is the code:
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System;
namespace IEWebDriver
{
class Program
{
static void Main(string[] args)
{
string username = args.Length > 1 ? args[1] : "test";
string password = args.Length > 1 ? args[2] : "test";
IWebDriver driver = new InternetExplorerDriver();
//IWebDriver driver = new ChromeDriver();
//Navigate to test URL
driver.Navigate().GoToUrl("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
try
{
new WebDriverWait(driver, new TimeSpan(0, 0, 20, 60)).Until(c => c.FindElement(By.CssSelector("input#user[name='pw_usr']"))).SendKeys(username);
driver.FindElement(By.CssSelector("input#pw_pwd[name='pw_pwd']")).SendKeys(password);
driver.FindElement(By.CssSelector("input.button.standard_button_size.large#pw_submit")).Click();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
//Close the browser
driver.Quit();
//driver.Close();
}
}
}
Hi as you want to enter the username and it's a input field, you need to locate the input tag.
you can try the following locator to locate the text input area and perform the sendkeys operation:
driver.findElement(By.xpath("//div[#class='line_normalized clear relative']/input")).sendKeys("username");
I suspect your application is too slow.Use WebDriverWait to identify the element and then Sendkeys operation.
package pkg1;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class testIE {
public static void main(String[] args) {
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + File.separator + "\\Executables\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement user= wait.until(ExpectedConditions.elementToBeClickable(By.id("user")));
user.sendKeys("user12345#gmail.com");
WebDriverWait wait1 = new WebDriverWait(driver, 30);
WebElement pass= wait1.until(ExpectedConditions.elementToBeClickable(By.id("pw_pwd")));
pass.sendKeys("password");
}
}
Output
As you are accessing the website initially you need to induce WebDriverWait for the desired elements to be clickable and you can use either of the following Locator Strategies:
cssSelector:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#user[name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.cssSelector("input#pw_pwd[name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.cssSelector("input.button.standard_button_size.large#pw_submit")).click();
xpath:
driver.get("https://accounts.login.idm.telekom.com/idmip?openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.return_to=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Fverify%3FreturnToUrl%3Dhttps%3A%2F%2Femail.t-online.de%2Fem&openid.realm=https%3A%2F%2Ftipi.api.t-online.de&openid.assoc_handle=S4d53c348-b3f2-49a9-b13e-65ade0af6da4&openid.mode=checkid_setup&openid.ns.ext1=http%3A%2F%2Fopenid.net%2Fsrv%2Fax%2F1.0&openid.ext1.mode=fetch_request&openid.ext1.type.attr1=urn%3Atelekom.com%3Aall&openid.ext1.required=attr1&openid.ns.ext2=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Foauth2%2F1.0&openid.ext2.client_id=10LIVESAM30000004901PORTAL00000000000000&openid.ext2.scopes=W3sic2NvcGUiOiJzcGljYSJ9XQ%3D%3D&openid.ns.ext3=http%3A%2F%2Fidm.telekom.com%2Fopenid%2Fext%2F2.0&openid.ext3.logout_endpoint=https%3A%2F%2Ftipi.api.t-online.de%2Fsrp-auth%2FoneIdm%2Flogout&openid.ns.ext4=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fui%2F1.0&openid.ext4.mode=popup");
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#id='user' and #name='pw_usr']"))).sendKeys("Jannik");
driver.findElement(By.xpath("//input[#id='pw_pwd' and #name='pw_pwd']")).sendKeys("Jannik");
driver.findElement(By.xpath("//input[#class='button standard_button_size large' and #id='pw_submit']")).click();

java.net.MalformedURLException in Java Selenium code

I tried to run simple Java Selenium code but am getting this error - can anyone help me to figure it out?
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class test
{
public static void main(String[] args)
{
stem.setProperty("webdriver.chrome.driver","D:/apache-jmeter-3.1/bin/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
String Title = driver.getTitle();
//compare the actual title of the page with the expected one
if (Title.contentEquals("Google"))
{
System.out.println("Test Passed!");
}
else
{
System.out.println("Test Failed");
}
driver.close();
}
}
It seems you are using incorrect url in get() method. Try to use get() method like below:
driver.get("http://www.google.com");
URL must contains "http://" or "https://" to define its protocol.
Fix in your code, you can try below once inside WebDriver Sampler :
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class test {
public static void main(String[] args) {
try{
System.setProperty("webdriver.chrome.driver","D:/apache-jmet‌​er-
3.1/bin/chromedri‌​ver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
String Title = driver.getTitle();
if (Title.contentEquals("Google")){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
driver.close();
} catch (Exception e){}
}
}
Use this:
System.setProperty("webdriver.chrome.driver","D://apache-jmeter-3.1//bin//chromedriver.exe");
As described in the javadoc for the System class:
Sets the system property indicated by the specified key.
First, if a security manager exists, its
SecurityManager.checkPermission method is called with a
PropertyPermission(key, "write") permission. This may result in a
SecurityException being thrown. If no exception is thrown, the
specified property is set to the given value.
Parameters:
key - the name of the system property. value - the value of the
system property.
Returns:
the previous value of the system property, or null if it did not have
one. Throws: SecurityException - if a security manager exists and
its checkPermission method doesn't allow setting of the specified
property. NullPointerException - if key or value is null.
IllegalArgumentException - if key is empty.

Cannot instantiate the type FirefoxDriver

Can seem to make this code execute. Compiler isn't instantiating the driver. what can I do to correct this.
package mypackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
org.openqa.selenium.support.ui.ExpectedConditions;
import
org.openqa.selenium.support.ui.WebDriverWait;
public class selenium {
public static void main(String[] args) {
WebDriver driver = (WebDriver) new FirefoxDriver();
WebDriverWait MyWaitlVar= new
WebDriverWait(driver, 10);
String baseUrl =
"http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page
witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals
(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
}

I see an error while running a package in Eclipse

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
This is my code.
package newpackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class MyClass {
public static void main(String[] args) {
//declaration and instatiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle ="";
//launch firefox and direct it to the URL
driver.get(baseUrl);
//get the actual value of the title
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed");
}else {
System.out.println("Test failed");
}
//close firefox
driver.close();
//exit the program
System.exit(0);
}
}
For selenium 3.0 and above it is mandatory to specify gecko.driver path. Add below lines to your code before initializing driver and error will not appear.
System.setProperty("webdriver.firefox.marionette","xxx\geckodriver.exe");
//xxx - path to geckodriver.exe file
WebDriver driver = new FirefoxDriver();

Categories

Resources