【问题标题】:Unable to click on element using method: browser.click and xpath: //input[2]无法使用方法单击元素:browser.click 和 xpath://input[2]
【发布时间】:2018-08-31 14:33:02
【问题描述】:

似乎收到以下异常:

element not visible
running chrome
Error: An element command could not be completed because the element is not visible on the page.
    at elementIdClick("0.12980231457632274-1") - click.js:20:22

我的测试如下:

    beforeEach(function() {
      browser.url("https://www.google.com");
    })
    describe('Test Google Homepage', function() {
        it('Should be able to click on im feeling lucky button', function(done) {
          browser.click('(//input)[7]');
    });
});

即使添加浏览器暂停,xpath 似乎也会抛出上面的异常,任何想法为什么我会收到异常?

【问题讨论】:

  • zfrisch im 使用 xpath 选择器定位“我感觉很幸运”按钮。
  • 您是否在定位此元素之前添加了足够的等待时间?另外,我建议参考其属性使用定位器。例如://input[@value=\"手气不错\"]
  • 我尝试添加暂停,但似乎存在相同的问题:/ 我的意图是使用数字 7 作为循环的一部分来单击多个按钮实例,但无法缩小问题范围;尤其是当同一个定位器在浏览器本身中工作时。
  • 我对带有 selenium 的 javascript 了解不多,但你能确定你的语法是否正确。不应该是 browser.click("(//input)[7]");而不是单引号?
  • 还尝试了以下仍然无效的工作:(

标签: javascript node.js selenium-webdriver webdriver-io


【解决方案1】:

因为你不应该使用 xpath?

尝试: browser('[aria-label="I\'m Feeling Lucky"]').click()

另外,使用命名的东西比指望页面中事物的顺序要好得多。

【讨论】:

  • 感谢 Tom 的评论,如果我打算使用索引号 7 作为循环的一部分,通过引用 xpath 来单击多个元素,该怎么办?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多