holyonline
holyonline

Reputation: 60

Text Exception Click (Ruby/Selenium Webdriver)

I'm trying to use each do to find texts now. When the object <td align="left">GOLD</td> don't show, Ruby will try to click on <td align="left">STANDARD</td> and he continue to find one after another until he succeed to find and click . On my last question, Rajagopalan helped me with this code:

["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

So I tried to put text instead of numbers (this is really possible?):

["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

And got a error:

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]

I tried to make more exceptions too, but got error in these code too

begin 
    browser.find_element(:xpath => '//td[.="DIGITAL"]').click
    el = browser.find_element(:xpath => '//td[.="STANDARD"]')
    browser.action.double_click(el).perform     
rescue Selenium::WebDriver::Error::NoSuchElementError
    browser.find_element(:xpath => '//td[.="ELO"]').click
    el20 = browser.find_element(:xpath => '//td[.="ELO"]')
    browser.action.double_click(el20).perform   
rescue Selenium::WebDriver::Error::NoSuchElementError
    browser.find_element(:xpath => '//td[.="STANDARD"]').click
    el21 = browser.find_element(:xpath => '//td[.="STANDARD"]')
    browser.action.double_click(el21).perform       
rescue Selenium::WebDriver::Error::NoSuchElementError
    browser.find_element(:xpath => '//td[.="GOLD"]').click
    el22 = browser.find_element(:xpath => '//td[.="GOLD"]')
    browser.action.double_click(el22).perform
end

In this case, he stops on second element ("ELO") and don't reach the third ("STANDARD") and forth ("GOLD"). (Trying to pass through ELO and DIGITAL to reach STANDARD, which is present in my test.)

*** WARNING: You must use ANSICON 1.31 or higher (https://github.com/adoxa/ansicon/) to get coloured output on Windows
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":"xpath","selector":"//td[.="ELO"]"} (Selenium::WebDriver::Error::NoSuchElementError)

But got this error too. Any hints about what I can do?

Upvotes: 1

Views: 113

Answers (1)

Rajagopalan
Rajagopalan

Reputation: 6064

You are extrapolating the string in a wrong way

a=23
Correct one: "something'#{a}'is correct"
wrong one  : 'something"#{a}"is correct'

So use this one

["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

You better use normalize-space(.) which will strip the leading and trailing space

["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

Upvotes: 1

Related Questions