首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法通过selenium中的sendkey上传文件

无法通过selenium中的sendkey上传文件
EN

Stack Overflow用户
提问于 2021-08-11 16:32:17
回答 2查看 400关注 0票数 2

我无法使用selenium中的sendkey上传文件。甚至我都试着在不同的网站上上传文件。就连我都改变了浏览器。早些时候,我使用Chrome,现在我也试用了Mozilla。我要破例了。让我分享下面的脚本

代码语言:javascript
复制
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FileUpload {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        

        System.setProperty("webdriver.chrome.driver","c:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        
        driver.get("https://html.com/input-type-file/");
        
    
        
        driver.findElement(By.xpath("//input[@id='fileupload']")).sendKeys("E:\\1.My Task\\My Task123");

    }

}


Error ----------------------

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument: File not found : E:\1.My Task\My Task123
  (Session info: chrome=92.0.4515.131)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'LAPTOP-D3V0TN3J', ip: '192.168.1.6', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_291'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 92.0.4515.131, chrome: {chromedriverVersion: 92.0.4515.43 (8c61b7e2989f2..., userDataDir: C:\Users\gunve\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:54243}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: fe1a2bfd380827509bef42daa3cee01e
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)
    at JavaNewLessons.FileUpload.main(FileUpload.java:25)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-11 16:38:05

如果网页包含属性为input且值为type的任何send_keys标记,则可以直接执行send_keys操作。

所以,你应该把

代码语言:javascript
复制
driver.findElement(By.xpath("//input[@id='fileupload']")).sendKeys("E:\\1.My Task\\My Task123");

使用

代码语言:javascript
复制
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("E:\\1.My Task\\My Task123");

另外,我建议您在执行此操作之前进行显式等待。

更新1 :

Selenium客户端可以使用显式等待,以获取命令式的过程语言。它们允许您的代码停止程序执行,或冻结线程,直到您传递的条件得到解决为止。条件以特定频率被调用,直到等待超时。这意味着只要条件返回一个falsy值,它就会继续尝试和等待。 由于显式等待允许您等待条件的发生,因此它们非常适合于同步浏览器之间的状态。 以及它的DOM和您的WebDriver脚本。

代码:

代码语言:javascript
复制
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='file']"))).sendKeys("E:\\1.My Task\\My Task123");

你可以在这里参考,看看官员说什么,单击此处

票数 1
EN

Stack Overflow用户

发布于 2021-08-11 18:23:16

该错误清楚地表明,您使用了要上载的文件的错误路径。

E:\\1.My Task\\My Task123不是有效的文件路径。

而且,这个路径必须包括文件本身。

它不能只是文件夹的路径。

应该是这样的:

代码语言:javascript
复制
'C:\Users\yourName\picture.png'

还可以将文件发送到的元素通常由

代码语言:javascript
复制
//input[@type='file']

XPath,所以您的命令应该如下所示:

代码语言:javascript
复制
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\Users\yourName\picture.png");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68745817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档