Javascript WebDriverIO浏览器。单击';在Chrome中找不到元素,在Firefox中运行良好

Javascript WebDriverIO浏览器。单击';在Chrome中找不到元素,在Firefox中运行良好,javascript,selenium,selenium-webdriver,webdriver-io,cucumberjs,Javascript,Selenium,Selenium Webdriver,Webdriver Io,Cucumberjs,我正在使用WebdriverIO和CucumberJS进行测试。 下面的代码在Firefox中运行良好,但我在Chrome中遇到错误,显示元素不可单击。我正在寻找JavaScript的解决方案 this.Then('I click on View Coupon Details button on a random coupon', () => { const randomElement = getRandomIndex(couponsCount); assert.ok(co

我正在使用WebdriverIOCucumberJS进行测试。 下面的代码在Firefox中运行良好,但我在Chrome中遇到错误,显示
元素不可单击。我正在寻找JavaScript的解决方案

this.Then('I click on View Coupon Details button on a random coupon', () => {
    const randomElement = getRandomIndex(couponsCount);
    assert.ok(coupons.value[randomElement].element('.print-coupon').click('a'));
});
优惠券
是一组
网络元素
。我正在尝试单击查看优惠券详细信息按钮

示例页面:

谢谢,

Vinod

在编写代码之前尝试使用浏览器暂停

browser.pause(2000);
有时,这种情况是由于chrome延迟而发生的。它总是对我有用


更多信息:

第一种方法是,您可以从inspector和中复制99%的xpath/css选择器。单击将始终有效。如果没有,有两种选择

如果您运行脚本localhost并具有访问权限,则可以执行以下操作

.execute(function(a, b, c, d) {
$('#button').click();
return a + b + c + d;
}, 1, 2, 3, 4).then(function(ret) {
// node.js context - client and console are available
log(ret.value); // outputs: 10
});
或者用这种方式。鼠标将被按下并释放。如果使用webdriver.io右键单击以了解坐标位置,则可以找到正确的位置

.moveToObject('#button', 0, -103)
.buttonDown()
.moveToObject('#button', 0, -104)
.buttonUp()

不确定是否有更好的方法来实现这一点,我使用getLocation()并滚动到按钮的位置,使其在视口中可见,然后单击它

this.Then('I click on View Coupon Details button on a random coupon', () => {
    const randomElement = getRandomIndex(couponsCount);
    const pos = coupons.value[randomElement].getLocation();
    browser.scroll(pos.x, pos.y);
    browser.pause(200); // we can use waitforVisible .print-coupon as well
    assert.ok(coupons.value[randomElement].element('.print-coupon').click('a'));
});

为什么建议使用browser.pause(),这在很多情况下都是不好的做法?Wdio有更合适的方法,如waitForVisible、waitForEnabled等。是的,这是一种不好的做法,但您可以尝试一下,看看是否有效。有时我不得不这样使用chromeAdding浏览器。暂停没有帮助。仍然出现相同的错误。
在点(164635)处不可单击。其他元素将收到单击:
您是否也可以添加您正在测试的项目的视图,有时很难访问嵌套的div我注意到在打开页面时,页面中有一个模式/弹出窗口。你应该先处理好这个问题,然后才能与页面上的项目交互。我正在本地测试它,禁用所有弹出窗口,但仍然面临相同的问题。我添加了一个没有弹出窗口的不同站点。您能添加更多关于您答案的描述吗?