【发布时间】:2020-12-04 16:47:46
【问题描述】:
为什么会出现这个错误?:
OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#gbqfq"}
这是我用来打开浏览器并在谷歌搜索中搜索给定单词的代码:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://google.com");
IWebElement element = driver.FindElement(By.Id("gbqfq"));
element.SendKeys("APPLES");
// Get the search results panel that contains the link for each result.
IWebElement resultsPanel = driver.FindElement(By.Id("search"));
// Get all the links only contained within the search result panel.
ReadOnlyCollection<IWebElement> searchResults = resultsPanel.FindElements(By.XPath(".//a"));
// Print the text for every link in the search results.
int resultCNT = 1;
foreach (IWebElement result in searchResults)
{
if (resultCNT <= 5)
{
Console.WriteLine(result.Text);
}
else
{
break;
}
resultCNT ++;
}
有人知道可能是什么问题吗?谢谢
完整日志:
OpenQA.Selenium.NoSuchElementException
HResult=0x80131500
Message=no such element: Unable to locate element: {"method":"css selector","selector":"#gbqfq"}
(Session info: chrome=84.0.4147.125)
Source=WebDriver
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.FindElementById(String id)
at OpenQA.Selenium.By.<>c__DisplayClass16_0.<Id>b__0(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at $Program.$Main(String[] args) in C:\Users\gabri\source\repos\Example\Example\Program.cs:line 10
【问题讨论】:
-
您确定页面已加载吗? PageSource 返回什么? saucelabs.com/resources/articles/… 是一个等待找到元素的示例。
-
它在谷歌浏览器中打开了谷歌搜索,仅此而已
标签: c# selenium xpath css-selectors webdriverwait