Selenium upload file failed, workaround one

Source: Internet
Author: User

Yesterday, a program encountered a problem, the UI above the need to upload files. But I don't know how to get selenium done.
Click the Upload File button, select the path and file in the pop-up File Selection window, click OK.
To know that the pop-up window belongs to window, selenium can only handle the Web page.
Jeremy the original code was used in selenium RC.
The type "Filepath+filename" method. I don't know what to do. It's not going to work here anyway.
Just take this opportunity to study what to do in Webdriver.
I wrote a Web page, and there was a sentence.
I first recorded with the Selenium IDE, the result is: The operation of the selected file into a sentence type "Path+name".
The type in RC Webdriver should be Webelement.sendkeys, so running a bit really works.
Do not worry about how to deal with the pop-up window, is simply skipping this step, more worry.
It seems that I thought too much before.

-----------------------------------------------------------------------------------------
Last week I tested a new version of the product, upload the file this piece of UI code changed a bit.
With SendKeys This trick unexpectedly, throws an exception:
Org.openqa.selenium.ElementNotVisibleException:Element is isn't currently visible and so could be interactedwith
That is, this input is still in, is not visible, so can not sendkeys.
The only way to do this is to use JavaScript to make this input visible!
Put this sentence Document.findelementbyid (' 123 '). style.visibility= ' visible ', put in the selenium, but the upload file box still does not come out. My JavaScript and CSS are compared to the menu, for the display, hide page elements will only this trick. Just helpless, found in firebug HTML below, input this line is gray. This should indicate that it is hidden, but where do you think it works?

I have selected the gray line, the right style shows the CSS style Display:none
Baidu a bit display all have what value, have none, Inline,block ..., I changed the none to block, the page appeared big and ugly

So: If you want to do Selenium Webdriver 2.0 automation when encountered in the interface has "upload files" demand, inputtype= ' file ' added style is "beautify" no, direct sendkey not, before SendKeys, Use JavaScript to get it out:
javascriptexecutorj= (javascriptexecutor) driver;
J.executescript ("Document.findelementbyid (' 123 '). style.display= ' block ';");
And then Webelement.sendkeys ("C:\abc.txt");

It's OK! This is not very elegant, but there is no way to approach. Must be automated code can not be stuck here. File upload does not go up, after a series of verification work can not be done. In the same vein, there is a two-level menu in the drop-down menu, sometimes a flash is not, you can change the Visiblility:hidden to visible, and set the left and top values, let it show. can go on.

Attached to the page I did to solve this problem (to save time for logging in, I made a separate webpage to simulate the problem)
and observed the situation in the Firebug.


Source: http://blog.sina.com.cn/s/blog_539a70d30101ajsg.html

Selenium upload file failed, workaround one

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.