Problem with switching focus on a file upload window using webdriver

3,399 views
Skip to first unread message

Rivlin Pereira

unread,
Oct 22, 2011, 9:17:06 PM10/22/11
to webd...@googlegroups.com
Hi All,

I am facing issues with switching focus on a file upload window as a result of which any operations after that fail. Here is what i am doing -

Step 1) -- > driver.findElement(By.cssSelector("#edit-field-local-audio-und-1 > a.button.launcher")).click(); -- This opens up a file upload window.

Step 2) --> driver.findElement(By.xpath("//a[contains(@href,'#media-tab-library')]/descendant::span")).click(); -- I click on a second tab on that window this steps fails because the focus is not on that window.

I have tried using -

                String handle = driver.getWindowHandle();
                driver.switchTo().window(handle);

but still step 2 fails for me. Any inputs would be helpful.

Thanks,


Rivlin Pereira

unread,
Oct 23, 2011, 3:52:06 PM10/23/11
to webd...@googlegroups.com
Hi All,

I issue is similar to this issue here - https://groups.google.com/forum/#!searchin/webdriver/webdriver$20how$20to$20switch$20focus/webdriver/zp0Kwu6JHCc/hrdnZGLGtAEJ

i have tried the suggestion which luke has mentioned but still its not working for me.

My OS is OS X Lion and I am using FF5

Rivlin Pereira

unread,
Oct 23, 2011, 6:34:38 PM10/23/11
to webd...@googlegroups.com
The following is my code -

final Set<String> handles = driver.getWindowHandles();
System.out.println(handles);
driver.findElement(By.linkText("Select media")).click(); -- This opens up a file upload window. The parent window is greyout and only pop-up window is visible



My script then clicks on the library tab on the pop-up window does some file upload and submits it after that control returns back to the parent window.

((JavascriptExecutor)driver).executeScript("window.showModalDialog = function( sURL,vArguments, sFeatures) { window.open(sURL, 'modal', sFeatures); }"); 
               
String newWindow = wait.until(new ExpectedCondition<String>() {
public String apply(WebDriver input) {
Set<String> newHandles = input.getWindowHandles();
System.out.println(newHandles);
newHandles.removeAll(handles);
if (newHandles.size() > 0) {
return newHandles.iterator().next();
}
return null;
}
});

driver.switchTo().window(newWindow);


With this code I still cannot focus on the file upload window. Any help on this would be greatly appreciated.

Thanks

Luke Inman-Semerau

unread,
Oct 23, 2011, 7:46:46 PM10/23/11
to webd...@googlegroups.com
File uploads are different, can you try using .sendKeys("/absolute/path/to/file")

Instead of clicking on it?

If that doesn't work can you send a link or sample HTML of your page so we can help troubleshoot. 

-Luke
--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/k6vB4GRmWFkJ.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Rivlin Pereira

unread,
Oct 23, 2011, 8:55:52 PM10/23/11
to webd...@googlegroups.com
Hi Luke,

Thank you for your reply. I tried using .sendkeys but the command  fails because it cannot find that xpath i am using to find the element. I guess its still not switching the focus to that window. I have attached a doc of steps and the html.

Also I noticed that when the file upload window opens up it is basically an anchored frame. The parent window is greyed out and this frame comes into focus. I cannot switch focus on that frame.

Thanks,
Rivlin
Steps.docx

Bill Ross

unread,
Oct 23, 2011, 10:31:07 PM10/23/11
to webd...@googlegroups.com
> With this code I still cannot focus on the file upload window. Any help on
> this would be greatly appreciated.

What OS? With Windows at least, I believe the file upload screen
is under control of the OS, so is not visible to webdriver.

Bill

Rivlin

unread,
Oct 24, 2011, 12:39:18 AM10/24/11
to webd...@googlegroups.com
Hi Bill,

I am on a Mac with OS X lion. I am using FF5 and using selenium-server standalone 2.9. When I click on the Select Media Button from the parent window, the parent window is greyed out and a file upload window opens which is actually a anchored frame. My problem is actually switching focus on that frame. I have tried digging through various posts here on the forum but still no luck.

Thanks,
Rivlin

Sent from my iPhone

> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.

Mark Collin

