How to handle OTP popup? - java

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);
}
}

Related

Main function not executing my second function

I have made two functions and I have called them in main. When I execute the program it only runs the first function in the main after that program stops . if i paste the second function in main above the first one it only runs the function that is defined first but does not runs the second function.
package test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class Testing {
public static void login_and_widthdrawl() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/home/ctp-016/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://staging.cintech.com/login");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement email = driver.findElement(By.id("email"));
email.sendKeys("zahid12#mailinator.com");
WebElement password = driver.findElement(By.id("password"));
password.sendKeys("zahid12#");
WebElement textbox = driver.findElement(By.id("login"));
textbox.click();
// checking widthdrawl functionality from here
Thread.sleep(4000);
driver.findElement(By.id("navbarDropdown")).click();
driver
.findElement(By.xpath("/html/body/app-root/div/app-header-guest/section[1]/nav/div/div/ul/li[3]/div/a[2]"))
.click();
// SELECTING BTC CRYPTO AND ENTRING VALUES FROM HERE
// WebDriverWait wait = new WebDriverWait(driver, 10);
// WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/app-root/div/app-withdrawal/section/div[1]/div/div/div[1]/div/div[1]/span")));
// ele.click();
Thread.sleep(2000);
WebElement submit1 = driver.findElement(By.xpath("//*[#id=\"select2\"]"));
submit1.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(2000);
WebElement submit12 = driver.findElement(By.xpath("/html/body/span/span/span[2]/ul/li[2]"));
submit12.click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(2000);
WebElement volume =
driver.findElement(By.xpath("//*[#id=\"wrapper\"]/div[1]/div/div/div[1]/div/div[3]/form/div[1]/input"));
volume.sendKeys("0.001");
WebElement wallet = driver.findElement(By.xpath(
"/html/body/app-root/div/app-withdrawal/section/div[1]/div/div/div[1]/div/div[3]/form/div[2]/div/input"));
wallet.sendKeys("1asas");
WebElement click = driver.findElement(
By.xpath("/html/body/app-root/div/app-withdrawal/section/div[1]/div/div/div[1]/div/div[3]/form/button"));
click.click();
// WebElement secret2 = driver.findElement(By.xpath("//*[#id=\"VerifyTwoFA\"]/div/div/div/app-verify-twofactor/div/div/form/div[1]/input"));
// secret2.sendKeys("0");
WebElement submit2 = driver.findElement(By.id("loginButton"));
submit2.click();
Thread.sleep(2000);
Alert alert = driver.switchTo().alert();
alert.accept();
//driver.quit();
// clear the fields
WebElement volumeclear =
driver.findElement(By.xpath("//*[#id=\"wrapper\"]/div[1]/div/div/div[1]/div/div[3]/form/div[1]/input"));
volumeclear.clear();
WebElement walletclear =
driver.findElement(By.xpath("//*[#id=\"wrapper\"]/div[1]/div/div/div[1]/div/div[3]/form/div[2]/input"));
walletclear.clear();
}
public static void ether() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/home/ctp-016/Downloads/chromedriver");
WebDriver driver = new ChromeDriver();
// driver = null;
driver.get("http://staging.cintech.com/login");
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// Thread.sleep(2000);
// WebElement selected_value_from_combo = driver.findElement(By.xpath("//*[#id=\"select2-mySelect2-result-2d1f-3\"]"));
// selected_value_from_combo .click();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Thread.sleep(2000);
WebElement eth_volume =
driver.findElement(By.xpath("//*[#id=\"wrapper\"]/div[1]/div/div/div[1]/div/div[3]/form/div[1]/input"));
eth_volume.sendKeys("1");
WebElement eth_wallet =
driver.findElement(By.xpath("//*[#id=\"wrapper\"]/div[1]/div/div/div[1]/div/div[3]/form/div[2]/input"));
eth_wallet.sendKeys("1asas");
WebElement click_widthdrawl = driver.findElement(
By.xpath("/html/body/app-root/div/app-withdrawal/section/div[1]/div/div/div[1]/div/div[3]/form/button"));
click_widthdrawl.click();
// WebElement secret_eth = driver.findElement(By.xpath("//*[#id=\"VerifyTwoFA\"]/div/div/div/app-verify-twofactor/div/div/form/div[1]/input"));
// secret_eth.sendKeys("0");
WebElement submit_eth = driver.findElement(
By.xpath("//*[#id=\"VerifyTwoFA\"]/div/div/div/app-verify-twofactor/div/div/form/div[2]/button"));
submit_eth.click();
Thread.sleep(2000);
Alert alert_eth = driver.switchTo().alert();
alert_eth.accept();
// clear the fields
WebElement ethvolumeclear =
driver.findElement(By.xpath("//*[#id=\"wrapper\"]/div[1]/div/div/div[1]/div/div[3]/form/div[1]/input"));
ethvolumeclear.clear();
WebElement ethwalletclear =
driver.findElement(By.xpath("//*[#id=\"wrapper\"]/div[1]/div/div/div[1]/div/div[3]/form/div[2]/input"));
ethwalletclear.clear();
}
public static void main(String[] args) throws InterruptedException {
ether();
login_and_widthdrawl();
}
}
not sure why it s happening, try this to know actual problem:-
public static void main(String[] args) throws InterruptedException {
try{
ether();
login_and_widthdrawl();
}catch(Exception e){
e.printStackTrace();
}
}

