OpenQA.Selenium.ElementNotInteractableException:无法单击元素

时间:2018-05-31 06:24:11

标签: c# selenium selenium-webdriver automation

public PriceChangeRequestStatus SelectPcrFrom(String[] fromdate)
        {
            wrapper.Click(imgPcrFrom);


            String year, day, month;
            day = fromdate[0];
            month = fromdate[1];
            year = fromdate[2];

            //for (int index = 0; index <= 1; index++)

                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);
            // By Sunayana
            //IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
            //js.ExecuteScript("document.getElementsByClassName('ajax__calendar_year')");

            // End


            ////   IReadOnlyList<IWebElement> yearClick =  wrapper.GetElements("xpath:=.//div[@class='ajax__calendar_year']");
            //IList<IWebElement> yearClick = driver.FindElements(By.XPath(".//div[@class='ajax__calendar_year']"));
            //Console.WriteLine(yearClick.Count);
            //for (int index = 1; index-1 <= yearClick.Count; index++)
            //{
            //    int i=0;
            //    Console.WriteLine(index);
            //    i=index;

            //    if (yearClick[i].GetAttribute("innerHTML").Equals(year))
            //    {
            //        yearClick[i].Click();
            //        Console.WriteLine("I am here");
            //    }
            //    else {
            //        Console.WriteLine("Sorry");
            //    }
            //}
            Thread.Sleep(4000);

            //var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

            //try
            //{
            //    wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[contains(text(),'" + year + "')]"))).Click();
            //}
            //catch (WebDriverTimeoutException)
            //{
            //    Console.WriteLine("Logout button was not visible!");
            //}

            IWebElement clickyear = driver.FindElement(By.XPath("//div[contains(text(),'" +year+ "')]"));
            clickyear.Click();
            //Actions action = new Actions(driver);
            //action.MoveToElement(clickyear).Click().Build().Perform();

            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;
         }

编辑代码并尝试运行脚本。为SELECTPCRFROM编写的逻辑工作正常,但同样的逻辑不适用于SELECTPCRTO。尝试了许多其他解决方案,但SELECTPCRTO仍然没有按预期工作请帮助我。执行的代码显示标题

中提供的错误消息

Screenshot of the Calendar

1 个答案:

答案 0 :(得分:0)

大多数日期选择器都是由xpath标识的,但是当你尝试对它们执行操作时,它们会给你一个错误。而是尝试使用javascript方式。

Java -

document.evaluate(PATH, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

相似你可以在C#中使用它 请参阅此处的答案 - link