selenium上传文件java ChromeDriver / FirefoxDriver

时间:2014-08-07 18:06:16

标签: java selenium selenium-chromedriver

我试图自动化上传图片的测试。我可以在“标题”中添加文字。框,但是当我输入文件的位置时,我可以成功输入文本,但网站告诉我,我没有上传有效的.jpg文件。当我手动进行测试时,我无法输入文本,但我可以浏览该文件并成功上传。这是我的代码:

 driver.findElement(By.xpath("//input[@id='FakeInputName2']")).sendKeys("C:\\Users\\MyUsername\\Pictures\\Lighthouse.jpg");

这是HTML代码的样子:

<tr>
<td align="right"> Image: </td>
<td>
    <div id="RenamedUpload1" class="RenameUpload RenameUpload RenameUpload_Office2007">
        <ul id="RenameUpload1ListContainer" class="renamedInputs">
            <li id="RenameUpload1row0">
                <span class="blerghFileWrap blerghStyled">
                    <input id="FakeInputName" class="renamedThisFakeInput somethingNoDecorate" type="text" tabindex="-1" size="67">
                    <label for="FakeInputName" style="display:none">label</label>
                    <input class="notButton notBrowse" type="button" tabindex="-1" value="Select" style="border: 0px dotted;">
                    <input id="FakeInputName2" class="notFileInput" type="file" tabindex="0" name="FakeInputName2" size="68">
                    <label for="FakeInputName2" style="display:none">label</label>
                </span>
            </li>
        </ul>
    </div>
</td>

我还尝试了Runtime.getRuntime()的事情,其他答案也提出了类似的问题。那根本不起作用。有人可以帮忙吗?

这是控制台输出和堆栈跟踪(澄清(MyClass第114行是行String filePath = url.getFile();所在的位置):

Creating Chrome webdriver... Starting ChromeDriver (v2.8.241075) on port 14489
Process took 2 seconds.
java.lang.NullPointerException
at builderselenium.MyClass.runUseCase(MyClass.java:114)
at builderselenium.UseCase.run(UseCase.java:104)
at builderselenium.TestDriver.runUseCase(TestDriver.java:165)
at builderselenium.MyGUI.runUseCase(MyGUI.java:410)
at builderselenium.MyGUI.actionPerformed(MyGUI.java:299)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6527)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6292)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4883)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4705)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:0)

A&#34; * .jpeg&#34; -File不是有效的.jpg文件。

你能否至少显示fileUpload的html-Code?

您确定JVM可以访问该文件吗?也许你应该将文件添加到resources-folder中。如果您的文件存储在&#34; [...] \ workspace \ projcetname \ src \ test \ resources&#34;这应该有效:

String pathToUpload = "/Lighthouse.jpg";
URL url = MYCLASSNAME.class.getResource(pathToUpload);
String filePath = url.getFile();
System.out.print(filePath);
File imgToUpload = new File(filePath);
String absolutePath = imgToUpload.getAbsolutePath();
System.out.print(absolutePath);
driver.findElement(By.xpath("//input[@id='FakeInputName2']")).sendKeys(absolutePath);
System.out.print(driver.getPageSource());

小心:&#39; Myclass&#39;必须替换为您当前的类名。