首页 > 解决方案 > 如何单击元素c#的中心?

问题描述

我对 selenium 和 c# 很陌生,单击此元素时遇到问题:

<i _ngcontent-ldd-c264="" class="close ms-icon iconms-cancel02 fixCloseModal">
 <::before>

我试图用类和 xpath 找到元素,但是每当我尝试单击时,我都无法找到。

IWebElement CloseCard = driver.FindElement(By.ClassName("fixCloseModal"));
CloseCard.Click();

上面是尝试使用类名查找元素的示例。我得到这个异常:“OpenQA.Selenium.ElementNotInteractableException:'元素不可交互”。

用 chrome 检查,需要点击的按钮在"::before".
类前面的行突出显示可点击区域的轮廓。

如果问题不清楚,请道歉,如果我需要澄清,请告诉我。

标签: c#seleniumselenium-chromedriver

解决方案


如果页面在重新渲染时正在移动,则可能会发生这种情况。显式等待可以帮助解决问题:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
var closeCard = wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector(".fixCloseModal")));

closeCard.Click();

推荐阅读