简体   繁体   中英

Getting error when trying to find element by xpath - System.InvalidOperationException: String cannot be cast to WebElement?

I am getting error on select2 element " System.InvalidOperationException: java.lang.String cannot be cast to org.openqa.selenium.WebElement " when trying to select value from it. It's strange that sometimes test passes and I am not having problems on other select2 elements for which I use the same code. What can be the problem here?

    Driver.FindElement(By.XPath("//*[@id='s2id_State']/a/span[2]/b")).Click();
    Driver.FindElement(By.CssSelector("#select2-drop input.select2-input")).SendKeys("1000");
    wait.Until(ExpectedConditions.TextToBePresentInElementLocated(By.XPath("//table/tbody/tr/td[1]/div[text()='1000']"), "1000")); //Error line
    Driver.FindElement(By.XPath("//table/tbody/tr/td[1]/div[text()='1000']")).Click();

Result StackTrace:  


  at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByXPath(String xpath)
   at OpenQA.Selenium.By.<>c__DisplayClasse.<XPath>b__c(ISearchContext context)
   at OpenQA.Selenium.By.FindElement(ISearchContext context)
   at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
   at OpenQA.Selenium.Support.UI.ExpectedConditions.<>c__DisplayClass26.<TextToBePresentInElementLocated>b__25(IWebDriver driver)
   at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
   at Automated.Test.Select2Test() in C:\....:line 86

Remote Webdriver :

     case "remote":
         var huburl = new Uri(ConfigurationManager.AppSettings["SeleniumHubAddress"]);
         DesiredCapabilities capabilities;
         switch (ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower())
                        {
                            case "firefox":
                                capabilities = DesiredCapabilities.Firefox();
                                break;
                            case "ie":
                                capabilities = DesiredCapabilities.InternetExplorer();
                                break;
                            case "chrome":
                                capabilities = DesiredCapabilities.Chrome();
                                break;
                            default:
                                throw new NotImplementedException(string.Format("WebDriverBrowserCapabilities of \"{0}\" is not implemented for {1} mode", ConfigurationManager.AppSettings["WebDriverBrowserCapabilities"].ToLower(), ConfigurationManager.AppSettings["WebDriverMode"].ToLower()));
                        }

         capabilities.IsJavaScriptEnabled = true;
         driver = new AdvancedRemoteWebDriver(huburl, capabilities);
         break;
     default:
         throw new NotImplementedException();

如果您使用的文本用户如下xpath:

//table/tbody/tr/td[1]/div[text()='1000']"

You can try to use explicit wait with ExpectedConditions

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.TextToBePresentInElementLocated(By.XPath("//table/tbody/tr/td[1]/div"), "1000")).Click();

This will wait up to 10 seconds for the element to have the text "1000" before clicking on it. You can also use

wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//table/tbody/tr/td[1]/div[.='1000']"))).Click();

Selenium for C# has several pre-constructed parameters for the WebDriverWait.Until() method. So you need not call FindElement() .

Maybe you can try something like

wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//table/tbody/tr/td[1]/div[.='1000']")));

If this doesn't work either, then maybe your XPath string is not correct (I just copied it from your question into my answer).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM