简体   繁体   English

尝试通过xpath查找元素时出错 - System.InvalidOperationException:String无法强制转换为WebElement?

[英]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. 我在select2元素上遇到错误“System.InvalidOperationException:java.lang.String在尝试从中选择值时无法转换为org.openqa.selenium.WebElement”。 It's strange that sometimes test passes and I am not having problems on other select2 elements for which I use the same code. 奇怪的是,有时候测试通过,而我在使用相同代码的其他select2元素上没有问题。 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 : 远程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 您可以尝试使用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. 在点击之前,元素将等待最多10秒,以使文本为“1000”。 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. Selenium for C#为WebDriverWait.Until()方法提供了几个预先构造的参数。 So you need not call FindElement() . 所以你不需要调用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). 如果这也不起作用,那么你的XPath字符串可能不正确(我只是将你的问题复制到我的答案中)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试foreach遍历列表时获取System.InvalidOperationException吗? - Getting System.InvalidOperationException when Trying to foreach loop over a List? 尝试打开和OLE DB连接时获取“ System.InvalidOperationException” - Getting “System.InvalidOperationException” When trying to open and OLE DB connection 无法连接到数据库,获取System.InvalidOperationException - Cannot connect to database, getting System.InvalidOperationException 尝试保存在数据库中时 System.InvalidOperationException - System.InvalidOperationException when trying to save in database 收到错误-未处理System.InvalidOperationException - getting error - System.InvalidOperationException was unhandled 使用ApiController获取列表时出现System.InvalidOperationException - System.InvalidOperationException when getting a List with an ApiController 填充数组时出现System.InvalidOperationException错误 - System.InvalidOperationException error when populating an array System.InvalidOperationException:转发新会话时出错:无法找到:功能{browserName:chrome,marionette:false} - System.InvalidOperationException : Error forwarding the new session cannot find : Capabilities {browserName: chrome, marionette: false} System.InvalidOperationException:&#39;如果没有元素,则无法分配本机控件; - System.InvalidOperationException: 'Cannot assign a native control without an Element; System.InvalidOperationException:找不到包MessagePack的编译库位置吗? - System.InvalidOperationException: Cannot find compilation library location for package MessagePack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM