如何在Safari(MAC)上的webdriver中上传文件

问题描述 投票:3回答:3

我正在尝试使用webdriver将文件上传到Safari(8.0.8)。任何人都可以确认它是否有可能?我正在搜索这个问题,我无法找到明确的信息。

我的测试环境:我在本地PC上使用Win7运行测试,浏览器启动在Selenium Grid上运行,该运行在MAC机器上(集线器+节点运行在MAC Yosemite 10.10.5)

首先,我尝试在MAC上传直接文件。但它没有用。

Browser.Driver.FindElement(By.Id("inputID")).SendKeys("/Users/administrator/Desktop/file.txt");

接下来,我尝试使用LocalFileDetetor,但它也不起作用:

driver.FileDetector = new LocalFileDetector();
Browser.Driver.FindElement(By.Id("inputID")).SendKeys("c:\\file.txt");

接下来,我尝试使用:WebDriverBackedSelenium:

ISelenium safari = new WebDriverBackedSelenium(webDriver, "http://systemname/");
safari.Start();
safari.AttachFile("xpath=//input[@id='inputID']", "e:\\file2.txt");

但它也不起作用。堆栈跟踪:

Selenium.SeleniumException:抛出WebDriver异常----> OpenQA.Selenium.InvalidElementStateException:元素必须是用户可编辑的才能清除它。 (警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:7毫秒构建信息:版本:'2.47.1',修订版:'411b314',时间:'2015-07-30 03:03:16'系统信息:主机:'mac.domain.company.com',ip:'192.168.136.67',os.name:'Mac OS X',os.arch:'x86_64',os.version:'10 .10.5' ,java.version:'1.8.0_51'驱动程序信息:org.openqa.selenium.safari.SafariDriver Capabilities [{browserName = safari,takesScreenshot = true,javascriptEnabled = true,version = 8.0.8,cssSelectorsEnabled = true,platform = MAC ,secureSsl = true}]会话ID:null

它不起作用,因为它是Safari或网格/ safari /远程主机或文件路径(带/的东西)的问题?

selenium selenium-webdriver safari selenium-grid safaridriver
3个回答

0
投票

您可以使用OSAScript上传。请执行以下操作:

  1. 使用下面的代码activate application "Safari" tell application "Safari" tell document 1 do JavaScript "document.getElementsByTagName('label')[0].click()" delay 2 end tell end tell tell application "System Events" keystroke "G" using {command down, shift down} delay 2 keystroke "/Users/melamc/Downloads/upload.jpeg" delay 2 keystroke return delay 2 keystroke return delay 2 end tell在mac中创建.scpt文件
  2. 每当您需要时触发此文件(编写代码以编程方式运行此文件

我希望这会对你有所帮助,试着告诉我


0
投票

我已经做了很多研究来完成在mac上的safari浏览器中上传文件,幸运的是我提出了以下解决方案。

以下是匹配给定解决方案的可能先决条件:

  • 编程环境:c#
  • 自动化环境:Selenium WebDriver,Selenium Grid
  • 浏览器:Safari(12)
  • OS:MAC(High Sierra)
  • Hub:Windows机器,Node:Mac机器

在上述上传文件的上下文中,可以实现以下代码行。

DesiredCapabilities dc = new DesiredCapabilities();

dc.SetCapability(CapabilityType.BrowserName, "safari");
dc.SetCapability(CapabilityType.Version, "12");                       

Driver = new RemoteWebDriver(new Uri("http://Node_Ip_Address:Port/wd/hub/"), dc);

下面的代码行是神奇的(这也适用于Chrome和Firefox。)

IAllowsFileDetection AllowsDetection = Driver as IAllowsFileDetection;
if (AllowsDetection != null)
{
    AllowsDetection.FileDetector = new LocalFileDetector();
}

在java中,相当于上面的内容

Driver.setFileDetector(new LocalFileDetector());

参考:https://saucelabs.com/blog/selenium-tips-uploading-files-in-remote-webdriver

为java编写的代码行尚未经过我的测试,因为我目前正在使用c#。

快乐的测试。

© www.soinside.com 2019 - 2024. All rights reserved.