unread,
Oct 24, 2011, 5:57:43 AM10/24/11
to webd...@googlegroups.com

The problem is that a file upload window is generally an OS level dialogue box, not a JavaScript based in browser one.  Selenium cannot interact with these hence why you need to use sendKeys on the input element.  This will trigger the workaround behaviour to deal with file uploads.

 

You’ll need to provide a link to the page or all the HTML for us to try and help you any further.

--

You received this message because you are subscribed to the Google Groups "webdriver" group.

To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/_Yn1Xn8SQuEJ.


To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


-- This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error please notify postm...@ardescosolutions.com

Luke Inman-Semerau

unread,
Oct 24, 2011, 9:03:53 AM10/24/11
to webd...@googlegroups.com
Rivlin, Sounds like you need to switchTo().frame() <- pass in the frame element after locating it, this is only a guess, you may or may not actually have a frame (I'm guessing so, since you said 'anchored frame'). As Mark said you'll need to provide some more complete HTML and the code you are currently using to perform your automation, for us to help you further.

Rivlin Pereira

unread,
Oct 24, 2011, 12:04:37 PM10/24/11
to webd...@googlegroups.com
Hi Luke/Mark,

I was thinking that when i click on the select media button from the parent window i was not using waitforelement present just in case webdriver clicks on the button and it cannot instantly find the element on the frame. So I used wait for element present on the frame. That is working fine but it still cannot focus on the frame. I have detailed steps in a word attachment with HTML. My code is -

//final Set<String> handles = driver.getWindowHandles();
driver.findElement(By.linkText("Select media")).click(); -- This clicks Select Media button on the parent window.
for (int second = 0;; second++) {
            if (second >= 1) fail("timeout");
            try { if (isElementPresent(By.xpath("//input[contains(@id,'edit-submit')]"))) break; } catch (Exception e) {} -- waits and checks if the submit button is present on the frame.
            Thread.sleep(1000);
                       
        }

//String newWindow = wait.until(new ExpectedCondition<String>() {
//public String apply(WebDriver input) {
//Set<String> newHandles = input.getWindowHandles();
//System.out.println(newHandles);
//newHandles.removeAll(handles);
//if (newHandles.size() > 0) {
//    //System.out.println(newHandles);
//return newHandles.iterator().next();
//}
//return null;
//}
//});
//
//driver.switchTo().window(newWindow);

        driver.findElement(By.xpath("//input[contains(@name,'files[upload]')]")).sendKeys("/Users"); -- This works fine when i use the selenium IDE, doesnt work when i run the test with webdriver
        driver.findElement(By.xpath("//a[contains(@href,'#media-tab-library')]")).click();

        driver.findElement(By.xpath("//a[contains(@href,'#media-tab-library')]/descendant::span")).click();
                driver.findElement(By.xpath("//html/body/div[4]/div/div/div/div/div/ul/li[2]")).click();
                driver.findElement(By.xpath("//div[contains(@class,'label-wrapper')]/descendant::label")).click();
                driver.findElement(By.xpath("//a[contains(@class,'button fake-ok')]")).click();
Steps.docx
Message has been deleted
Message has been deleted

Rivlin Pereira

unread,
Oct 24, 2011, 11:50:57 PM10/24/11
to webd...@googlegroups.com
Here is the snapshot of the HTML. So when I click Select Media button and then the library tab the frame just opens up inside a div and the control returns back too the parent window when i close it.


2011-10-24_13-27-44.jpg

Mark Collin

unread,
Oct 25, 2011, 1:35:45 AM10/25/11
to webd...@googlegroups.com

What happens if you use:

 

driver.findElement(By.xpath(“//input[@id=’edit-upload’]”)).sendKeys(“c:\myfile.txt”)

 

In the test you have below it looks like to are passing a path to the file upload, and not a file.  I’m also wondering if the square brackets may be causing issues.

--

You received this message because you are subscribed to the Google Groups "webdriver" group.

To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/bnj6kvsPxioJ.


To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Rivlin

unread,
Oct 25, 2011, 6:54:49 AM10/25/11
to webd...@googlegroups.com
Hi Mark,

I have tried that, it cannot find that xpath.

Thanks,
Rivlin

Sent from my iPhone

Mark Collin

unread,
Oct 25, 2011, 7:19:08 AM10/25/11
to webd...@googlegroups.com

Is this HTML inside a frame/iFrame that you haven’t switched to?

Rivlin

unread,
Oct 25, 2011, 7:29:41 AM10/25/11
to webd...@googlegroups.com
Its HTML inside a frame that I haven't switched to. 

Sent from my iPhone

Mark Collin

unread,
Oct 25, 2011, 8:49:11 AM10/25/11
to webd...@googlegroups.com

In that case you need to switch to the frame first J

Rivlin Pereira

unread,
Oct 25, 2011, 9:19:38 AM10/25/11
to webd...@googlegroups.com
I have tried doing that mark but its not working. The frame actually opens within a div at a different url. I will try-
driver.switchTo().Frame("giving the complete url")

Luke Inman-Semerau

unread,
Oct 25, 2011, 12:21:12 PM10/25/11
to webd...@googlegroups.com
Rivlin, depending on how the main page is including the frame (I'm going to assume it's an iframe and the only iframe on the page).

You could / should do this:

driver.switchTo().frame(driver.findElement(By.tagName("iframe"));

Rivlin Pereira

unread,
Oct 25, 2011, 2:44:48 PM10/25/11
to webd...@googlegroups.com
Mark,

Here is an similar example of what i am doing the only difference is instead of clicking on the link i am clicking on a button.

http://livedemo.exadel.com/richfaces-demo/richfaces/modalPanel.jsf?c=modalPanel#

Rivlin Pereira

unread,
Oct 25, 2011, 2:45:44 PM10/25/11
to webd...@googlegroups.com
Hi Luke,

I will give that  a shot.

Simon Stewart

unread,
Oct 25, 2011, 4:18:44 PM10/25/11
to webd...@googlegroups.com
driver.findElement(By.id("edit-upload")).sendKeys("path/to/file");

Simon

On Mon, Oct 24, 2011 at 8:40 PM, Rivlin Pereira
<rivlin....@gmail.com> wrote:
> Hi Mark/ Luke,
>
> In the HTML snapshot I have posted If i click on the Select Media button and
> then hover over the Library tab it link is
>
> http://xxx.xxx.xxxxx.xx/media/browser?render=media-popup&amp;types%5Baudio%5D=audio&amp;activePlugins=&amp;schemes%5Bnpraudio%5D=npraudio&amp;schemes%5Bnprshared%5D=0&amp;schemes%5Bcsscache%5D=0&amp;schemes%5Bpublic%5D=0&amp;plugins=undefined#media-add-upload;
>
> so to open the frame it just opens up another div and appends the url from
> the form tag which can be seen from this html -
>
> <div class="content">
> <div id="media-browser-tabset"><ul><li><a
> href='#media-tab-upload'><span>Upload</span></a></li><li><a
> href='#media-tab-library'><span>Library</span></a></li></ul> <div
> class="media-browser-tab" id="media-tab-upload">
> <form enctype="multipart/form-data"
> action="/media/browser?render=media-popup&amp;plugins=undefined"
> method="post" id="media-add-upload" accept-charset="UTF-8"><div><div
> class="form-item form-type-file form-item-files-upload">
> <label for="edit-upload">Upload a new file </label>
>
> <input type="file" id="edit-upload" name="files[upload]" size="60"
> class="form-file" />
> <div class="description">Allowed file types: <strong>mp3</strong>.</div>
> </div>
>
> i tried using driver.get(/media/browser?.....) and the frame opens up in
> that url but then i cannot click on the submit button to return back to the
> parent window.
>
>


> --
> You received this message because you are subscribed to the Google Groups
> "webdriver" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/webdriver/-/A_UtpSsX8ukJ.

Rivlin

unread,
Oct 25, 2011, 4:22:28 PM10/25/11
to webd...@googlegroups.com
Hi Simon,

I will try that

Sent from my iPhone

Rivlin Pereira

unread,
Oct 25, 2011, 8:24:23 PM10/25/11
to webd...@googlegroups.com
Hi All,

Finally I have it working. Big thanks to you all. My issue was i was not passing the right iframe id when i was switching to the frame.

Thanks,
Rivlin
Reply all
Reply to author
Forward
0 new messages