文本异常单击(Ruby/Selenium Webdriver)

文本异常单击(Ruby/Selenium Webdriver),ruby,selenium-webdriver,Ruby,Selenium Webdriver,我现在试着用每个do来查找文本。当对象GOLD未显示时,Ruby将尝试单击STANDARD,并继续一个接一个地查找,直到成功查找并单击为止。关于我的最后一个问题,拉贾戈帕兰帮助我完成了以下代码: ["04", "08", "10", "12"].each do |num| begin browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail

我现在试着用每个do来查找文本。当对象
GOLD
未显示时,Ruby将尝试单击
STANDARD
,并继续一个接一个地查找,直到成功查找并单击为止。关于我的最后一个问题,拉贾戈帕兰帮助我完成了以下代码:

["04", "08", "10", "12"].each do |num|
  begin
    browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl#{num}_btnCancelarOn").click
    el3 = browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl04_GECBtnExpandColumn")
    browser.action.double_click(el3).perform
    break
  rescue
  end
end
所以我试着用文字代替数字(这真的有可能吗?)

并得到一个错误:

C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok': no such element: Unable to locate element: {"method":"id","selector":"ctl00_ContentPlaceHolder1_ucInfoCliente_lblCpf"} (Selenium::WebDriver::Error::NoSuchElementError)
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 6.1.7601 SP1 x86_64)
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `new'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:84:in `create_response'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/default.rb:104:in `request'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/http/common.rb:62:in `call'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/bridge.rb:166:in `execute'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/oss/bridge.rb:584:in `execute'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/remote/oss/bridge.rb:552:in `find_element_by'
    from C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/search_context.rb:60:in `find_element'
    from C:/test/normaliza conta.rb:58:in `<main>'
[Finished in 47.5s with exit code 1]
[shell_cmd: ruby "C:\test\normaliza conta.rb"]
[dir: C:\test]
[path: C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Ruby25-x64\bin]
在这种情况下,他停在第二个元素(“ELO”)上,没有达到第三个元素(“标准”)和第四个元素(“黄金”)。(试图通过ELO和DIGITAL以达到我的测试中的标准。)


但我也犯了这个错误。有关于我能做什么的提示吗?

您以错误的方式推断字符串

a=23
Correct one: "something'#{a}'is correct"
wrong one  : 'something"#{a}"is correct'
所以用这个

["GOLD", "STANDARD", "ELO", "DIGITAL"].each do |var|
  begin
    browser.find_element(:xpath => "//td[.='#{var}']")
    el3 = browser.find_element(:xpath => "//td[.='#{var}']")
    browser.action.double_click(el3).perform
    break
  rescue Selenium::WebDriver::Error::NoSuchElementError
  end
end
您最好使用
规范化空格(.)
,这将去掉前导空格和尾随空格

["GOLD", "STANDARD", "ELO", "DIGITAL"].each do |var|
  begin
    browser.find_element(:xpath => "//td[normalize-space(.)='#{var}']")
    el3 = browser.find_element(:xpath => "//td[normalize-space(.)='#{var}']")
    browser.action.double_click(el3).perform
    break
  rescue Selenium::WebDriver::Error::NoSuchElementError
  end
end

再次感谢拉贾戈帕兰!很好用=D
a=23
Correct one: "something'#{a}'is correct"
wrong one  : 'something"#{a}"is correct'
["GOLD", "STANDARD", "ELO", "DIGITAL"].each do |var|
  begin
    browser.find_element(:xpath => "//td[.='#{var}']")
    el3 = browser.find_element(:xpath => "//td[.='#{var}']")
    browser.action.double_click(el3).perform
    break
  rescue Selenium::WebDriver::Error::NoSuchElementError
  end
end
["GOLD", "STANDARD", "ELO", "DIGITAL"].each do |var|
  begin
    browser.find_element(:xpath => "//td[normalize-space(.)='#{var}']")
    el3 = browser.find_element(:xpath => "//td[normalize-space(.)='#{var}']")
    browser.action.double_click(el3).perform
    break
  rescue Selenium::WebDriver::Error::NoSuchElementError
  end
end