BMR
BMR

Reputation: 1

How to select button appearing in a new pop-up window in 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>

This above is the code for content of pop-up window and i have to select the Go button.

My code is as follows:

  #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

I've tried within method and find method, using xpath to click the Go button but the error i get is "stale element reference: element is not attached to the page document". Help me out, please?

Upvotes: 0

Views: 1515

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

Step one is to to stop using driver specific methods and use the Capybara API provided for working with windows. Assuming you are in the current session you can do

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

Note - this isn't handling any 'searchFrame' frame that you attempt to switch to, but that frame isn't shown in the HTML either, so it's not exactly clear whats going on there - if the frame is in the popup window then you would use `#within_frame' provided by Capybara to switch to the frame inside the within_window block like

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

Upvotes: 1

Related Questions