【问题标题】:Unable to click on Web Element using Selenium with Java使用 Selenium 和 Java 无法单击 Web 元素
【发布时间】:2020-03-20 22:33:57
【问题描述】:

我尝试了 WebDriverWait、.click()、.sendKeys(Keys.RETURN)、implicitWait、explicitWait 和许多其他方法,但我无法单击此 Web 元素。

<div class="actions style-scope tv-overlay-record-on-the-go">
  <tv-button pill-action="" class="style-scope tv-overlay-record-on-the-go x-scope tv-button-2 left"><button class="style-scope tv-button"><div class="wrapper style-scope tv-button"><iron-icon id="icon" class="style-scope tv-button x-scope iron-icon-0"></iron-icon><div id="content" role="presentation" class="style-scope tv-button">Got it</div></div></button>
    <div
      class="hover-hint style-scope tv-button"> </div>
</tv-button>
</div>

基于上面的 HTML 代码,我创建了以下 xpath:

WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[@id='content']"));

gotIt.click();

我相信代码运行并确认按钮在那里,因此 Web 元素已成功创建。但是,当我尝试使用多种交互方法与之交互时,什么也没有发生。

我得到的异常:org.openqa.selenium.ElementNotInteractableException: element not interactable

【问题讨论】:

  • 您遇到了什么异常,请添加有问题的。另外检查是否在 iframe 内
  • 检查 iframe 上是否存在“得到它”按钮。如果是这样,那么您必须在按下任何按钮之前切换到它。

标签: java html selenium automation selenium-chromedriver


【解决方案1】:

我会尝试查询文本,而不仅仅是div 的 ID 属性——您的 XPath 可能会返回许多结果,然后单击第一个结果,这就是没有任何反应的原因。

我会试试这个:

WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[text()='Got it']"));

//gotIt.click(); throws element not interactable

// you can also try Javascript click -- work around element not interactable issue
((JavascriptExecutor)driver).executeScript("arguments[0].click();", gotIt);

我将您的 XPath 更改为查询“知道了”文本,而不是 ID 属性 content,它可能会返回多个结果。我还包含了执行 Javascript 点击的代码,这可能有助于解决任何点击问题。

Xfinity 网站似乎有很多 iframe 元素,但我无法访问您的特定页面进行查看,因此这可能会也可能不会导致问题。

【讨论】:

  • 我可以在清除 xfinity 缓存后访问此页面。这段代码看起来应该可以工作,但我得到的元素不可交互。我已经在不同的 URL 上尝试过这种类型的执行,它完美地工作。也许它是一个 iframe 元素,将不得不调查。我对此有点陌生。
  • @MayankYadav 删除 gotIt.click(); 并运行 executeScript 行。这将解决ElementNotInteractable
  • 我删除了 gotIt.click();并且程序通过了这两项测试。但是,它根本没有点击“得到它”按钮。
  • 如果你打印driver.findElements(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[text()='Got it']")).size();会发生什么?我想知道这个选择器是否在页面上定位多个元素,这是问题的一部分。
  • 这正是问题所在。我发现它遇到了 4 个不同的元素。我使 xpath 更简洁,它工作。感谢你的帮助! WebElement gotIt = driver.findElement(By.xpath("//tv-button[@class='style-scope tv-overlay-record-on-the-go x-scope tv-button-2 left']/button[ @class='style-scope tv-button' and 1]"));
【解决方案2】:

如果没有,请检查您的按钮是否不在 iframe 上,然后您可以在 XPath 下方找到使用包含单击“得到它”按钮

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'Got it')]")));
element1.click();

解决方案 2:

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'Got it')]")));
Actions builder = new Actions(driver);
Action buttonClick = builder.moveToElement(element1).click().build();

【讨论】:

  • 遇到了和我平时一样的错误。元素不可交互。
  • 检查更新的解决方案,并检查你不在 iframe 中,否则你需要切换到 iframe ,这个异常告诉元素存在于 HTML DOM 上,但它不是可以交互的状态与
  • 执行此操作后,仍然没有单击“知道”按钮。预期条件失败:等待元素可点击:By.xpath: //div[contains(text(),'Got it')](尝试 10 秒,间隔 500 毫秒)
  • 好的,我认为您需要切换到 iframe 共享您的链接或完成 html。或者在单击“获取它”按钮之前尝试 driver.switchto.frame(1)
  • 文件太大,无法附加。
【解决方案3】:

请尝试操作对象点击如下:

    WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[@id='content']"));

    Actions actions = new Actions(driver);
    actions.moveToElement(gotIt).click().build().perform();

【讨论】:

  • 在 Dipak 建议后尝试了该解决方案。没用。
猜你喜欢
  • 2016-12-05
  • 2015-09-01
  • 2016-09-05
  • 2017-07-25
  • 2018-05-26
  • 2017-10-11
  • 2022-12-09
  • 2020-11-10
相关资源
最近更新 更多