How to handle window dialogs while uploading docs

71 views
Skip to first unread message

Soujanya A

unread,
Jul 22, 2015, 5:28:29 AM7/22/15
to Selenium Users
Hi ALL,

I am uploading 30books in my library.while uploading multiple books(one bye one) some times what happened know  after adding the file path " open " button cant be 
clicked.

public static String fileUploadPopUp() throws Exception
{
String data =testData.getCellData(currentTest, data_column_name, testRepeat);
 StringSelection stringSelection = new StringSelection(data);
   Toolkit.getDefaultToolkit().getSystemClipboard()
            .setContents(stringSelection, null);
 try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V); 
robot.keyRelease(KeyEvent.VK_V); 
robot.keyRelease(KeyEvent.VK_CONTROL); 
 Thread.sleep(1000);  
robot.keyPress(KeyEvent.VK_ENTER); 
robot.keyRelease(KeyEvent.VK_ENTER); 
                                      }
catch ( AWTException e) {
// TODO Auto-generated catch block
return "Fail"+ e.getMessage();
}
above i have writen code for uploading books .while uploading multiple books(one bye one) some times what happened know  after adding the file path " open " button cant be clicked.My script has been strucked here.please help me how handle this situation.



Regards,
Ramesh.


chhagan mathuriya

unread,
Jul 22, 2015, 1:19:51 PM7/22/15
to Selenium Users
Hi Ramesh,

You can upload files without clicking on upload button and opening popup dialog.

follow below steps:
  • Find  web element with tag input and type='file'  using below xpath

//input[@type='file']

  • Make it visible if it is hidden ( style.visibility='hidden' ) using javascript

    document.getElementById("myImg").style.visibility = "visible"; 

  •  Send keys on above input element with absolute filepath

   driver.findElement(By.xpath("//input[@type='file']")).sendKeys("/absolute/filepath.jpeg");


It will upload file.


Let me know in case of any queries :)

chhagan mathuriya

unread,
Jul 22, 2015, 5:48:29 PM7/22/15
to Selenium Users, soujany...@gmail.com
get complete java code for uploading a file using webdriver with java from here .


On Wednesday, 22 July 2015 14:58:29 UTC+5:30, ramesh wrote:

Kaleem Uddin Mohammed Abdul

unread,
Jul 26, 2015, 1:53:49 AM7/26/15
to Selenium Users, soujany...@gmail.com
Thanks Chhagan! I have learned a new thing today! It worked like a charm.

I was add a phone in my facebook status using automation

 _WebDriver=new FirefoxDriver();
          _WebDriver.navigate().to("http://www.facebook.com");
          _WebDriver.manage().window().maximize();
          _WebDriver.findElement(By.id("email")).sendKeys("userid");
          _WebDriver.findElement(By.id("pass")).sendKeys("pwd");

          WebElement Element = _WebDriver.findElement(By.id("persist_box"));

          if (Element.isSelected()==false)
          {
              Element.click();
          }
          _WebDriver.findElement(By.xpath("//*[@id='loginbutton']/input")).click();
          Thread.sleep(3000);
       _WebDriver.findElement(By.id("js_0")).sendKeys("C:/Users/k_mohammedabdul/Desktop/Kaleem_Photo.jpg");
But changing the attribute is not as simple as you suggested.

We need to use executescript method to change attribute.
JavascriptExecutor JSE=(JavascriptExecutor) _WebDriver;

JSE.executeScript("arguments[0].setAttribute("style:visibility", "visible");", WebElement);

chhagan mathuriya

unread,
Jul 26, 2015, 4:54:18 AM7/26/15
to Selenium Users, soujany...@gmail.com, makale...@gmail.com
Good to hear that it helped you.

Yes, for executing javascript with webdriver we need to use javascript executor. I forgot to mention that.

Here i am posting complete java program to upload a file using webdriver.

hope this will help other readers as well.

public static void main(String[] args) {

		WebDriver driver = new FirefoxDriver();
		driver.get("https://how-old.net/");
		((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1p'; arguments[0].style.opacity = 1",driver.findElement(By.id("uploadBtn")));
		driver.findElement(By.id("uploadBtn")).sendKeys("C:\\img.jpeg");

	}

Cheers,

CH!NN@ K

unread,
Jul 26, 2015, 5:07:19 AM7/26/15
to seleniu...@googlegroups.com

---
Thanks & Regards,
Purushotham Karimbedu,
Drupal Developer and QA Engineer,
Website for Selenium : http://techlearn.in
Phone no: 09000109120.


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/c25d06cf-fe26-428d-94c5-d38b0967b725%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Thomas Melville

unread,
Jul 26, 2015, 11:49:30 AM7/26/15
to Selenium Users
Hi Ramesh,

Selenium can only interact with the browser, once a file window is opened Selenium loses control.
You'll need to interact with the file input element below the button.

Milan Yadav

unread,
Jul 29, 2015, 1:58:07 AM7/29/15
to Selenium Users, clmat...@gmail.com
Hi Chhagan,

Its throwing Element not visible error.
Have you executed your code?

Kaleem Uddin Mohammed Abdul

unread,
Jul 29, 2015, 3:22:13 AM7/29/15
to Selenium Users, soujany...@gmail.com
Use the below piece of code below executing the Send Keys. He already mentioned


WebDriver driver = new FirefoxDriver();
		driver.get("https://how-old.net/");
		((JavascriptExecutor) driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1p'; arguments[0].style.opacity = 1",driver.findElement(By.id("uploadBtn")));
		driver.findElement(By.id("uploadBtn")).sendKeys("C:\\img.jpeg");


On Wednesday, July 22, 2015 at 2:58:29 PM UTC+5:30, ramesh wrote:

Milan Yadav

unread,
Jul 29, 2015, 3:45:43 AM7/29/15
to Selenium Users, soujany...@gmail.com, makale...@gmail.com
That is what I have done.
tried even copy pasting your exact code,still getting element not visible.

Milan Yadav

unread,
Jul 29, 2015, 4:37:48 AM7/29/15
to Selenium Users, soujany...@gmail.com, makale...@gmail.com, milan.k...@gmail.com
Working now x was missing in below.
arguments[0].style.width = '1p'

Can someone suggest why do we need to set all the attributes why can't this be done by only making it(arguments[0].style.visibility = 'visible') visible.

Thanks,
Milan

Mercious

unread,
Jul 29, 2015, 4:58:33 AM7/29/15
to Selenium Users, soujany...@gmail.com, makale...@gmail.com, milan.k...@gmail.com
Because Selenium wants to simulate a real user-case.

An element could have the take visible but a size of 0. A real user would not see this element despite it's visibility being set as "visible" from a technical point of view. The element has no size so a human eye cannot see it.

This is why Selenium considers a lot more factors than only the visibility-tag.

And that is why you have to set those so that Selenium considers the element visible.
Reply all
Reply to author
Forward
0 new messages