【问题标题】:Selenium ElementNotInteractableError: element not interactableSelenium ElementNotInteractableError:元素不可交互
【发布时间】:2021-04-22 01:43:25
【问题描述】:

我今天早上开始学习 Selenium,哈哈,但我一直在尝试点击这样的元素:

<a rel="nofollow" style="" href="javascript:void(0);" time="" itemid="1523208" data-has-req="0" class="button okletsdothis nonsubscriber dl button-green" title="Download">
  <span class="icon-download" time="" itemid="1523208"></span>
  Download
</a>

在控制台中我收到此错误

ElementNotInteractableError: 元素不可交互

这是我等到找到元素然后尝试执行点击操作的代码

let downloadButton = await driver.wait(
    until.elementLocated(
      By.xpath(
        '(//a[@class="button okletsdothis nonsubscriber dl button-green"])'
      )
    ),
    100000,
    "Timed out after 30 seconds",
    5000
  );

await downloadButton.click();

如果我更改 xpath 并单击这样的元素

By.xpath('(//div[@class="download-button ide"])')

它工作正常,但在我无法使其工作之前的情况下。请帮忙。

【问题讨论】:

  • 两个 xpath 都不一样。。不知道你想问什么。。你不仅改变了 xpath,还改变了元素
  • 我想表明,对于像 div 这样的另一个元素,点击似乎可以正确触发,但是对于锚元素它不起作用......问题是为什么我会遇到这个错误锚元素
  • 链接是否手动工作?我怀疑它有 href="javascript:void(0); 意味着它不会在点击时执行任何操作
  • 使用直到元素可点击,而不是等到元素被定位。如果链接手动工作,那么它应该有帮助

标签: javascript selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

要点击文本为下载的元素,您可以使用以下Locator Strategies

let downloadButton = await driver.wait(
    until.elementLocated(
      By.xpath(
    '(//a[@class="button okletsdothis nonsubscriber dl button-green" and @title="Download"])'
      )
    ),
    100000,
    "Timed out after 30 seconds",
    5000
  );

await downloadButton.click();

或者

let downloadButton = await driver.wait(
    until.elementLocated(
      By.xpath(
    '(//a[@class="button okletsdothis nonsubscriber dl button-green" and @title="Download"]/span[@class="icon-download"])'
      )
    ),
    100000,
    "Timed out after 30 seconds",
    5000
  );

await downloadButton.click();

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 1970-01-01
    • 2020-03-08
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2019-04-19
    • 1970-01-01
    相关资源
    最近更新 更多