how to handle popups on website - java

title : handle popup box
selenium code for handle div pop on same windows
//package booking;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Dubai {
public static void main(String[] args) throws Exception {
System.setProperty ("webdriver.chrome.driver",
"C:\\Users\\miyau\\Desktop\\test\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.thomascook.in/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[text()='Not Now']")).click();
System.out.println("operation complit");
}
}
// snapshot of popup## Heading ##

You can try this code:
public class Dubai {
public static void main(String[] args) throws InterruptedException {
try {
public static void main(String[] args) throws Exception {
System.setProperty ("webdriver.chrome.driver",
"C:\\Users\\miyau\\Desktop\\test\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebDriverWait wait=new WebDriverWait(driver,50 );
driver.manage().window().maximize();
Thread.sleep(500);
driver.get("http://www.thomascook.in/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='__st_preview_frame_bpn']")));
System.out.println(driver.findElement(By.xpath("html/body/div[1]/div[2]/div[3]/span")).getText());
driver.findElement(By.id("__st_bpn_no")).click();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Related

How to handle OTP popup?

I am trying to inspect elements on the webpage but as soon as popup comes it shows an error of element not visible.
I am using correct locators for the same, also I tried using different timeouts for the thread.
Sleep it is not working.
When I click on view source it is showing the same frame as well.
I wrote the below code:
\\\
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class INDIALENDS {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.navigate().to("https://indialends.com/");
driver.manage().window().maximize();
String s=driver.getCurrentUrl();
System.out.println(s);
driver.getTitle();
String x=driver.getTitle();
System.out.println(x);
\\\
\\\
driver.findElement(By.linkText("Personal Loan")).click();
driver.findElement(By.name("li_display_name")).sendKeys("RAVNEET KAUR");
driver.findElement(By.id("email")).sendKeys("ravneetkaur#indialends.com");
driver.findElement(By.name("pincode")).sendKeys("122018");
driver.findElement(By.id("employment")).click();
Thread.sleep(5000);
driver.findElement(By.id("salaried12")).click();
Thread.sleep(5000);
driver.findElement(By.id("companyName")).sendKeys("INDIALENDS");
Thread.sleep(10000);
driver.findElement(By.id("monthlyIncome")).sendKeys("34000");
driver.findElement(By.id("mobile")).sendKeys("5282273663");
driver.findElement(By.id("li_submit")).click();
Thread.sleep(5000);
\\\
\\\
Thread.sleep(25000);
driver.findElement(By.id("li_submit")).click();
Thread.sleep(25000);
//new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.id("control__indicator")));
driver.findElement(By.id("li_agree")).click();
String s2=driver.getCurrentUrl();
\\\
\\\
System.out.println(s2);
driver.getTitle();
String x1=driver.getTitle();
System.out.println(x1);
Thread.sleep(5000);
}
}
\\\
This the full code. Please try it from your end.
public class MyTesting {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "F:\\Swaroop\\ChromeVersion87\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://indialends.com/");
driver.manage().window().maximize();
String s = driver.getCurrentUrl();
System.out.println(s);
driver.getTitle();
String x = driver.getTitle();
System.out.println(x);
driver.findElement(By.linkText("Personal Loan")).click();
driver.findElement(By.name("li_display_name")).sendKeys("RAVNEET KAUR");
driver.findElement(By.id("email")).sendKeys("ravneetkaur#indialends.com");
driver.findElement(By.name("pincode")).sendKeys("122018");
driver.findElement(By.id("employment")).click();
Thread.sleep(5000);
driver.findElement(By.id("salaried12")).click();
Thread.sleep(5000);
driver.findElement(By.id("companyName")).sendKeys("INDIALENDS");
Thread.sleep(10000);
driver.findElement(By.id("monthlyIncome")).sendKeys("34000");
driver.findElement(By.id("mobile")).sendKeys("5282273663");
driver.findElement(By.id("li_submit")).click();
Thread.sleep(5000);
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("control-indicator"))).click();
String s2 = driver.getCurrentUrl();
System.out.println(s2);
driver.getTitle();
String x1 = driver.getTitle();
System.out.println(x1);
Thread.sleep(5000);
}
}

Chrome run by Selenium doesn't advance farther than mazimizing the browser window

I'm trying run a test searching for tents on eBay, but the test stops after maximizing the window
package com.xxx.yyy;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class eBay {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe");
WebDriver driver = new ChromeDriver();
mySleep(3);
driver.manage().window().maximize();
String url = "www.ebay.com";
driver.get(url);
driver.findElement(By.className("gh-tb ui-autocomplete-input"));
WebElement textbox = driver.findElement(By.id("gh-ac-box"));
textbox.sendKeys("tent");
mySleep(1);
driver.quit();
}
private static void mySleep(int t) {
try {
Thread.sleep(t * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
The Chrome window opens, is maximized, but nothing happens next and in Eclipse, I can see this error:
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument

How to open links consecutelively from CSV

I'm trying to open links one-by-one from a CSV-file.
So far I've only managed to launch Chrome. How do you add the data source to the code? After that you should be able to simply open every link as loop.
Thank you!
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\user\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("URL from CSV as Loop");
}
}
Please check below piece of code and lets know if it resolved your problem
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchChrome {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\user\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
readFileLineByLineUsingBufferedReader(driver);
}
public static void readFileLineByLineUsingBufferedReader(WebDriver driver) {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("CSV_FILE_PATH"));
String line = reader.readLine();
while(line != null) {
driver.get(line);
// You may give some pause here (Thread.sleep(2000));
// read next line
line = reader.readLine();
}
reader.close();
} catch(IOException io) {
io.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
}

