I'm writing a small piece of code to automate a daily task. As part of this task I login into our internal website and click on a specific tab which prompts a dialog box where i need to click on yes or no to disable. I used an alert statement but it still throws the below error. Also I'm doing this for the first time in my career. Never done this before. Can anyone please help me with this ?
Exception in thread "main" org.openqa.selenium.NoAlertPresentException: No modal dialog is currently open
My code:
package test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class automate {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver","C:\\Users\\405325\\eclipse-workspace\\Monitoring\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.mywebsite.com");
driver.findElement(By.name("userName")).sendKeys("myusername");
driver.findElement(By.name("passWord")).sendKeys("mypassword");
driver.findElement(By.name("loginForm")).click();
driver.findElement(By.xpath("//*[#id=\"tabIndent\"]/div/table/tbody/tr[7]/td[2]/a/font")).click();
Alert alert = driver.switchTo().alert();
driver.findElement(By.xpath("//*[#id=\"dialog20HideableContent\"]/table/tbody/tr[1]/td/form/div/table/tbody/tr/td[2]/div/table[1]/tbody/tr/td")).click();
}
}
As per you question, your code trials and error you are seeing it appears that the dialog box is a Modal Dialog Box and to invoke click() on it you have to induce WebDriverWait as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"dialog20HideableContent\"]/table/tbody/tr[1]/td/form/div/table/tbody/tr/td[2]/div/table[1]/tbody/tr/td"))).click();
Related
Have been trying to access the drop down button element using selenium, the page asks for a pincode on opening and after entering it and clicking ok it refreshes and then when i try to find the element by id or class name or css selector it throws an exception stating " stale element reference: element is not attached to the page document ", It will be helpful to know where im going wrong with the following code, Thanx in advance !
here is my code :
package introduction;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Locators {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "D:/Temp/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
driver.get("https://v5.fipola.in/");
driver.findElement(By.id("DelLocation")).sendKeys("600020");
driver.findElement(By.className("top_pincode_select")).click();
driver.findElement(By.className("customer-name")).click(); //error on this line
}
}
image with the dom
I solved it by introducing thread.sleep(3000) line with interrupted exception on void main, but my question is why does the
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5)); did not work ? does that suppose to make the webdriver wait for 5 seconds until the page gets reloaded after entering the pincode and click ok ?
I am trying to close pop up using selenium web driver with java. I have tried different ways, but unable to succeed. Please help me.
package Demo;
import java.util.Set;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class YahooTest {
public static void main(String[] args) throws InterruptedException {
FirefoxDriver obj = new FirefoxDriver();
String url = "https://www.planyourjourney.com/";
obj.get(url);
obj.manage().window().maximize();
obj.findElement(By.xpath("html/body/div[12]/div/div/div[9]")).click();
obj.findElementByClassName("dyna-link-new-registration").click();
obj.findElement(By.linkText("B2B Login")).click();
Thread.sleep(4000);
obj.findElement(By.id("userid")).clear();
obj.findElement(By.id("userid")).sendKeys("1592jet#gmail.com");
obj.findElement(By.id("ulPassword")).clear();
obj.findElement(By.id("ulPassword")).sendKeys("spyj01");
obj.findElement(By.name("Next")).click();
Thread.sleep(2000);
obj.findElement(By.id("affilitetrainadvpage"));
Alert alert = obj.switchTo().alert();
alert.dismiss();
}
}
I have seen the source code of the web page and could not find any alert in it.
The pop-up is actually written inside a div tag.
Hence, remove the codes related to alert and use below code which uses xpath.
obj.findElement(By.xpath("//*[#id='affilitetrainadvpage']/span/a/img")).click();
I have written a program to click on a link, enter the username and Password and then click on the Signin Button, but i'am not getting the output and its giving me an error.
The Program is as below
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class CssSelector3 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[#id='yui_3_12_0_1_1454585688215_223']/div/ul[1]/li[1]/a")).click();
driver.findElement(By.cssSelector("#login-username")).sendKeys("tester#yahoo.com");
driver.findElement(By.cssSelector("#login-passwd")).sendKeys("tester1234");
driver.findElement(By.cssSelector("#login-signin")).click();
}
}
The program executes only till the maximize window part and then it stops. Can anyone assist me on this.
In your situation xpath of Sign In button is wrong. Yahoo generates everytime new id for its Sign In button. You can try following code.
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.yahoo.com");
driver.manage().window().maximize();
driver.findElement(By.xpath("//*[text()='Sign In']"))
.click();
driver.findElement(By.cssSelector("#login-username"))
.sendKeys("tester#yahoo.com");
driver.findElement(By.cssSelector("#login-passwd")).sendKeys("tester1234");
driver.findElement(By.cssSelector("#login-signin")).click();
}
I am new to WebDriver, i am facing an issue on browser window switching.
I googled for my query resolution and the answer i found best is still not working for me.
Here is my code :
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeSuite;
public class FrameWorkBase {
public static WebDriver driver;
public static WebDriverWait wait;
public static String firstWindow,secondWindow;
#BeforeSuite
public void startDriver() throws Exception{
driver= new FirefoxDriver(); // this firefox window is to open survey
driver.manage().window().maximize();
wait=new WebDriverWait(driver, 40);
driver.get("http://www.cricinfo.com");
firstWindow=driver.getWindowHandle();
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://translate.google.co.in/");
secondWindow=driver.getWindowHandle();
System.out.println("First window handle :" + firstWindow);
System.out.println("\n Second window handle :" + secondWindow);
driver.switchTo().window(firstWindow);
System.out.println("hello");
}
}
I am getting an error on execution as Unable to find window 'xyz' where 'xyz' is the name of first window.
Even i am printing the window name and it is displaying the same window for which it is displaying error.
Please suggest me what i am doing wrong here.
Thanks
This is happening because you have reinitialized the driver instance.
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://translate.google.co.in/");
This line has reinitialised your driver instance so what ever u try to do you won't find the window handle. If you are trying to work on both websites simultaneously, try to create another object of driver like WebDriver driver2 = new FirefoxDriver();
#Vivek has aptly answered your question. But, if you still want to open a link in a new window, you can try the below code for that:
Actions act = new Actions();
WebElement link = driver.findElement(By.xpath("//xpath of the link"));
//Opening the link in new window (works in FF and Chrome)
act.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
And you can switch in between them accordingly, with the use of handles. Furthermore, this link will help you handle two windows simultaneously.
I switch from window A to window B.When i try to perform an action on window B,it throws No such element exception.I am new to selenium webdriver.Please help me out.
My requirement :
1)Go to http://www.kotak.com/bank/personal-banking/convenience-banking/net-banking.html
2)Click on SECURELY LOGIN
3)Switch to the newly opened window and fill username and password in it.Locating username and password on this window throwing error.
My code :
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WindowHandler1 {
public static void main(String args[]) throws InterruptedException
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.kotak.com/bank/personal-banking/convenience- banking/net-banking.html");
Thread.sleep(5000);
driver.findElement(By.xpath(".//*[#id='label-01']/a[1]")).click();
Set<String> windowids = driver.getWindowHandles();
Iterator<String> iter = windowids.iterator();
System.out.println(windowids);
String mainWindowId = iter.next();
String tabedWindowId = iter.next();
Thread.sleep(2000L);
// switching to the new pop up window
driver.switchTo().window(tabedWindowId);
Thread.sleep(20000);
//getting no such element exception upon executing line below
driver.findElement(By.xpath(".//*[#id='Username']")).sendKeys("username");
driver.findElement(By.id("Username")).sendKeys("abc");
}
}
I had a similar problem and noticed that in the list of window handles from selenium, the order is not always the same. So in your code it looks like you are dependent on the last window in the list being the new window, when it may be the first. The solution was to make sure that the window you are trying to switch to is not the same as the current window handle.
You may be getting the NoSuchElement exception because you are not in the right window.