如何选择出现在水豚的新弹出窗口中的按钮?

时间:2016-03-17 06:05:52

标签: ruby capybara

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html style="">
<head>
<body class="hasMotif lookupTab LookupSearchFrame brandNoBgrImg" onfocus="if(this.bodyOnFocus)bodyOnFocus();" onload="if(this.bodyOnLoad)bodyOnLoad();" onbeforeunload="if(this.bodyOnBeforeUnload){var s=bodyOnBeforeUnload();if(s)return s;}" onunload="if(this.bodyOnUnload)bodyOnUnload();">
<form id="theForm" target="resultsFrame" onsubmit="if (window.ffInAlert) { return false; }" name="theForm" method="GET" action="/_ui/common/data/LookupResultsFrame">
<input id="lkfm" type="hidden" value="editPage" name="lkfm">
<input id="lknm" type="hidden" value="Account" name="lknm">
<input id="lktp" type="hidden" value="001" name="lktp">
<div class="lookup">
<div class="bPageTitle">
<div class="pBody">
<label class="assistiveText" for="lksrch">Search</label>
<input id="lksrch" type="text" value="" size="20" placeholder="Search..." name="lksrch" maxlength="80">

###
<input class="btn" type="submit" title="Go!" name="go" value=" Go! ">

###
<input class="btn" type="button" title="New" onclick="javascript:top.resultsFrame.location='LookupResultsFrame?lktp=001&lkfm=editPage&lknm=Account&lksrch=&lkenhmd=SEARCH_NAME&lknew=1&igdp=0'" name="new" value=" New ">
<div class="bDescription">You can use "*" as a wildcard next to other characters to improve your search results.</div>
</div>
</div>
</form>

以上是弹出窗口内容的代码,我必须选择Go按钮。

我的代码如下:

  #Get the main window handle
  main = page.driver.browser.window_handles.first
  #Get the popup window handle
  popup = page.driver.browser.window_handles.last

  #Then switch control between the windows
  page.driver.browser.switch_to.window(popup)

  page.driver.browser.switch_to.frame("searchFrame")


  # within(".pBody") do
    # find("go").click
  # end

  #find("#theForm").first("div").all("div")[2].all("input")[2].click

  #find(:xpath, "//*[@id='theForm']/div/div[2]/input[2]").click

我已尝试在方法和查找方法中,使用xpath单击Go按钮,但我得到的错误是“陈旧元素引用:元素未附加到页面文档”。请帮帮我?

1 个答案:

答案 0 :(得分:1)

第一步是停止使用特定于驱动程序的方法,并使用为处理Windows而提供的Capybara API。假设您在当前会话中,您可以

popup = window_opened_by do
  click_link 'open new window' # whatever action opens the popup window
end

within_window(popup) do
  click_button('Go!')
end

注意 - 这不会处理您尝试切换到的任何“searchFrame”框架,但HTML中也没有显示该框架,所以它不清楚是什么进行 - 如果框架在弹出窗口然后你会使用Capybara提供的`#within_frame'来切换到within_window块内的框架,如

within_window(popup) do
  within_frame('searchFrame') do
    click_button('Go!')
  end
end
相关问题