Webdriver上传文件并使用

Webdriver上传文件并使用

本文介绍了使用Selenium Webdriver上传文件并使用<按钮类型=“按钮”>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Selenium WebDriver和Ruby,我需要自动化文件上传功能。
我试过了

WAIT.until {driver.find_element(:css,'div.ui-dialog-buttonset')。显示? }



通过使用它可以让超时错误

  elem = driver.find_element(:css,'div.ui-dialog-buttonset')
putsHi
js =<< -end
var buttons = document.querySelectorAll('button');
buttons [0] .setAttribute('aria-disabled',true);
end
elem = driver.execute_script(js,elem)
puts elem
elem.send_keys(C:\\ Users \\\\\\\\\\\\' Desktop \\1413.jpg)

以上代码无法正常工作,脚本因错误

这里是HTML代码

 < div class =ui -dialog-buttonpane ui-widget-content ui-helper-clearfix> 
< div class =ui-dialog-buttonset>
< button class =otherButtonClass ui-state-hover ui-state-focustype =buttonrole =buttonaria-disabled =false>
< button class =otherButtonClasstype =buttonrole =buttonaria-disabled =falsestyle =display:none;>
< button class =cancelButtonClasstype =buttonrole =buttonaria-disabled =false>
< / div>
< / div>

这里是错误堆栈
$ b $

 ,message:error  -  undefined me 
thod`send_keys'for nil:NilClass \\\
[\tests / upload_branding.rb:38:in`create_publish_pla $ b $在PublishPlayerTest中,\org / jruby / RubyBasicObjec
t.java:1703:在`__send __'\中检测/ upload_branding.rb:80 ,\c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib
/minitest/unit.rb:1258:in`run'\ ,\c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-rep
orters-0.14.20 / lib / minitest / reporter.rb:62:在`run_with_hooks '\,\c:/jruby-1.7.4/lib/ru
by / gems / shared / gems / scope-0.2.4 / lib / scope.rb:105:在`run' \,\org / jruby / RubyProc.java:255
:in`call'\,\c:/jruby-1.7.4/lib/ruby/gems/shared/gems /scope-0.2.4/lib/scope.rb:153:in`
recursively_run_setup_and_teardown'\,\c:/jruby-1.7.4/lib/ruby/gem S /共享/宝石/范围-0。
2.4 / lib / scope.rb:155:在`recursively_run_setup_and_teardown'\,\c:/jruby-1.7.4/lib/ruby/
gems / shared / gems / scope- 0.2.4 / lib / scope.rb:139:在`run_setup_and_teardown'\,\c:/ jruby-1。
7.4 / lib / ruby​​ / gems / shared / gems / scope-0.2.4 / lib / scope.rb:105:'run'\,\c:/jruby-1.7.4/ li
b / ruby​​ / gems / shared / gems / minitest-4.7.4 / lib / minitest / unit.rb:933:在`_run_suite'\,\org / j
ruby​​ / RubyArray.java:2417:in`map'\,\c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4
.7.4 / lib / minitest / unit.rb:926:in _run_suite'\,\c:/jruby-1.7.4/lib/ruby/gems/shared/gem
s / minitest-reporter-0.14.20 / lib / minitest / reporter_runner.rb:45:`_run_suite'\,\c:/ jr
uby-1.7.4 / lib / ruby​​ / gems / shared / gems / minitest-4.7.4 / lib /minitest/parallel_each.rb:71:in`_r
un_suites'\,\org / jruby / RubyArray.java:2417:in`map'\,\c:/ jruby -1.7.4 / lib / ruby​​ / gems / s
hared / gems / minitest-4.7.4 / lib / minitest / parallel_each.rb:70:在`_run_suites'\中,\c:/ jruby-
1.7.4 / lib / ruby​​ / gems / shared / gems / minitest-reporter-0.14.20 / lib / minitest / reporter_runner.rb
:37:在`_run_suites'\中,\c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/mini
test / unit。 rb:877:在`_run_anything'\中,\c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest
-4.7.4 / lib / minitest / unit。 rb:1085:在`run_tests'\中,\c:/jruby-1.7.4/lib/ruby/gems/shared/g
ems / minitest-reporter-0.14.20 / lib / minitest /reporter_runner.rb:30:in`run_tests'\,\org /
jruby / RubyBasicObject.java:1703:in`__send __'\,\org / jruby / RubyKernel.java :2209:在`send
'\中,\c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/minitest/unit.rb :1072:I
n`_run'\,\org / jruby / RubyArray.java:1617:in`each'\,\c:/jruby-1.7.4/lib/ ruby / gems / sh
ared / gems / minitest-4.7.4 / lib / minitest / unit.rb:1071:在`_run'\中,\c:/jruby-1.7.4/lib / ruby​​ /
gems / shared / gems / minitest-4.7.4 / lib / minitest / unit.rb:1059:在`run'\,\tes ts / upload_brand
ing.rb:87:in'(root)'\],result:false}



