OpenQA.Selenium.ElementNotInteractableException:SelectPcrTo点击元素失败求助
ElementNotInteractableException in SelectPcrTo Method Let's tackle this tricky issue—your SelectPcrFrom works flawlessly, but the nearly identical SelectPcrTo throws an ElementNotInteractableException when trying to click elements in the calendar. First, let's recap the problem details:
Exception Details
OpenQA.Selenium.ElementNotInteractableException : Cannot click on element
Relevant Code
Here's the code for both methods (and the helper findMonth function):
public PriceChangeRequestStatus SelectPcrFrom(String[] fromdate) { wrapper.Click(imgPcrFrom); String year, day, month; day = fromdate[0]; month = fromdate[1]; year = fromdate[2]; wrapper.Click(lnkPcrFrom); wrapper.Click(lnkPcrFrom); Thread.Sleep(4000); driver.FindElement(By.XPath("//div[contains(text(),'" + year + "')]")).Click(); Thread.Sleep(4000); driver.FindElement(By.XPath("//div[contains(text(),'" + findMonth(month).Substring(0, 3) + "')]")).Click(); Thread.Sleep(2000); driver.FindElement(By.XPath("//div[contains(@title,'" + findMonth(month) + " " + day + ", " + year + "')]")).Click(); Thread.Sleep(2000); return this; } public PriceChangeRequestStatus SelectPcrTo(String[] todate) { wrapper.Click(imgPcrTo); wrapper.Click(lnkPcrTo); wrapper.Click(lnkPcrTo); String year, day, month; day = todate[0]; month = todate[1]; year = todate[2]; Thread.Sleep(4000); IWebElement clickyear = driver.FindElement(By.XPath("//div[contains(text(),'" +year+ "')]")); clickyear.Click(); Thread.Sleep(4000); driver.FindElement(By.XPath("//div[contains(text(),'" + findMonth(month).Substring(0, 3) + "')]")).Click(); Thread.Sleep(2000); driver.FindElement(By.XPath("//div[contains(@title,'" + findMonth(month) + " " + day + ", " + year + "')]")).Click(); Thread.Sleep(2000); return this; } public String findMonth(String month) { String CorrectMonth = ""; switch (month) { case "01": CorrectMonth = "January"; break; case "02": CorrectMonth = "February"; break; case "03": CorrectMonth = "March"; break; case "04": CorrectMonth = "April"; break; case "05": CorrectMonth = "May"; break; case "06": CorrectMonth = "June"; break; case "07": CorrectMonth = "July"; break; case "08": CorrectMonth = "August"; break; case "09": CorrectMonth = "September"; break; case "10": CorrectMonth = "October"; break; case "11": CorrectMonth = "November"; break; case "12": CorrectMonth = "December"; break; } return CorrectMonth; }
Calendar UI Reference

Now, let's go through the most likely fixes and debugging steps:
1. Replace Hardcoded Sleeps with Explicit Waits
Thread.Sleep is unreliable because it doesn't account for actual page/calendar loading times. The "To" calendar might take a split second longer to render than the "From" one, leading to clicks on non-interactive elements.
Update your SelectPcrTo method to use explicit waits for each element:
public PriceChangeRequestStatus SelectPcrTo(String[] todate) { wrapper.Click(imgPcrTo); wrapper.Click(lnkPcrTo); // Wait for the calendar to become visible first var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15)); wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[contains(@class,'ajax__calendar_container')]"))); String year = todate[2], day = todate[0], month = todate[1]; // Wait for year element to be clickable before clicking var yearElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(text(),'" + year + "')]"))); yearElement.Click(); // Wait for month element var monthShort = findMonth(month).Substring(0, 3); var monthElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(text(),'" + monthShort + "')]"))); monthElement.Click(); // Wait for day element var dayTitle = $"{findMonth(month)} {day}, {year}"; var dayElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(@title,'" + dayTitle + "')]"))); dayElement.Click(); return this; }
2. Make Your XPaths Specific to the "To" Calendar
Your current XPaths might be matching elements from the "From" calendar (if it's still in the DOM, even hidden). Check the HTML structure of the "To" calendar—if it has a unique parent ID or class, adjust your XPaths to target only elements inside that container.
For example, if the "To" calendar has a parent div with id="pcr-to-calendar", modify your year XPath like this:
By.XPath("//div[@id='pcr-to-calendar']//div[contains(text(),'" + year + "')]")
3. Use JavaScript Executor to Force Clicks (Last Resort)
If the element is visible but Selenium can't click it due to CSS issues (like overlays, z-index, or custom event handlers), force the click via JavaScript:
IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("arguments[0].click();", yearElement);
4. Verify the Calendar Toggle Logic
You're clicking lnkPcrTo twice—this might be accidentally closing the calendar right after opening it. Try reducing it to a single click, or wait for the calendar to appear after the first click before clicking again:
wrapper.Click(lnkPcrTo); wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[contains(@class,'ajax__calendar_container')]"))); // Remove the second click unless it's explicitly needed to switch views
Debugging Tips
- Inspect the DOM: When the "To" calendar is open, right-click the year/month/day element and check:
- Is it marked as
disabled? - Does it have
display: noneorvisibility: hiddenin its CSS? - Is another element (like an overlay) covering it?
- Is it marked as
- Log Element State: Add console logs to check if the element is actually interactive:
Console.WriteLine($"Year element displayed: {yearElement.Displayed}"); Console.WriteLine($"Year element enabled: {yearElement.Enabled}");
内容的提问来源于stack exchange,提问作者Hargovind

