[英]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.