在IRB中,我发现以下错误:

  irb(main):038:0> Wait.until {$ driver.find_element(:css,'div.ui-dialog-buttonse>按钮')[1] .displayed? } 
Selenium :: WebDriver :: Error :: TimeOutError:80秒后超时(无法找到元素:{method:css selector,selector:div.ui-dialog-buttonse>按钮})}
from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/wait.rb:57 :在`直到'
从(irb):38:在`评估'
从org / jruby / RubyKernel.java:1093:在`eval'
从org / jruby / RubyKernel。 java:1489:在`loop'中
来自org / jruby / RubyKernel.java:1254:在catch中
来自org / jruby / RubyKernel.java 1254:在catch中
从C:\jruby-1.7.4\ / bin / jirb_swing:53:在`(root)'

也用于第二个命令

  irb(main):043:0> elem = $ driver.find_element(:css,'div.ui-dialog-buttonse>按钮')[1] 
Selenium :: WebDriver :: Error :: NoSuchElementError:无法找到元素:{method: css选择器,选择器:div.ui-dialog-buttonse按钮}
from [remote server] file:/// C:/ Users / seema / AppData / Local / Temp / webdriver- profile20130704-2984-1wifg7r/extensions/[email protected]/components/driver_component.js:8444:在[FirefoxDriver.prototype.findElementInternal_'
from [remote server] file:/// C:/ Users / seema /AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/driver_component.js:8453:in [FirefoxDriver.prototype.findElement'
from [remote server] file: ///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/command_processor.js:10456:in`DelayedCommand.prototype.executeInternal_ / h'从[远程服务器]文件
:/// C:/ Users / seema / AppData / Local / Temp / webdriv er-profile20130704-2984-1wifg7r/extensions/[email protected]/components/command_processor.js:10461:在[DelayedCommand.prototype.executeInternal_'
from [remote server] file:/// C:/ Users /seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/command_processor.js:10401:in`DelayedCommand.prototype.execute /<'
from C :/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:51:在`assert_ok'
from C :/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:15:在`初始化'
从C :/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:在`create_response'
中从C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:66:在`request'$ b $ C从C:/jruby-1.7.4/lib/ ruby / gems / shared / gems / selenium-webdriver-2.33.0 / lib / selenium / webdriver / remote / http / common.rb:40:在`call'中
从C:/jruby-1.7.4/ lib / ruby​​ / gems / shared / gems / selenium-webdriver-2.33.0 / lib / selenium / webdriver / remote / bridge.rb:629:在`raw_execute'
从C:/jruby-1.7.4/ lib / ruby​​ / gems / shared / gems / selenium-webdriver-2.33.0 / lib / selenium / webdriver / remote / bridge.rb:607:在`execute'
从C:/jruby-1.7.4/ lib / ruby​​ / gems / shared / gems / selenium-webdriver-2.33.0 / lib / selenium / webdriver / remote / bridge.rb:575:在`find_element_by'
从C:/jruby-1.7.4/ lib / ruby​​ / gems / shared / gems / selenium-webdriver-2.33.0 / lib / selenium / webdriver / common / search_context.rb:42:在`find_element'
from(irb):43:in`evaluate '
from C:\jruby-1.7.4\ / bin / jirb_swing:53:in`(root)'

善意帮助

解决方案

首先检查链接以避免这种超时错误。



您可以尝试下面的代码:

 #隐式超时声明
wait = Selenium :: WebDriver :: Wait.new(:timeout => 60)

