当前位置: 首页 > 知识库问答 >
问题:

如何通过使用Selenium Grid将文件从本地机器传输到远程Web服务器来上传文件

漆雕绍晖
2023-03-14

使用Selenium网格上载文件:

密码

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Main 
{
    
    public static void main(String[] args) throws MalformedURLException
    {
          DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
          capabilities.setBrowserName("internet explorer");
    
          RemoteWebDriver driver = new RemoteWebDriver(new URL("http://URL:4444/wd/hub"), capabilities);
          driver.setFileDetector(new LocalFileDetector());
          driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    
          driver.get("https://url.de/index.xhtml");
          driver.findElement(By.xpath("//*[@id='form:sdsupload']/span")).click();
        
          WebElement addFile = driver.findElement(By.xpath("//input[@type='file']"));
          ((RemoteWebElement) addFile ).setFileDetector(new LocalFileDetector());
          addFile.sendKeys("C:\\daten\\test\\test2.xml");
    }
}

例外

Jun 26, 2020 3:47:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMATION: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Attempting to upload file 'C:\daten\test\test2.xml' which does not exist.
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'xxx', ip: 'xxx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:5494/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, webdriver.remote.sessionid: ae053a22-c088-402e-8de1-f25...}
Session ID: ae053a22-c088-402e-8de1-f25c4398ccbf
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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.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 de.xxx.xxx.keywords.Main.main(Main.java:41)

文件存在于本地节点上,但不存在于远程节点上。如果文件在远程节点上,它的工作原理很好。文件存在于本地节点上,但不存在于远程节点上:https://i.stack.imgur.com/f31Wb.png

我读到:

  • https://www.selenium.dev/documentation/en/remote_webdriver/remote_webdriver_client/
  • https://www.lambdatest.com/blog/how-to-download-upload-files-using-selenium-with-java/

如何将文件传递到远程节点?

编辑

与Firefox、IE和Edge相同的错误Chromium

Firefox:

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: File not found: C:\daten\test\test3.xml
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'xxx', ip: 'xxx', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_171'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 60.5.0, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.25.0, moz:headless: false, moz:processID: 624, moz:profile: C:\Users\username\Ap..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 10.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, webdriver.remote.sessionid: 8801c61e-6d61-488b-8f86-c0a...}
Session ID: 8801c61e-6d61-488b-8f86-c0a1fb2f2df8
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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.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 de.xxx.xxx.keywords.Main.main(Main.java:52)

当我将文件复制到远程客户端时,它再次工作良好:(

调试:使用Firefox进行调试

共有2个答案

潘璞瑜
2023-03-14

感谢您@DebanjanB帮助我找到解决方案-我对您的回答投了赞成票,这是另一个说明它确实有效的答案。

我的问题:

在本地,我正在对本地运行的Selenium服务器(运行selenium-server-4.0.0-beta-4.jar文件)运行Selenium测试,我也在使用chromedriver.exe文件。

我的测试(用TypeScript编写)上载文件时使用:

await (
  await driver.findElement(By.xpath("//form//input[@id='upload-input']"))
).sendKeys(join(__dirname, "testfile.txt"));

本地无问题运行。但在我们的DevOps管道中,使用selenium/standalone chrome图像运行Docker,就像驱动程序找不到文件一样:

InvalidArgumentError: invalid argument: File not found: /home/vsts/work/1/s/selenium/tests/flows/testfile.txt

尽管我们已通过运行以下命令确认文件确实位于正确的文件夹中:

> pwd
/home/vsts/work/1/s
> find . -name testfile.txt
./selenium/tests/flows/testfile.txt

这意味着文件确实位于home/vsts/work/1/s/selenium/tests/flows/testfile。txt。

我和我的团队在google上搜索了这是为什么,并找到了@DebanjanB的答案。

我们没有意识到的是,selenium/standalone chrome Docker映像正在运行一个selenium网格服务器,它是“远程”而不是“本地”的。因此,驱动程序在运行“remote”时无法找到文件。

解决方案

我们补充道

driver.setFileDetector(new FileDetector());

之前

await (
  await driver.findElement(By.xpath("//form//input[@id='upload-input']"))
).sendKeys(join(__dirname, "testfile.txt"));

现在,我们的测试也通过了,当我们在管道中运行docker容器时!

来自Selenium的文档:

本地文件检测器允许将文件从客户端计算机传输到远程服务器。例如,如果测试需要将文件上载到web应用程序,则远程WebDriver可以在运行时自动将文件从本地计算机传输到远程web服务器。这允许从运行测试的远程计算机上载文件。

资料来源:https://www.selenium.dev/documentation/webdriver/remote_webdriver/#local-文件检测器

我希望这能帮助那些与同样问题作斗争的人:-)

