0

In selenium webdriver, I am trying to click on a field in new window, but I keep getting error as

 "Timed out after 20 seconds waiting for presence of element located by: By.xpath: .//*[@id='UpdatePanel1']/table/tbody/tr[2]/td/table/tbody/tr[1]/td Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55"

Here's the piece of code I have written. Kindly help

public class AE_Shipment {

public void AE_Shipment1() throws InterruptedException 
{
    MyClass1 M1= new MyClass1();
    WebDriver w1 = M1.w;
    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001']")).click();
    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001-sM-mI001']/table/tbody/tr/td[2]")).click();
    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001-sM-mI001-sM-mI000']/table/tbody/tr/td[2]")).click();
    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001-sM-mI001-sM-mI000-sM-mI000']/table/tbody/tr/td[2]")).click();

    WebDriverWait wait = new WebDriverWait(w1, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='UpdatePanel1']/table/tbody/tr[2]/td/table/tbody/tr[1]/td")));

    w1.findElement(By.id("ButSearch")).click();
}

Update: I have tried switching to the new window using the code below but I get the same error.

String parentHandle = w1.getWindowHandle(); // get the current window handle 
w1.findElement(By.xpath(".//*[@id='ButSearch']")).click(); 
for (String winHandle : w1.getWindowHandles()) 
{ 
    w1.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window) 
}
6
  • The element is in new window? Have you tried to switch window? Have you tried increasing the waiting time? Feb 1, 2016 at 7:54
  • Yes, switch window, waiting time.. everything has been tried out
    – Sharvari
    Feb 1, 2016 at 8:23
  • Your code doesn't account for switching window. Include that and then try for another element and see if you are getting an error or not.May be the xpath you're trying to use is not correct. Feb 1, 2016 at 8:27
  • String parentHandle = w1.getWindowHandle(); // get the current window handle w1.findElement(By.xpath(".//*[@id='ButSearch']")).click(); for (String winHandle : w1.getWindowHandles()) { w1.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window) }
    – Sharvari
    Feb 1, 2016 at 9:08
  • this is what i tried still same error
    – Sharvari
    Feb 1, 2016 at 9:08

3 Answers 3

1

We have to use SwitchTo() functions, to click an element on new window which opens from your original webDriver instance. Following code might work.

public class AE_Shipment {

public void AE_Shipment1() throws InterruptedException 
{
    MyClass1 M1= new MyClass1();
    WebDriver w1 = M1.w;

 // get the current window handle
 String patentWindow = w1.getWindowHandle();

    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001']")).click();
    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001-sM-mI001']/table/tbody/tr/td[2]")).click();
    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001-sM-mI001-sM-mI000']/table/tbody/tr/td[2]")).click();
    w1.findElement(By.xpath(".//*[@id='MainMenu-mI001-sM-mI001-sM-mI000-sM-mI000']/table/tbody/tr/td[2]")).click();

 // switch focus of WebDriver to the next found window handle (that's your newly opened window)
 for (String newWindow : w1.getWindowHandles()) {
  w1.switchTo().window(newWindow); 
 }

    WebDriverWait wait = new WebDriverWait(w1, 20);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='UpdatePanel1']/table/tbody/tr[2]/td/table/tbody/tr[1]/td")));

 // click the element on new window
    w1.findElement(By.id("ButSearch")).click();

 // close newly opened window when done with it. (If required)
 w1.close(); 

 // switch back to the original window
 w1.switchTo().window(patentWindow); 
}
2
  • Tried, but still same error Timed out after 30 seconds waiting for presence of element located by: By.id: ButSearch Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
    – Sharvari
    Feb 2, 2016 at 10:10
  • Can you check in firebug console, if the selectors are right? As I have verified above mentioned code in website and it is working fine. Feb 3, 2016 at 13:42
0

There is something wrong with the way you have mentioned your code.

You've mentioned that your element is in new window-I am assuming the waiting needs to be done once you have switched the window.

So you need to place the wait condition after your window handling code-

String parentHandle = w1.getWindowHandle(); // get the current window handle 
w1.findElement(By.xpath(".//*[@id='ButSearch']")).click(); 
for (String winHandle : w1.getWindowHandles()) 
{ 

 w1.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window) 

    WebDriverWait wait = new WebDriverWait(w1, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='UpdatePanel1']/table/tbody/tr[2]/td/table/tbody/tr[1]/td")));

//do whatever you want to do here
}

This should do away with the wait condition. And also try increasing the wait time to around 50 seconds to see if you still get the time out exception.

Also I would want to check the xpath- if the exception still persists. Try using any other locater (like CSS Selector) or a different element to wait for to see if the wait condition is working correctly.

7
  • there was some problem while posting the comment, the code written was as below, still the error persists..
    – Sharvari
    Feb 2, 2016 at 5:53
  • String parentHandle = w1.getWindowHandle(); // get the current window handle w1.findElement(By.xpath(".//*[@id='ButSearch']")).click(); for (String winHandle : w1.getWindowHandles()) { w1.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window) } WebDriverWait wait = new WebDriverWait(w1, 50); wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[@id='ButSearch']"))); w1.findElement(By.id("ButSearch")).click();
    – Sharvari
    Feb 2, 2016 at 5:53
  • Please specify the exception that you are getting when posting comment. Feb 2, 2016 at 8:26
  • You are searching for the wrong element inside the window handle. Search for the (.//*[@id='UpdatePanel1']/table/tbody/tr[2]/td/table/tbody/tr[1]/td) inside the child window handle Feb 2, 2016 at 8:30
  • Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"txtShipmentNo"} Command duration or timeout: 10 milliseconds For documentation on this error, please visit: seleniumhq.org/exceptions/no_such_element.html
    – Sharvari
    Feb 2, 2016 at 9:31
0

Just you are traversing through all the window handles using a for loop. First print the parent window handle.

System.out.println(w1.getWindowHandle());

Take all window handles use toArray() to convert to Object[] a array.

Object[] a=w1.getWindowHandles().toArray();

Then print the window handles of all the windows opened using for loop.

for(int i=0;i<a.length;i++)
{
    System.out.println(a[i]);
}

Now identify in which order windows are printed. (It will be in such an order, that the latest window opened will be at last index of the array elements).

Just get the web element at last index of the array a to switch to it.

String latestWindow=(String) a[a.length-1];//Typea casting from Object type to String is required
w1.switchTo().window(latestWindow);

Now, you can do whatever the things you would like to do on the new window you switched to.

11
  • tried the code you suggested, still no luck, error remains same 'unable to locate element'
    – Sharvari
    Feb 2, 2016 at 5:55
  • Can I have the link of the webpage you are working on...
    – Murali
    Feb 2, 2016 at 7:55
  • Its the company app, wont be able to share the link
    – Sharvari
    Feb 2, 2016 at 10:10
  • Can I have any snapshots of html source and webpage?
    – Murali
    Feb 2, 2016 at 10:21
  • HTML code for the field I am trying to click: -color: #ffffff;"> <iframe id="DocFrame" width="100%" height="100%" frameborder="0" src="Modules/FrgtFwding/ShipmentJobsList.aspx?Mode=A&_cb_=1454409492590" scrolling="no" name="DocFrame"> <html xmlns="w3.org/1999/xhtml"> <head id="Head1"> <body style="margin: 0px; padding: 0px;"> <link id="winShowDtl_style" href="/Logisys2_39/wdstyles/blue/blue.css" type="text/css" media="all" rel="StyleSheet">
    – Sharvari
    Feb 2, 2016 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.