Upload image in selenium C# hidden object

久未见 提交于 2019-12-24 13:13:06

问题


I am not able to upload image:
The code is:

<div class="divFile">
<div id="FileUploaddiv" style="height: 70px; cursor: pointer;">
<div style="position: relative; margin: 0px; padding: 0px; width: inherit; height: inherit;" class="mf_upload_m">
<div style="position: absolute; text-align: center; z-index: 1; width: 100%; height: 100%; top: 0px; left: 0px; font-size: 13px;" class="mf_upload_ins">
<div class="FileHeading">Select File</div><i class="icon-file-alt icon-2" style="margin-top: 3px;"></i></div></div></div>

<div id="uploaded" style=""></div>
<span style="display: none;" class="reqFile error span2">Please Select File</span>

<input id="hdnParentID" name="hdnParentID" type="hidden">
<input id="hdnDocID" name="hdnDocID" value="0" type="hidden">
<input id="hdnDocFolderID" name="hdnDocFolderID" value="40520" type="hidden">
<input id="hddnIsEdit" name="hddnIsEdit" value="1" type="hidden">
</div>
<div class="divFileName hide"></div>

Code for upload:

driver.FindElement(By.XPath("//input[@id='mf_file_FileUploaddiv']")).SendKeys("C:\\Users\\Y.P.Singh\\Desktop\\ETS.txt");

The code executes but the file is not getting uploaded.Also, No Error is coming.


回答1:


Just like in real life, after you input some text, you need to submit it.

My guess is that you need to apply either one of the three options below:

  • Add .click() after SendKeys(...)

  • Add .submit() after SendKeys(...)

  • Find the 'Upload File' button and click that element instead

If you provide the URL of the web-page which you are attempting to automate, then I might be able to give you the exact solution.

UPDATE:

OK, I understand your problem now.

First, change SendKeys(...) to Click().

Then, use Actions in order to type the name of the input file and to open it.

The code below is in Java (I am guessing that it should be pretty similar in C#):

import org.openqa.selenium.interactions.Actions;
Actions actions = new Actions(driver);
actions = actions.sendKeys(fileName);
actions = actions.sendKeys(Keys.RETURN);
actions.build().perform();

Then, wait until the file-upload process completes:

while (!driver.FindElement(By.XPath("//div[@id='PRO0']")).equals("100%"))
    Thread.sleep(100);

Finally, click the 'Save' button using any of the following options:

driver.FindElement(By.XPath("//a[@value='SAVE']")).click();
driver.FindElement(By.XPath("//a[@innerHTML='SAVE']")).click();
driver.FindElement(By.XPath("//a[@class='btn btn-primary btnSaveFile']")).click();



回答2:


Thank you for your Help without which I will not be able able to solve this issue. I have solved this using your technique and mixed it with something that I researched on the net.

I have imported System.Windows.Forms; and then used this Code to

IWebElement checkbx = driver.FindElement(By.CssSelector("input#mf_file_FileUploaddiv.file"));
Actions actions = new Actions(driver);
actions = actions.MoveToElement(checkbx);
actions = actions.Click();
actions.Build().Perform();
System.Windows.Forms.SendKeys.SendWait("C:\\Users\\Y.P.Singh\\Desktop\\ETS.txt");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
System.Threading.Thread.Sleep(4000);
driver.SwitchTo().DefaultContent();
System.Threading.Thread.Sleep(10000);
driver.FindElement(By.XPath("//a[@value='SAVE']")).Click();
System.Threading.Thread.Sleep(10000);


来源:https://stackoverflow.com/questions/21361203/upload-image-in-selenium-c-sharp-hidden-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!