宋昕
2023-03-14

此错误消息。。。

Exception in thread "main" org.openqa.selenium.InvalidArgumentException: Attempting to upload file 'C:\daten\test2.xml' which does not exist.

。。。表示客户端计算机上不存在所需的文件。

本地文件检测器允许将文件从客户端计算机传输到远程服务器。如果测试需要将文件上载到web应用程序,远程WebDriver可以在运行时自动将文件从本地计算机传输到远程web服务器。这允许从运行测试的远程计算机上载文件。默认情况下不启用,可以按如下方式启用:

>

  • Java:

    driver.setFileDetector(new LocalFileDetector());
    

    Python:

    from selenium.webdriver.remote.file_detector import LocalFileDetector
    
    driver.file_detector = LocalFileDetector()
    

    C#:

    var allowsDetection = this.driver as IAllowsFileDetection;
    if (allowsDetection != null)
    {
       allowsDetection.FileDetector = new LocalFileDetector();
    }
    

    红宝石:

    @driver.file_detector = lambda do |args|
      # args => ["/path/to/file"]
      str = args.first.to_s
      str if File.exist?(str)
    end
    

    JavaScript:

    var remote = require('selenium-webdriver/remote');
    driver.setFileDetector(new remote.FileDetector); 
    

    静态编程语言:

    driver.fileDetector = LocalFileDetector()
    

    如果您在Selenium Grid上运行测试,则需要让远程驱动程序知道需要上载的文件位于本地计算机上,而不是远程计算机上。在这些情况下,要将文件从客户端计算机上载到远程服务器,WebDriver可以在运行时自动将文件从本地计算机传输到远程web服务器。您可以使用以下代码块:

    WebElement addFile = driver.findElement(By.xpath("//input[@type='file']"));
    ((RemoteWebElement)addFile).setFileDetector(new LocalFileDetector());
    addFile.sendKeys("C:\\daten\\test2.xml");
    

    在Selenium网格上运行测试时选择和上载文件

  •  类似资料:
    • 我需要将文件从远程服务器复制到本地服务器,然后使用JMETER使用SSH SFTP进行尝试。我提供了登录凭据,并在“文件传输”部分中,选择了操作 - PUT源路径 - 远程路径( /ftp / xxx / yyy)目标 - 本地路径。 收到错误为` Java . io . file not found exception:C:\ Apache-jmeter-5 . 1 . 1 \ bin \ FT

    • 问题内容: 我想在我的app.js服务器中上传文件,该文件应将该文件通过管道传送到跨域服务器,例如我的upload.js服务器。 完整代码可在以下链接下找到 upload.js服务器正在运行。我的问题是app.js服务器。请求似乎能够流式传输文件(https://github.com/request/request#streaming)。但是我没有使它工作。我总是在我的app.js中收到[错误:无

    • 问题内容: 我正在开发一个远程备份应用程序,有时我需要上传大文件,例如15 MB,我已经在某些手机上进行了测试,但遇到内存不足错误 使用此功能是否可以使用更少的内存? 这里的错误日志 修改并添加固定无缓冲区后,出现此错误 我猜这差个字节是标题数据吗?如何获取标头的length()? 问题答案: 您应该使用HttpURLConnection 的or 方法。这将防止您的数据在内存中缓冲并耗尽。 文档中

    • 问题内容: 我创建了一个从有权访问的FTP服务器下载文件的功能。如何将文件上传回FTP服务器? 以下是我使用的download_files方法: 问题答案: 使用Apache Commons Net库中的FTPClient类。 这是一个带有示例的代码段: 摘录自http://www.kodejava.org/examples/356.html

    • 名为charles的工具中有一个功能,允许您映射远程请求: http://www.charlesproxy.com/documentation/tools/map-remote/ 基本上,它可以将任何请求发送到服务器(即使不是运行它的人),然后向另一台服务器发出新请求,保留路径和查询字符串。然后,来自第二台服务器的响应将覆盖来自第一台服务器的响应。 我只是想知道是否有一个节点模块可以做到这一点。我

    • 了解如何使用“文件”面板在 Dreamweaver 中管理文件和文件夹、在本地和远程站点之间传输和同步它们。此外,还可了解在 Dreamweaver 中自动恢复功能如何运行。 使用 Dreamweaver 中的“文件”面板,您可以访问和管理与您的站点关联的文件。在“文件”面板中,您可以将视图切换为“FTP 视图”或“Git 视图”,进而使用 FTP 服务器或 Git 存储库来管理文件。 要打开“文