#等待显示所需的按钮元素
#使用链接:http://www.w3schools.com/cssref/css_selectors.asp
#for css选择器
wait.until {driver.find_element(:css,'div.ui-dialog-buttonse>按钮')[1] .displayed? }
elem = driver.find_element(:css,'div.ui-dialog-buttonse> button')[1]
elem.sendKeys(C://path/To/File.jpg )

更新

查看链接和您的零件 html 下方,我看到 aria-disabled 设置为 false

 < button class =otherButtonClass ui-state-hover ui-state-focustype =buttonrole =buttonaria-disabled =false> 

所需的按钮元素已禁用。

现在看到以下问题: $ b

更新

  #implicit超时声明
wait = Selenium :: WebDriver :: Wait.new(:timeout => 60)

#等待显示div元素
wait.until {driver.find_element(:css,'div.ui-dialog-buttonse')。显示? }
elem = driver.find_element(:css,'div.ui-dialog-buttonse')

js =<<< -end
var buttons = document.querySelectorAll ( '按钮');
buttons [0] .setAttribute('aria-disabled',true);
end

elem = driver.execute_script(js,elem)
elem.sendKeys(C://path//To//File.jpg)


I am using Selenium WebDriver and Ruby and I need to automate the file upload feature.I tried with

WAIT.until { driver.find_element(:css,'div.ui-dialog-buttonset').displayed? }

by using this it gives time out error

  elem = driver.find_element(:css,'div.ui-dialog-buttonset')
  puts "Hi"
  js = <<-end
var buttons = document.querySelectorAll('button');
    buttons[0].setAttribute('aria-disabled', true);
end
  elem = driver.execute_script(js, elem)
  puts elem
  elem.send_keys("C:\\Users\\seema\\Desktop\\1413.jpg")

Above code is not working and script fails by following error

here is HTML code

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
   <div class="ui-dialog-buttonset">
      <button class="otherButtonClass ui-state-hover ui-state-focus" type="button" role="button" aria-disabled="false">
      <button class="otherButtonClass" type="button" role="button" aria-disabled="false" style="display: none;">
      <button class="cancelButtonClass" type="button" role="button" aria-disabled="false">
   </div>
</div>

here is error stack

,"message":"error - undefined me
    thod `send_keys' for nil:NilClass \n [\"tests/upload_branding.rb:38:in `create_publish_pla
    yer'\", \"tests/upload_branding.rb:80:in `PublishPlayerTest'\", \"org/jruby/RubyBasicObjec
    t.java:1703:in `__send__'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib
    /minitest/unit.rb:1258:in `run'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-rep
    orters-0.14.20/lib/minitest/reporters.rb:62:in `run_with_hooks'\", \"c:/jruby-1.7.4/lib/ru
    by/gems/shared/gems/scope-0.2.4/lib/scope.rb:105:in `run'\", \"org/jruby/RubyProc.java:255
    :in `call'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/scope-0.2.4/lib/scope.rb:153:in `
    recursively_run_setup_and_teardown'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/scope-0.
    2.4/lib/scope.rb:155:in `recursively_run_setup_and_teardown'\", \"c:/jruby-1.7.4/lib/ruby/
    gems/shared/gems/scope-0.2.4/lib/scope.rb:139:in `run_setup_and_teardown'\", \"c:/jruby-1.
    7.4/lib/ruby/gems/shared/gems/scope-0.2.4/lib/scope.rb:105:in `run'\", \"c:/jruby-1.7.4/li
    b/ruby/gems/shared/gems/minitest-4.7.4/lib/minitest/unit.rb:933:in `_run_suite'\", \"org/j
    ruby/RubyArray.java:2417:in `map'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4
    .7.4/lib/minitest/unit.rb:926:in `_run_suite'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gem
    s/minitest-reporters-0.14.20/lib/minitest/reporter_runner.rb:45:in `_run_suite'\", \"c:/jr
    uby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/minitest/parallel_each.rb:71:in `_r
    un_suites'\", \"org/jruby/RubyArray.java:2417:in `map'\", \"c:/jruby-1.7.4/lib/ruby/gems/s
    hared/gems/minitest-4.7.4/lib/minitest/parallel_each.rb:70:in `_run_suites'\", \"c:/jruby-
    1.7.4/lib/ruby/gems/shared/gems/minitest-reporters-0.14.20/lib/minitest/reporter_runner.rb
    :37:in `_run_suites'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/mini
    test/unit.rb:877:in `_run_anything'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest
    -4.7.4/lib/minitest/unit.rb:1085:in `run_tests'\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/g
    ems/minitest-reporters-0.14.20/lib/minitest/reporter_runner.rb:30:in `run_tests'\", \"org/
    jruby/RubyBasicObject.java:1703:in `__send__'\", \"org/jruby/RubyKernel.java:2209:in `send
    '\", \"c:/jruby-1.7.4/lib/ruby/gems/shared/gems/minitest-4.7.4/lib/minitest/unit.rb:1072:i
    n `_run'\", \"org/jruby/RubyArray.java:1617:in `each'\", \"c:/jruby-1.7.4/lib/ruby/gems/sh
    ared/gems/minitest-4.7.4/lib/minitest/unit.rb:1071:in `_run'\", \"c:/jruby-1.7.4/lib/ruby/
    gems/shared/gems/minitest-4.7.4/lib/minitest/unit.rb:1059:in `run'\", \"tests/upload_brand
    ing.rb:87:in `(root)'\"]","result":false}

In IRB i found following errors

irb(main):038:0> Wait.until { $driver.find_element(:css,'div.ui-dialog-buttonse>button')[1].displayed? }
Selenium::WebDriver::Error::TimeOutError: timed out after 80 seconds (Unable to locate element: {"method":"css selector","selector":"div.ui-dialog-buttonse>button"})}
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/wait.rb:57:in `until'
    from (irb):38:in `evaluate'
    from org/jruby/RubyKernel.java:1093:in `eval'
    from org/jruby/RubyKernel.java:1489:in `loop'
    from org/jruby/RubyKernel.java:1254:in `catch'
    from org/jruby/RubyKernel.java:1254:in `catch'
    from C:\jruby-1.7.4\/bin/jirb_swing:53:in `(root)'

also for second command

irb(main):043:0> elem = $driver.find_element(:css,'div.ui-dialog-buttonse>button')[1]
Selenium::WebDriver::Error::NoSuchElementError: Unable to locate element: {"method":"css selector","selector":"div.ui-dialog-buttonse>button"}
    from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/driver_component.js:8444:in `FirefoxDriver.prototype.findElementInternal_'
    from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/driver_component.js:8453:in `FirefoxDriver.prototype.findElement'
    from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/command_processor.js:10456:in `DelayedCommand.prototype.executeInternal_/h'
    from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/command_processor.js:10461:in `DelayedCommand.prototype.executeInternal_'
    from [remote server] file:///C:/Users/seema/AppData/Local/Temp/webdriver-profile20130704-2984-1wifg7r/extensions/[email protected]/components/command_processor.js:10401:in `DelayedCommand.prototype.execute/<'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:51:in `assert_ok'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/response.rb:15:in `initialize'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:59:in `create_response'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/default.rb:66:in `request'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:629:in `raw_execute'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:607:in `execute'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/remote/bridge.rb:575:in `find_element_by'
    from C:/jruby-1.7.4/lib/ruby/gems/shared/gems/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/search_context.rb:42:in `find_element'
    from (irb):43:in `evaluate'
    from C:\jruby-1.7.4\/bin/jirb_swing:53:in `(root)'

Kindly help

解决方案

First check out the link Selenium::WebDriver::Wait to avoid such time out error.

You can try the below code :

# implicit time out declarations
wait = Selenium::WebDriver::Wait.new(:timeout => 60)

# wait till the required button element being displayed
# Use the link : http://www.w3schools.com/cssref/css_selectors.asp
# for css selectors
wait.until { driver.find_element(:css,'div.ui-dialog-buttonse>button')[1].displayed? }
elem = driver.find_element(:css,'div.ui-dialog-buttonse>button')[1]
elem.sendKeys("C://path/To/File.jpg")

Update

Looking at the link aria-disabled attribute and your part html below, I am seeing aria-disabled is set as false :

<button class="otherButtonClass ui-state-hover ui-state-focus" type="button" role="button" aria-disabled="false">

That desired button element is disabled.

Now see the question : Does WebDriver support file uploads?

Update

#implicit time out declarations
wait = Selenium::WebDriver::Wait.new(:timeout => 60)

#wait till the div element is being displayed
wait.until { driver.find_element(:css,'div.ui-dialog-buttonse').displayed? }
elem = driver.find_element(:css,'div.ui-dialog-buttonse')

js = <<-end
var buttons = document.querySelectorAll('button');
    buttons[0].setAttribute('aria-disabled', true);
end

elem = driver.execute_script(js, elem)
elem.sendKeys("C://path//To//File.jpg")

这篇关于使用Selenium Webdriver上传文件并使用&lt;按钮类型=“按钮”&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 23:53