继续一个接一个地单击按钮,直到您成功单击一个按钮而没有任何错误(Ruby Selenium / Webdriver)

时间:2019-01-09 12:29:49

标签: ruby selenium-webdriver

我试图进行单击,但前提是该元素是可单击的,但是我遇到了问题,因为该元素可单击是随机的。第一个按钮可以在第一个,第二个,第三个按钮上使用(他可以在第三行上单击,如image所示,并且第四和第五行有时可以显示,因此我需要Ruby尝试单击{ {1}},如果无法单击传递到Cart_ctl00_ctl06_Detail10_ctl04_btnCancelOn,则会一直持续到最后一行

Cart_ctl00_ctl06_Detail10_ctl08_btnCancelOn

第三行的HTML:

...
sleep 5
wait = Selenium::WebDriver::Wait.new(:timeout => 40)
    wait.until do
    browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl04_btnCancelarOn").click
    el3 = browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl04_GECBtnExpandColumn")
    browser.action.double_click(el3).perform    
rescue Selenium::WebDriver::Error::NoSuchElementError
    browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl08_btnCancelarOn").click
    el5 = browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl04_GECBtnExpandColumn")
    browser.action.double_click(el5).perform
rescue Selenium::WebDriver::Error::NoSuchElementError
    browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl10_btnCancelarOn").click
    el6 = browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl04_GECBtnExpandColumn")
    browser.action.double_click(el6).perform
rescue Selenium::WebDriver::Error::NoSuchElementError
    browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl12_btnCancelarOn").click
    el7 = browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl04_GECBtnExpandColumn")
    browser.action.double_click(el7).perform
rescue Selenium::WebDriver::Error::NoSuchElementError
end
browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl06_btnCancelarOn").click
el8 = browser.find_element(:id, "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_GECBtnExpandColumn")
browser.action.double_click(el8).perform        
...

错误

<input type="image" name="ctl00$ContentPlaceHolder1$ucCancBloqRem$gridListCartoes$ctl00$ctl06$Detail10$ctl08$btnCancelarOn" id="ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl08_btnCancelarOn" title="Cancelar Cartão" src="../../../App_Themes/Con/Images/CancelarOn.png" style="border-width:0px;">

1 个答案:

答案 0 :(得分:1)

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

如果您使用的WATIR是Ruby Selenium Binding的包装,则

require 'watir'
browser = Watir::Browser.new
["04", "08", "10", "12"].each do |num|
  begin
    browser.image(id: "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl06_Detail10_ctl#{num}_btnCancelarOn").click
    browser.image(id: "ctl00_ContentPlaceHolder1_ucCancBloqRem_gridListCartoes_ctl00_ctl04_GECBtnExpandColumn").double_click #hope this is image as well 
    break
  rescue
  end
end

如果使用watir,则在查找元素时自动等待;如果不使用watir,则需要设置隐式等待find_element以等待给定时间。