我无法使用selenium中的sendkey上传文件。甚至我都试着在不同的网站上上传文件。就连我都改变了浏览器。早些时候,我使用Chrome,现在我也试用了Mozilla。我要破例了。让我分享下面的脚本
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)发布于 2021-08-11 16:38:05
如果网页包含属性为input且值为type的任何send_keys标记,则可以直接执行send_keys操作。
所以,你应该把
driver.findElement(By.xpath("//input[@id='fileupload']")).sendKeys("E:\\1.My Task\\My Task123");使用
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("E:\\1.My Task\\My Task123");另外,我建议您在执行此操作之前进行显式等待。
更新1 :
Selenium客户端可以使用显式等待,以获取命令式的过程语言。它们允许您的代码停止程序执行,或冻结线程,直到您传递的条件得到解决为止。条件以特定频率被调用,直到等待超时。这意味着只要条件返回一个falsy值,它就会继续尝试和等待。 由于显式等待允许您等待条件的发生,因此它们非常适合于同步浏览器之间的状态。 以及它的DOM和您的WebDriver脚本。
代码:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@type='file']"))).sendKeys("E:\\1.My Task\\My Task123");你可以在这里参考,看看官员说什么,单击此处
发布于 2021-08-11 18:23:16
该错误清楚地表明,您使用了要上载的文件的错误路径。
E:\\1.My Task\\My Task123不是有效的文件路径。
而且,这个路径必须包括文件本身。
它不能只是文件夹的路径。
应该是这样的:
'C:\Users\yourName\picture.png'还可以将文件发送到的元素通常由
//input[@type='file']XPath,所以您的命令应该如下所示:
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("C:\Users\yourName\picture.png");https://stackoverflow.com/questions/68745817
复制相似问题