Click()方法并不总是有效 [英] Click() method will not always work

查看:119
本文介绍了Click()方法并不总是有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Selenium WebDriver中的测试有问题。当程序试图单击按钮时,单击事件并不总是有效。在一次测试中,一切都很好,在其他测试中则不然。

I have a problem with my tests in Selenium WebDriver. The Click event not always works when a program tries to click on button. In one test everything is ok, in others it is not.

每个测试都从一个页面开始。首先,用户必须从选择组件中选择一个选项,然后用户单击一个按钮。

Every test starts from one page. First the user has to choose an option from a select component and after that the user clicks on a button.

我想知道为什么有一次一切都好,而且当我第二次运行测试时它不是?

I want to know why one time everything is ok, and when I run tests a second time it is not?

以下是查找和点击按钮的源代码:

Here is the source code of finding and clicking the button:

public void clickContinueBtn() {    
    webElement = driver.findElement(By.xpath("//div[@class='btn magenta_s']/a/span"));
    webElement.click(); 
}


推荐答案

我遇到了类似的问题。 click方法适用于其他页面,然后在特定页面上根本不起作用。

I ran into a similar issue. The click method worked on other pages, then didn't work at all on a particular page.

竞争条件导致问题:


  1. HTML内容随着按钮已禁用。

  2. 在javascript onload 事件已被触发(或已完成执行)。所以 button.click 将出现在已禁用的元素上。没有任何事情会发生。

  3. 然后javascript onload事件将触发(或完成执行)并且javascript将启用该按钮。

  4. 我查看了页面,无法弄清楚为什么我的代码无法正常工作,因为按钮似乎在检查时启用了,如果我手动点击按钮,它就可以工作。

  1. HTML content is rendered with the button disabled.
  2. The selenium web driver script was executed before the javascript onload event was triggered (Or finished executing). So the button.click would occur on a disabled element. And nothing would happen.
  3. Then the javascript onload event would trigger (or finish executing) and the javascript would enable the button.
  4. I looked at the page and couldn't figure out why my code wasn't working because the button appeared to be enabled upon inspection, and if I manually clicked the button, it worked.

一旦我发现这是一个时间问题,我在这里找到了解决方案:如何让Selenium Web Driver等待元素可访问,不只是在场?

Once I figured out that it was a timing issue, I found the solution here: How can I get Selenium Web Driver to wait for an element to be accessible, not just present?

用Ruby解释解决方案:

To paraphrase the solution in Ruby:

//This will not return the button until it is enabled.
button = driver.find_element(:xpath,  "//button[@id='myButtonId' and not(@disabled)]")
button.click

这篇关于Click()方法并不总是有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