how to handle popups on website

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();
}
}
}

How to open multiple tab in the same browser?

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class newtab {
public static void main(String[] args) {
// TODO Auto-generated method stub
String driverpath = "geckodriver path\\";
System.setProperty("webdriver.gecko.driver",driverpath+"geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.get("http://www.gmail.com");
}
}
In my code I want to open two tabs in the same browser, but this code is opening only one tab. How do I open multiple tabs in the same browser?
You are not able to open Gmail in other tab because focus is still at 1st window, because selenium identifies a particular window to work with using its window handles hence you have to first switch that particular window using handle like: driver.switchTo().window(handle value)
here is the complete code:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "c:\\SRP\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
String newTab =null;
String baseTab = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
Set <String> allTabs = driver.getWindowHandles();
allTabs.remove(baseTab);
Iterator<String> itr = allTabs.iterator();
while(itr.hasNext()){
newTab = (String) itr.next();
}
driver.switchTo().window(newTab);
driver.get("http://www.gmail.com");
}
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NewTab {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\User-024\\Downloads\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
String parent = driver.getWindowHandle();
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
Set<String> browsers = driver.getWindowHandles();
for (String i : browsers) {
if (!i.equals(parent)) {
driver.switchTo().window(i);
driver.get("http://www.gmail.com");
}
}
}
}
This code is working for me.
I have written simple code and it worked for me. It is opening two different URLs in different tabs of same browser.
public void cls(){
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
String baseTab = driver.getWindowHandle();
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.get("http://gmail.com");
}

Exception in thread “main” org.openqa.selenium.StaleElementReferenceException: Element not found in the cache

Can someone help with it ?
I have error
"Exception in thread “main” org.openqa.selenium.StaleElementReferenceException: Element not found in the cache"
Why showing this error?
I need to hover on each category menu than click on each text in sub-menu.
public class santander {
private static WebDriver driver = null;
public static JavascriptExecutor js = (JavascriptExecutor) driver;
public static void main(String[] args) throws FileNotFoundException, InterruptedException, IOException {
// TODO Auto-generated method stub
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.santander.co.uk/uk/index");
driver.manage().window().maximize();
driver.get("http://www.santander.co.uk/uk/index");
JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver, 10);
String submenutxtlinks = "submenu.txt";
List<String> submenu = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader(submenutxtlinks));
String line;
while ((line = reader.readLine()) != null) {
submenu.add(line);
}
reader.close();
Actions action = new Actions(driver);
/*
WebElement menu = driver.findElement(By.linkText("Current Accounts"));
action.moveToElement(menu).perform();
WebElement submenu = driver.findElement(By.linkText("See all current accounts"));
action.moveToElement(submenu);
action.click();
action.perform();
*/
// String title = driver.getTitle();
// wait.until(ExpectedConditions.titleIs(title));
// driver.navigate().back();
//Loop to read all lines one by one from file and print It.
// while((menu = BR.readLine())!= null && !menu.isEmpty()){
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Action hovering = action.moveToElement(a).build();
// hovering.perform();
//action.moveToElement(a).perform();
//action.clickAndHold(a).perform();
WebElement a = driver.findElement(By.cssSelector("#nav > div.navMain > div.nav_menu > nav > ul > li:nth-child(1) > a"));
Action hovering = action.moveToElement(a).build();
//Thread.sleep(2000);
for (int i=0;i<=submenu.size()-1;i++ ) {
//String b = submenu.get(i);
// System.out.println(b);
//WebElement b = driver.findElement(By.xpath(submenu.get(i)));
try{
//Your code which causes exception
hovering.perform();
//action.moveToElement(b).click(b).build().perform();
Thread.sleep(1000);
clickAnElementByLinkText(submenu.get(i));
//b.click();
Thread.sleep(1000);
/*
wait.until(ExpectedConditions.visibilityOf(b));
action.moveToElement(b);
action.click();
action.perform();
*/
// wait.until(ExpectedConditions.titleIs(title));
driver.navigate().back();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
// driver.get("http://www.santander.co.uk/uk/index");
//driver.navigate();
Thread.sleep(2000);
// driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
catch(org.openqa.selenium.StaleElementReferenceException e){
//Repeat the code in try
}
}
// }
driver.close();
}
public static void clickAnElementByLinkText(String linkText) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(linkText)));
driver.findElement(By.xpath(linkText)).click();
}
}
Dont mix ImplicitWait ExplicitWait and Thread.Sleep all in the same context.
Learn when and where those should be used.
The hoverElement wont work because the driver navigates to different page and element no longer exists you have find it again inside the forLoop
For the sake of test I've hardcoded the submenulist
This code will do what you've asked for
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ListTest {
static WebDriver driver;
public static void main(String[] args) {
List<String> submenu = new ArrayList<>(Arrays.asList(new String[]{"See all current accounts", "1|2|3 Current Account", "Everyday Current Account", "Basic Current Account", "Choice Current Account"}));
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.santander.co.uk/uk/index");
for (String sMenu : submenu) {
WebElement a = driver.findElement(By.cssSelector("#nav > div.navMain > div.nav_menu > nav > ul > li:nth-child(1) > a"));
new Actions(driver).moveToElement(a).build().perform();
clickAnElementByLinkText("//li[#role='listitem']/a[normalize-space(text())='" + sMenu + "']");
driver.navigate().back();
}
driver.quit();
}
public static void clickAnElementByLinkText(String linkText) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(linkText))).click();
}
}
You are trying to access an element which does not exists.
The code collects the elements on the page,
then the code clicks an element which changes the source
then it try to access an object he collected before but it is not longer there (what you see is a new one).
You can enter the finding of the elements process into the loop and loop changing the 'i' element in every iteration (If you must click the element in every iteration).

