【问题标题】:Selecting local files using watir-webdriver使用 watir-webdriver 选择本地文件
【发布时间】:2015-02-17 17:06:11
【问题描述】:

我正在尝试使用 watir-webdriver 自动执行从 html 页面中选择本地文件的过程

我有以下html

<body>
<form method="post" action="upload" enctype="multipart/form-data">
test file to upload: <input type="file" name="file" size="60" id="test"/>
<input type="submit" value="Upload" name="upload" id="upload" />
</form>
</body>

我正在尝试使用 idtest 单击输入,并设置我希望使用 watir-webdriver 上传的本地文件的路径。

我可以使用下面的点击按钮来调出选择窗口

@browser.goto 'http://www.test.com'
@browser.button(:id => 'test').click

但是,我正在尝试使用以下方法(通过研究,这似乎是正确的方法。但不起作用)

@browser.file_field(:name => 'file').set("C:\\path\\to\\test\\file\\validTest.xml")

导致以下错误

Watir::Exception::UnknownObjectException: unable to locate element, using {:name=>"file",    :tag_name=>"input", :type=>"file"}

尝试

@browser.button(:id => 'test').set("C:\\path\\to\\test\\file\\validTest.xml")

导致以下错误

NoMethodError: undefined method `set' for #<Watir::Button:0x3859920>

有人可以帮忙吗?我很难理解为什么 file_field 选项不起作用。

【问题讨论】:

  • 如果您收到UnknownObjectException,请尝试@browser.file_field(:name =&gt; 'file').exists?。如果false,那么该元素可能不是真正可定位的(例如,在框架中、异步加载等)。而Button 类没有.set 方法,所以这个错误是合法的。

标签: ruby automation watir-webdriver filefield


【解决方案1】:

尝试使用以下函数:

@browser.file_field(:id,"upload").set("filepath")

另外,如果您使用的是 IE 浏览器,请确保您使用的是 IEDriverServer_Win32_2.33.0,因为它在此驱动程序上运行良好,而不是在最新版本上。

【讨论】:

    【解决方案2】:

    试试这个:

    @browser.file_field(:id => 'test').set("C:\\path\\to\\test\\file\\validTest.xml")
    

    【讨论】:

    • 返回Watir::Exception::UnknownObjectException: unable to locate element, using {:id=&gt;"test", :tag_name=&gt;"input", :type=&gt;"file"}
    • @user3927287,好的,试试这个,还有@browser.element(:xpath =&gt; '//input[@type="File"]')
    • 返回NoMethodError: undefined method "set" for #&lt;Watir::HTMLElement:0x38239c8&gt;
    • @user3927287,对不起,这个:@browser.file_field(:xpath =&gt; '//input[@type="File"]')
    • 感谢您的快速回复:@browser.file_field(:xpath =&gt; '//input[@type="File"]') 返回 Watir::Exception::UnknownObjectException: unable to locate element, using {:xpath=&gt;"//input[@type=\"File\"]", :tag_name=&gt;"input", :type=&gt;"file"}@browser.file_field(:xpath =&gt; '//input[@type="file"]') 返回 Watir::Exception::UnknownObjectException: unable to locate element, using {:xpath=&gt;"//input[@type=\"file\"]", :tag_name=&gt;"input", :type=&gt;"file"} 问题出在 HTML 上吗?
    【解决方案3】:

    尝试在最新的 IEDriver 中像这样使用。 将文件路径分配给变量,然后设置它

    filepath = "C:\\path\\to\\test\\file\\validTest.xml"
    @browser.file_field(:id,"upload").set(filepath)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多