Below Is the site page
I'm trying to click on Continue to Netbanking button. But I am unable to do that. I have used xpath but its not working. Here is xpath which I've tried :
driver.findElement(By.xpath(".//*[#id='wrapper']/div[6]/a/img")).click();
Steps:
Open URl http://www.hdfcbank.com
Click on Login button on website. New popup will get open.
Click on "Continue on Netbanking". THIS IS NOT WORKING
Here is code:
driver.findElement(By.id("loginsubmit")).click();
Thread.sleep(3000);
Set<String> set = driver.getWindowHandles();
Iterator<String> it = set.iterator();
System.out.println(set.size());
for( String windowTab : set){
if(!windowTab.equalsIgnoreCase(MainWindow)){
driver.switchTo().window(it.next());
driver.manage().window().maximize();
String Wdinw2 = driver.getWindowHandle();
Thread.sleep(10000);
System.out.println(driver.getTitle());
driver.findElement(By.xpath(".//*[#id='wrapper']/div[6]/a/img")).click();
break;
}
}
Console :
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element:
{"method":"xpath","selector":".//*[#id='wrapper']/div[6]/a/img"}
Try to use below code and let me know the result:
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);}
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("img[alt='continue']"))).click();
To switch back to main window (if you need):
driver.switchTo().window(winHandleBefore);
Related
Driver is unable to find element on the page even when its present
. Basically Im just trying that when a popup (ad popup which we see in many website) is present it should get clicked. Below is the code :
System.setProperty("webdriver.gecko.driver","C:\\SeleniumDriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
driver.get("https://www.sportskeeda.com/wwe");
//WebElement popup=driver.findElement(By.className("bullbg"));
WebElement popup=driver.findElement(By.xpath("//div[contains(text(),'Close')]"));
if (popup.isDisplayed())
{
System.out.println("True");
}
else
{
System.out.println("False");
}
ERROR
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element: //div[contains(text(),'Close')]
Please click here to check there is a popup which gets display
Try scrolling to the element first, something might be in the way
WebElement element = driver.findElement(By.id("id"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Use Instead:
Boolean popup=driver.findElement(By.xpath("//div[contains(text(),'Close')]")).isDisplayed();
if (popup == true)
{
System.out.println("True");
}
else
{
System.out.println("False");
}
In my project, there has one advertisement provider who shows us pop up advertisement but Problem is this pop-up will come it's not fixed.Sometimes add not show sometimes comes this pop up after 1 minute and interrupts my test case.
I write a code for close this popup but it's not a perfect solution I think..If anyone can help?
boolean Imclose = wd.findElement(By.xpath(".//*[#class='IM_overlay_close_container IM_overlay_close_button']")).isDisplayed();
if (Imclose == true) {
wd.findElement(By.xpath(".//*[#class='IM_overlay_close_container IM_overlay_close_button']")).click();
}
Try these code, to handle the unwanted pop up page. I have provided 180 seconds of wait. Then Click on close button inside the advertisement pop up page.
After click on advertisement pop up page, another window is getting open, So I have to redirect to my main window, then only further code will execute.
driver.get("https://www.rentbyowner.com/usa");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
try
{
WebDriverWait wait = new WebDriverWait(driver, 180);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//div[contains(#class,'IM_overlay_foreground')]"))));
driver.findElement(By.xpath("//div[contains(#class,'IM_overlay_foreground')]//span[#class='IM_close_text']")).click();
try
{
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
//driver.switchTo().window(winHandle).close();
}
driver.switchTo().window(winHandleBefore);
//Verify purpose written code, weather my driver is getting move to parent window or not.
WebDriverWait element = new WebDriverWait(driver, 10);
element.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("top_k_search"))));
driver.findElement(By.id("top_k_search")).clear();
System.out.println("Try block");
}
catch(Exception a)
{
System.out.println("Inner Catch");
}
}
catch(Exception e)
{
System.out.println("Element not present");
//Provide your code here..
}
I observed that this is a normal pop-up overlay over the webpage and not an alert and it appears almost within 60 seconds. So, the below code might just work for you.
//Waiting for the Popup to appear
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(#class,'IM_overlay_foreground')]")));
//Clicking on the 'Close' text to close the popup.
driver.findElement(By.xpath("//div[contains(#class,'IM_overlay_foreground')]//span[#class='IM_close_text']")).click();
NOTE: You can increase the time from 60 to 90, in case it appears to take longer. Thing is, whenever the popup appears within the timeframe set, it is handled(as in Closed).
Actually it is coming after 30-50 seconds so you have to wait and then click on close button.
System.setProperty("webdriver.chrome.driver", "E:\\software and tools\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.rentbyowner.com/usa");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
WebElement ad=driver.findElement(By.xpath(".//*[#id='IM_target_overlay']/div/div/div/div[1]/a/div"));
WebDriverWait wait= new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(ad));
ad.click();
Refer this link for more information:
Handling Alerts in Selenium Webdriver
Need help on following scenario(using Java):
Doing it manually like this: after filling in some info in a parent page, clicking Continue button on it,
<INPUT TYPE='button' VALUE='Continue' onClick='sendForm()'>
A child window(UserConfirmationPage) pops up with those info from its parent window, clicking the Continue button on the child page, posting the data to server.
<FORM NAME='userConf' ACTION='user.jsp' METHOD='post'>
Do you want to continue?<BR>
<INPUT TYPE='button' VALUE='Continue' onClick='createUser()'>
</FORM>
However, when I do it using Selenium Web Driver, on the parent page,
btnContinue.submit()
a child page pops up with those info from parent page just like what I got when I do it manually but parent does not exist any more. While using
btnContinue.click()
a blank child page is opened without getting any info from parent page and it also complains "session is lost".
I also tried:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();",btnContinue);
and
new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[#value='Continue']"))).click().perform();
but nothing works.
It seems that neither Submit() nor Click() could simulate what was done manually. Any idea?
Thanks a lot in advance!
You did not specify the language, so I assume you're using JAVA:
// since new tab has been opened - need to switch to this tab
// get a list of the currently open windows
Set<String> allTabs = driver.getWindowHandles();
// save the window handle for the current window
String programTab = driver.getWindowHandle();
// switching to the Save tab
String saveTab = ((String) allTabs.toArray()[1]);
driver.switchTo().window(saveTab);
// set timeout
// Click button
driver.findElement(By.id("buttonId")).click();
// set timeout
// switch back to the program tab
driver.switchTo().window(programTab);
Try this it is simplest and easiest code which help you to understand Parent and child scenario.
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://demo.guru99.com/popup.php");
driver.findElement(By.xpath("html/body/p/a")).click();
// return the parent window name as a String
String parentWindow=driver.getWindowHandle();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Pass a window handle to the other window
for(String childWindow: driver.getWindowHandles())
{
System.out.println("child");
//switch to child window
driver.switchTo().window(childWindow);
//find an element and print text of it
WebElement textLabel=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println(" text: "+textLabel.getText());
driver.close();
}
System.out.println("Come to parent window");
//switch to Parent window
driver.switchTo().window(parentWindow);
//find an element and print text of it
WebElement logotext=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println("text: "+logotext.getText());
driver.close();
}
I am trying to read object on a new tab but driver focus is not shifting on that. Here is a scenario:
1. Open Gmail.
2. Click on "term" link below Gmail site. New Tab will get open.
3. Read any object from new tab of term link.
Unable to do step 3. Here is code
WebDriver driver= new ChromeDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
String Mainwindow = driver.getWindowHandle();
boolean Term = driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).isDisplayed();
if(Term){
driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).click();
}
else{
driver.findElement(By.xpath("html/body/footer/div/div[5]/div[1]/a[4]")).click();
// this is written for IE and Mozila
}
Set<String> set = driver.getWindowHandles();
System.out.println(set.size());
Iterator<String> it = set.iterator();
String main =it.next();
String maintab =it.next();
System.out.println(maintab);
driver.switchTo().window(maintab);
Thread.sleep(10000);
driver.findElement(By.xpath("//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_DOWN);
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_UP);
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.BACK_SPACE);
driver.switchTo().window(main);
}}
Use Array List instead of Set. As Set stores the elements by using a mechanism called hashing. So String 'main' or 'mainTab' may not have right window handle to switch.
May be you need to try this:
driver.switchTo().window("windowName");
Webdriver is successfully able to switch to the new tab. However, you are trying to invoke sendKeys API on a control which is not an input element.
If you just want to scroll page down and up in the new tab, you can do that using JavascriptExecutor, as shown in the below code:
WebDriver driver= new ChromeDriver();
driver.get("http://gmail.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(2000);
String Mainwindow = driver.getWindowHandle();
boolean Term = driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).isDisplayed();
if(Term){
driver.findElement(By.xpath("//*[#id='footer-list']/li[3]/a")).click();
}
else{
driver.findElement(By.xpath("html/body/footer/div/div[5]/div[1]/a[4]")).click();
// this is written for IE and Mozila
}
Set<String> set = driver.getWindowHandles();
System.out.println(set.size());
Iterator<String> it = set.iterator();
String main =it.next();
String maintab =it.next();
System.out.println(maintab);
driver.switchTo().window(maintab);
Thread.sleep(10000);
//Using JavaScriptExecutor
//Scroll page down
JavascriptExecutor jscriptExec = (JavascriptExecutor)driver;
jscriptExec.executeScript("window.scrollBy(0,250)", "");
//Scroll page up
jscriptExec.executeScript("window.scrollBy(0,-250)", "");
//driver.findElement(By.xpath("//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_DOWN);
//Thread.sleep(3000);
//driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.PAGE_UP);
//Thread.sleep(3000);
//driver.findElement(By.xpath(".//*[#id='maia-nav-y']/ul/li[1]")).sendKeys(Keys.BACK_SPACE);
driver.switchTo().window(main);
driver.quit();
I have to check switching behavior of sites but when i test it on site http://www.baidu.com , which in usual browsers, opens options in a new tab, opens the clicked option in the same tab while using selenium. How to make it execute as it executes normally in browsers?
Following code will help you to open link in new window :
driver.get("http://www.google.com"); //Replace your URL
WebElement link = driver.findElement(By.xpath("Your Element Xpath"]"));
Actions act = new Actions(driver);
act.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// It will close new opened window
driver.close();
You can use id or other locator for element as per your flexibility.
We are just pressing keys Shift + Up by selenium code.