如何 select 按钮出现在水豚的新弹出窗口 window 中?
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>
以上是弹出窗口内容的代码 window,我必须 select 前往按钮。
我的代码如下:
#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 单击“开始”按钮,但我得到的错误是 "stale element reference: element is not attached to the page document"。请帮帮我好吗?
第一步是停止使用特定于驱动程序的方法,并使用为 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 中,因此不清楚发生了什么那里 - 如果框架在弹出 window 中,那么您将使用 Capybara 提供的“#within_frame”来切换到 within_window 块内的框架,如
within_window(popup) do
within_frame('searchFrame') do
click_button('Go!')
end
end
<!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>
以上是弹出窗口内容的代码 window,我必须 select 前往按钮。
我的代码如下:
#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 单击“开始”按钮,但我得到的错误是 "stale element reference: element is not attached to the page document"。请帮帮我好吗?
第一步是停止使用特定于驱动程序的方法,并使用为 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 中,因此不清楚发生了什么那里 - 如果框架在弹出 window 中,那么您将使用 Capybara 提供的“#within_frame”来切换到 within_window 块内的框架,如
within_window(popup) do
within_frame('searchFrame') do
click_button('Go!')
end
end