我正在尝试编写一些ruby来执行以下操作:
我正在测试一个网页,它有一个表单,一个搜索按钮,以及下面的搜索结果。
当我在表单中输入发票编号(例如123456 )并单击“搜索”按钮时,如果发票已成功保存,则它将显示在“搜索结果”部分下,如果没有,则会显示“未找到结果”。有时数据库需要几秒钟才能完成对发票的处理,所以我想做一个循环,每隔几秒钟按一次搜索按钮,直到找到发票,一旦满足条件,就做一些其他的事情。
发布于 2019-10-10 04:13:12
您可以设置一个while循环,一旦找到满足特定条件的WebElement,该循环就会中断。
# Keep track of whether or not invoice has been found
found = false
# First, attempt to locate the desired invoice
begin
# click the search button
driver.find_element(:name, "search_button").click
# check if invoice exists, set found to true if it does
expect(driver.find_element(some_locator_here).displayed?).to eql true
found = true
rescue Selenium::WebDriver::Error::NoSuchElementError
# catch NoSuchElementError if invoice does not exist, leave found as false
puts("Element not found")
# if invoice is not found, continue trying to find it in a loop
while found == false do
begin
# click the search button
driver.find_element(:name, "search_button").click
# attempt to locate the invoice
expect(driver.find_element(some_locator_here).displayed?).to eql true
found = true
rescue Selenium::WebDriver::Error::NoSuchElementError
puts("Element not found")这将单击search按钮并尝试在循环中定位所需的发票,如果所需的发票不存在,则捕获NoSuchElementError。您需要用:name, "invoice_name"之类的东西替换some_locator_here,这样您就可以找到您的发票,并确定是否已经找到它。希望这能对你有所帮助。
https://stackoverflow.com/questions/58310841
复制相似问题