0

Unable to do any action in list which is opened in new window after changing control. I have a parent window after clicking an <img> it's opening the list of values, in a list with new window. I have changed the control to new window using below code for (String handle : driver.getWindowHandles()){ driver.switchTo().window(handle); } driver.findElement(By.cssSelector("a[id='dynshlp_sel_2-rowsel']"));

After that I was not able to do anything. I need to click dynshlp_sel_2-rowsel. But the program is not clicking the element also it is not displaying any error unless I close the new window.

It is throwing an error stating unable to locate the element after I close the new window.

I'm new user to this site and I don't know how to add screenshot for this issue.

HTML CODE:

    <td id="dynshlp_sel_2" class="th-clr-nsel th-clr-cel">
        <div style="text-align:center;">
            <a id="dynshlp_sel_2-rowsel" class="th-lk" title="Select table row" href="javascript:void(0);" onfocus="thtmlbSaveKeyboardFocus('dynshlp_sel_2-rowsel');"></a>
        </div>
    </td>
    <td class="th-clr-cel th-clr-pad th-clr-cel-dis th-clr-cel-first-col" style="width:000056%; 

My Code: String mwh=driver.getWindowHandle(); driver.findElement(By.cssSelector("a[id='C20_W57_V58_V59_V60_marketing_struct.classific-btn'] > img:nth-child(1)")).click(); //above click will open a Popup window System.out.println(driver.getWindowHandles().size()); Set s=driver.getWindowHandles();

    //this method will gives you the handles of all opened windows
    Iterator ite=s.iterator();

    while(ite.hasNext())
    {
        String popupHandle=ite.next().toString();
        if(!popupHandle.contains(mwh))
        {
                    driver.switchTo().window(popupHandle);
                    driver.findElement(By.cssSelector("a[id='dynshlp_sel_2-rowsel']"));
                    //**here you can perform operation in pop-up window**
                    //After finished your operation in pop-up just select the main window again

                    driver.switchTo().window(mwh);
        }
    }

2 Answers2

0

From the attached code, I can see that the Onfocus event is triggered.

To handle such things,

  1. Try simulating a Mouse event
  2. Check if the element is inside a "Frame"

Please post the error log which can help in debugging.

Regards,

Praveen
  • 1,387
  • 1
  • 12
  • 22
  • Hi, As you suggested i have used below mouse events and try to click that element in new window.facing same issue and program is not even running after opening the new window list box. WebElement we = driver.findElement(By.cssSelector("a[id='C20_W57_V58_V59_V60_marketing_struct.classific-btn'] > img:nth-child(1)")); for (String handle : driver.getWindowHandles()){ driver.switchTo().window(handle); } Actions action = new Actions(driver); action.moveToElement(we).click().moveToElement(driver.findElement(By.cssSelector("a[id='dynshlp_sel_2rowsel']"))).click().build().perform(); – Saravananjoghee May 29 '14 at 06:28
0

The code you are using to switch the control to popup is wrong.

Refer below thread to know how to switch the control to new window.

How can selenium web driver get to know when the new window has opened and then resume its execution

See this blog post to know more about popup handling.

EDIT-I

String mwh=driver.getWindowHandle();
<<<<<<  Click on Button which leads to open a new window  >>>>>
Set s=driver.getWindowHandles();

//this method will gives you the handles of all opened windows
Iterator ite=s.iterator();

while(ite.hasNext())
{
    String popupHandle=ite.next().toString();
    if(!popupHandle.contains(mwh))
    {
                driver.switchTo().window(popupHandle);

                //**here you can perform operation in pop-up window**
                //After finished your operation in pop-up just select the main window again

                driver.switchTo().window(mwh);
    }
}
Community
  • 1
  • 1
Santoshsarma
  • 5,627
  • 1
  • 24
  • 39
  • Hi Santhosh. Thanks for your reply. But still same issue persists. After clinking a button a Help window is opened with values. after that i was not able to do anything .pls help – Saravananjoghee May 29 '14 at 09:10
  • Edited my answer and added some logic there go through it. – Santoshsarma May 29 '14 at 09:42
  • Hi Santhosh, I have updated my code based on your suggestion,but still i was not able to do anything. I don't know whether control is changed to Popup window or not. After opening the Popup i was not able to do anything. System is not displaying any error also while the popup is in open status. After i close the Popup window manually. I;m getting error as Unable to fine the element. Please Advice – Saravananjoghee May 29 '14 at 10:12
  • after opening the popup just try to print the windowhandles size. i.e., System.out.println(driver.getWindowHandles().size()); and also can you post your code here ?? – Santoshsarma May 29 '14 at 10:20
  • I'am not able to perform any action after opening the New Popup. I have attached my code pls check in question section. – Saravananjoghee May 29 '14 at 10:29
  • Is it opening new window after clicking button? I think there is some problem with that locator ? Do you want to click that anchor tag in posted HTML code? By.id("dynshlp_sel_2-rowsel"); – Santoshsarma May 29 '14 at 10:32
  • Is it opening new window after clicking button? - Yes small popup window with some 5 records. i want to select 2nd record.Do you want to click that anchor tag in posted HTML code? By.id("dynshlp_sel_2-rowsel"); - Yes Santhos you are right. – Saravananjoghee May 29 '14 at 10:35
  • String mwh=driver.getWindowHandle(); driver.findElement(By.id("dynshlp_sel_2-rowsel")).click(); – Santoshsarma May 29 '14 at 10:41
  • Hi, I was not even print inside the while loop. I hope its not going inside. – Saravananjoghee May 29 '14 at 10:44
  • Can you pls share your email id. SO that i can share my code and screenshot with details. Here i was not able to share screenshot since i have less points. – Saravananjoghee May 29 '14 at 10:47
  • Email : santoshsarma.jv@gmail.com – Santoshsarma May 29 '14 at 10:51
  • Hi, I have sent details to mail id. Please check and help me. – Saravananjoghee May 29 '14 at 10:55