How to do web scraping using htmlunitsriver?

i am getting somthing like this
Hi i am scraping a web page using Selenium Webdriver an i am able to achieve my data but problem is that this directly interact with browser and i dont want to open a web browser and want to scrape all data as it is
How can i achieve my goal
Here is my code
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class GetData {
public static void main(String args[]) throws InterruptedException {
String sDate = "27/03/2014";
WebDriver driver = new FirefoxDriver();
String url="http://www.upmandiparishad.in/commodityWiseAll.aspx";
driver.get(url);
Thread.sleep(5000);
// select barge
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
// click buttonctl00_ContentPlaceHolder1_txt_rate
Thread.sleep(3000);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
Thread.sleep(5000);
//get only table tex
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
String htmlTableText = findElement.getText();
// do whatever you want now, This is raw table values.
System.out.println(htmlTableText);
driver.close();
driver.quit();
}
}
My updated New code
import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.Select;
public class Getdata1 {
public static void main(String args[]) throws InterruptedException {
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3_6);
driver.get("http://www.upmandiparishad.in/commodityWiseAll.aspx");
System.out.println(driver.getPageSource());
Thread.sleep(5000);
// select barge
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
String sDate = "12/04/2014"; //What date you want
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
Thread.sleep(3000);
//get only table tex
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
String htmlTableText = findElement.getText();
// do whatever you want now, This is raw table values.
System.out.println(htmlTableText);
driver.close();
driver.quit();
}
}
Thanks in advance
Use HtmlUnit or HtmlUnitDriver by Selenium
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_17);
driver.get("http://www.upmandiparishad.in/commodityWiseAll.aspx");
System.out.println(driver.getPageSource());
Thread.sleep(5000);
// select barge
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
String sDate = "12/04/2014"; //What date you want
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
Thread.sleep(3000);
//get only table tex
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
String htmlTableText = findElement.getText();
// do whatever you want now, This is raw table values.
System.out.println(htmlTableText);
driver.close();
driver.quit();
To get tabular output, you can try something like this..
String arrCells[] = htmlTableText.split(" ");
Boolean bIsANumber = false;
for(int i = 0; i < arrCells.length; i++) {
try {
int tmp = Integer.parseInt(arrCells[i]);
bIsANumber = true;
}
catch(Exception ex) {
bIsANumber = false;
}
if(bIsANumber) {
System.out.print("\n"+arrCells[i]+"\t");
}
else {
System.out.print(arrCells[i]+"\t");
}
}

Categories