我正在使用 Selenium 版本:“4.12.1”和浏览器版本 112。但在文件上传过程中遇到困难。我正在使用
sendKeys
方法发送 xpath //input[@type='file']
的文件路径。
规格:
我正在使用版本112的google chromedriver和版本4.1的selenium
驱动程序:RemoteWebDriver
代码:
WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
String filePath = "/path/to/file.txt";
fileInput.sendKeys(filePath);
堆栈跟踪:
org.openqa.selenium.UnsupportedCommandException: unknown command: unknown command: session / 9845 d50a442f6c23dc498210a0d253d7 / se / file
Build info: version: '4.12.1', revision: '8e34639b11' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '5.10.215-181.850.amzn2int.x86_64', java.version: '17.0.11'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [9845 d50a442f6c23dc498210a0d253d7, uploadFile {
file = UEsDBBQACAgIADtor1gAAAAAAAAAAAAAAAARAAAAYnVsa1NoaXBUZXN0Lnhsc3icuwNwZdHXJZ502LFtq2PbTse2bTvp2LZt27Zt2x07mUr6+9d8M/+aX03Nq1dPdc/aZ6219z2n9rtXRgIIGAkAHBwcgNWkSgXgvz0QAQAA1AWsrRwMrRy0FVxtDO01qV0sLSYSxn730cIJeRPfoDBRw}]
Capabilities {
acceptInsecureCerts: true,
browserName: chrome,
browserVersion: 112.0 .5615 .49,
chrome: {
chromedriverVersion: 112.0 .5615 .49(bd2a7bcb881c..., userDataDir: /tmp/.org.chromium.Chromium...
},
goog: chromeOptions: {
debuggerAddress: localhost: 46485
},
networkConnectionEnabled: false,
pageLoadStrategy: normal,
platformName: linux,
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: extension: minPinLength: true,
webauthn: extension: prf: true,
webauthn: virtualAuthenticators: true
}
Element: [
[RemoteWebDriver: chrome on linux(9845 d50a442f6c23dc498210a0d253d7)] - > xpath: //div[@role='dialog']//input[@accept='.xlsx']]
Session ID: 9845 d50a442f6c23dc498210a0d253d
at java.base / jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base / jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java: 77)
at java.base / jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java: 45)
at java.base / java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java: 499)
at java.base / java.lang.reflect.Constructor.newInstance(Constructor.java: 480)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(Unknown Source)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(Unknown Source)
at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(Unknown Source)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(Unknown Source)
at org.openqa.selenium.remote.RemoteWebDriver.execute(Unknown Source)
at org.openqa.selenium.remote.RemoteWebElement.execute(Unknown Source)
at org.openqa.selenium.remote.RemoteWebElement.upload(Unknown Source)
at java.base / java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java: 197)
at java.base / java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java: 1625)
at java.base / java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java: 509)
at java.base / java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java: 499)
at java.base / java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java: 921)
at java.base / java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java: 234)
at java.base / java.util.stream.ReferencePipeline.collect(ReferencePipeline.java: 682)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(Unknown Source)
我尝试了什么:
使用Java中的Robot类实现文件路径复制粘贴,可以按照以下步骤操作:
使用 setClipboardData 函数将文件路径复制到剪贴板。 使用Robot类来模拟粘贴剪贴板内容并按Enter键的键盘动作。
公共类 FilePathPaster {
public static void setClipboardData(String filePath) {
StringSelection stringSelection = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
public static void pasteFilePath() throws AWTException {
Robot robot = new Robot();
// Simulate Ctrl+V
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
// Simulate Enter key press
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
public static void main(String[] args) {
try {
// Set the file path to clipboard
String filePath = "C:\\path\\to\\your\\file.txt";
setClipboardData(filePath);
// Simulate the paste action and Enter key press
pasteFilePath();
} catch (AWTException e) {
e.printStackTrace();
}
}
}