为什么量角器'EC.elementToBeClickable'没有像它声明的那样实现?

时间:2018-03-12 01:39:07

标签: selenium-webdriver protractor

我有一个元素(按钮'che-in / boarding pass'),该类首先被“禁用”,然后在点击另一个元素(按钮'check eligibility')后启用。请参阅屏幕截图。

在:

enter image description here

后:

enter image description here

我的量角器测试步骤是:

1)点击“检查资格”按钮,然后点击

2)点击“登记/登机牌”按钮

我在点击'登记/登机牌'按钮之前使用标准等待功能,例如:

var elm = element(by.id('xxxxxx'));
var EC = protractor.ExpectedConditions;

browser.wait(EC.elementToBeClickable(elm), 5000);
elm.click();

但它确实起作用。它给出了错误'WebDriverError:未知错误:元素xxx在点(759,725)处无法点击。其他元素将收到点击'

然后我去检查'EC.elementToBeClickable'的定义,它说,

 /**
 * An Expectation for checking an element is visible and enabled such that you
 * can click it.
 *
 * @example
 * var EC = protractor.ExpectedConditions;
 * // Waits for the element with id 'abc' to be clickable.
 * browser.wait(EC.elementToBeClickable($('#abc')), 5000);
 *
 * @alias ExpectedConditions.elementToBeClickable
 * @param {!ElementFinder} elementFinder The element to check
 *
 * @returns {!function} An expected condition that returns a promise
 *     representing whether the element is clickable.
 */
elementToBeClickable(elementFinder: ElementFinder): Function;

具体说它将检查元素是否“可见并启用”。那为什么我的代码片段不起作用?

P.S。

  1. 我使用的元素定位器是正确的,因为如果我在点击第一个按钮和第二个按钮之前添加5秒的硬编码等待,那么一切正常

  2. elementToBeClickable()似乎根本没有等待。它几乎立即抛出错误。

1 个答案:

答案 0 :(得分:1)

我认为您的问题与Selenium如何确定元素启用或禁用有关。根据WebDriver W3C Sepcification,我发现Selenium使用以下规则来确定元素是启用还是禁用。

您的按钮使用CSS pointer-events: none来存档禁用效果,Selenium不会将其纳入计算逻辑。

实际上,您可以尝试获取按钮的disabled属性,只有false个事件才能点击它。我认为Selenium使用disabled属性作为元素启用与否的结果。

所以你需要等待pointer-events的CSS值不是none,而不是调用EC.elementToBeClickable

enter image description here enter image description here