Selenium webdriver gecko error: The path to the driver must be set by webdriver.gecko

trying to get selenium working but didn't work and showing me below error.
Libs:junit4.12, selenium-java-3.4, selenium-server-standalone-3.5
Could anyone take a look and tell me whats wrong with the code or what is
missing, please.
Every time when I start the test, I get 2 errors.
Screenshot embedded at the bottom showing the error details.
My code as below:
package com.example.tests;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;
public class TestCase16 {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "URL";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ayre1de\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("URL");
}
#Test
public void testCaseAcandoIntranet() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("IDToken1")).clear();
driver.findElement(By.id("IDToken1")).sendKeys("XXX");
driver.findElement(By.id("IDToken2")).clear();
driver.findElement(By.id("IDToken2")).sendKeys("*XXX");
driver.findElement(By.name("Login.Submit")).click();
driver.findElement(By.id("yui_patched_v3_18_1_1_1502961326988_317")).click();
driver.findElement(By.xpath("//a[#id='_com_liferay_product_navigation_user_personal_bar_web_portlet_ProductNavigationUserPersonalBarPortlet_sidenavUserToggle']/span/div")).click();
driver.findElement(By.linkText("Abmelden")).click();
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}
`
The problem is you are initializing webdriver instance two times like below. specially you have missed specifying the System.setProperty for the first instance of webdriver where exactly your code is failing:-
driver = new FirefoxDriver(); // 1st instance
baseUrl = "URL";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ayre1de\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver(); // 2nd instance
driver.get("URL");
Now delete above two lines and write your code like below :-
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ayre1de\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("URL");

Selenium Java - java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property

My FireFox Version 49.0.1
Selenium Version: Selenium-java-3.0.0-beta3
Java: 8.0.1010.13
I have replaced all the existing Selenium Jar Files with the new files. Added the gecko.Driver to my code still I am seeing this message:
Error Message:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property;
My Code:
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AbstractPage {
WebDriver Driver =new FirefoxDriver();
#Before
public void Homescreen() throws InterruptedException
{
System.getProperty("Webdriver.gecko.driver", "C:/geckodriver.exe");
System.setProperty("Webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Driver.get("URL");
Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#After
public void TestComplete() {
Driver.close();
}
#Test
public void Projects() {
Driver.findElement(By.id("login-form-username")).sendKeys("Login");
Driver.findElement(By.id("login-form-password")).sendKeys("Password");
Driver.findElement(By.id("quickSearchInput")).sendKeys("ID");
}
}
You can remove main() method from the code. unzip your gecko driver and save it to your local system with wires.exe.
my sample class path is
G:\ravik\Ravi-Training\Selenium\Marionette for firefox\wires.exe
public class AbstractPage
{
WebDriver Driver;
System.setProperty("WebDriver.gecko.Driver", "C:\\TEMP\\Temp1_geckodriver-v0.10.0-win64.zip");
Driver=new FirefoxDriver();
#Before
public void Homescreen() throws InterruptedException
{
Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Driver.get("https://QualityAssurance.com");
Driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Driver.findElement(By.id("login-form-username")).sendKeys("Login");
Driver.findElement(By.id("login-form-password")).sendKeys("Password");
//JavascriptExecutor js = (JavascriptExecutor) Driver;
//js.executeScript("document.getElementById('login-form-password').setAttribute('value', val );");
}
#After
public void TestComplete() {
Driver.close();
}
#Test
public void Projects() {
Driver.findElement(By.id("quickSearchInput")).sendKeys("WMSSE-229");
Please replace below line
System.setProperty("WebDriver.gecko.Driver", C:\\TEMP\\Temp1_geckodriver-v0.10.0-win64.zip");
You should pass geckodriver.exe not Zip file.
String driverPath = "F:/Sample/Selenium3Example/Resources/";
System.setProperty("webdriver.firefox.marionette", driverPath+"geckodriver.exe");
You can make your code more cleaner when posting here.
You must do something like this:
System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
This is a working example, it make you know the context what the above code snippet used
package selenium;
import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Junit4FirefoxJava {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.gecko.driver", "C:/geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
driver = new FirefoxDriver();
baseUrl = "http://www.bing.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void testJunit4IeJava() throws Exception {
driver.get(baseUrl + "/");
driver.findElement(By.id("sb_form_q")).click();
driver.findElement(By.id("sb_form_q")).clear();
driver.findElement(By.id("sb_form_q")).sendKeys("NTT data");
driver.findElement(By.id("sb_form_go")).click();
driver.findElement(By.linkText("NTT DATA - Official Site")).click();
driver.findElement(By.id("js-arealanguage-trigger")).click();
driver.findElement(By.linkText("Vietnam - English")).click();
driver.findElement(By.id("MF_form_phrase")).clear();
driver.findElement(By.id("MF_form_phrase")).sendKeys("internet");
driver.findElement(By.cssSelector("input.search-button")).click();
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
private boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alertText;
} finally {
acceptNextAlert = true;
}
}
}

Categories