【发布时间】:2020-04-29 05:47:58
【问题描述】:
我的 HTML 如下所示
<div class="container">
<div class="jumbotron">
<h1 id="survey-title" class="display-5 text-capitalize">Your favourite candidate</h1>
<hr class="my-4">
<div class="card-body">
<h5 id="question-title" class="card-title">What is your name?</h5>
<form id="question_form" action="/survey/takesurvey/1/2" method="post" class="form">
<input type="hidden" name="csrfmiddlewaretoken" value="ELBWYGZRxRuI7CoZ2xkmgCJ3f9JweFMM4Ew1pQbgSE3BLb38VStPJEbHvsyiBEFg">
<div class="form-group"><input type="text" name="response" value="asdfasdfas" maxlength="400" class="form-control" title="" id="id_response"></div>
</form>
</div>
<a id="btn-previous" href="javascript:history.back()" class="btn btn-primary ">Previous</a>
<a id="btn-next" href="/survey/takesurvey/finish/1" class="btn btn-primary">Next</a>
</div>
</div>
注意上一个按钮上的href
<a id="btn-previous" href="javascript:history.back()" class="btn btn-primary ">Previous</a>
点击下一步将提交表单并转到“下一页”页面,再次点击“上一页”应返回并保留上一页中的输入值。我正在使用以下代码来测试此行为,以下代码仅在我将sleep(1)line 放在查找元素和单击之间时才有效。否则页面不会返回(网址不变)
- 为什么在点击按钮前等待网址更改不起作用
- 我怀疑这与未加载 javascript 事件处理程序(在 html 中内联)有关。
- 我有什么方法可以检查页面是否已为点击事件做好准备
cur_url = self.browser.current_url
self.browser.find_element_by_id('btn-next').click()
wait(lambda: self.assertNotEqual(self.browser.current_url, cur_url))
btn_previous = self.browser.find_element_by_id('btn-previous')
## This code is not working without the sleep below. Which I suspect
## has to do with javascript event handler (which is inline in html) not being loaded.
## Is there better way to check that the button is ready to be clicked
sleep(1)
btn_previous.click()
【问题讨论】:
-
如果你在做测试,应该在 onclick 而不是 href。如果您没有返回,还有更简单的方法。
-
当我把它放在 onclick 时是一样的,当我把它放在
sleep(1)时你可能会注意到它的工作原理
标签: python selenium selenium-chromedriver