C# 使用Selenium和C在Google主页上找不到搜索框元素#
为什么我会出现这个错误C# 使用Selenium和C在Google主页上找不到搜索框元素#,c#,selenium,xpath,css-selectors,webdriverwait,C#,Selenium,Xpath,Css Selectors,Webdriverwait,为什么我会出现这个错误 OpenQA.Selenium.NoSuchElementException: 'no such element: Unable to locate element: {"method":"css selector","selector":"#gbqfq"} 这是我用来打开浏览器并在谷歌搜索中搜索给定单词的代码: IWebDriver driver = new ChromeDriver()
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 ++;
}
IWebDriver=新的ChromeDriver();
driver.Navigate().gotour(“http://google.com");
IWebElement元素=driver.FindElement(By.Id(“gbqfq”);
元素。发送键(“苹果”);
//获取包含每个结果链接的“搜索结果”面板。
IWebElement resultsPanel=driver.FindElement(By.Id(“搜索”));
//获取仅包含在搜索结果面板中的所有链接。
ReadOnlyCollection searchResults=resultsPanel.FindElements(By.XPath(“.//a”);
//打印搜索结果中每个链接的文本。
int resultCNT=1;
foreach(搜索结果中的IWebElement结果)
{
如果(resultCNT作为您的用例是打开浏览器并在其中搜索给定单词,则搜索框HTML如下所示:
<input class="gLFyf gsfi" maxlength="2048" name="q" type="text" jsaction="paste:puy29d" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search" data-ved="0ahUKEwiq_ZvUyJvrAhXhzjgGHXBjBx4Q39UDCAQ">
CSS选择器:
XPath:
此外,所需元素是已启用的元素,因此要在元素内发送字符序列,您必须为elementtobelickable()
诱导WebDriverWait,并且您可以使用以下任一ocator策略:
- 姓名:
- CSS选择器:
- XPath:
您确定页面已加载吗?PageSource返回什么?On是一个等待找到元素的示例。它在google chrome中打开了google search,但这一切都要感谢您的回答。我将代码更改为driver.FindElement(By.Name(“q”)。SendKeys(“gbqfq”)
它确实有效,但当我尝试使用IWebElement resultsPanel=driver.findelelement(By.Id(“search”);
获取搜索结果时,我遇到了相同的错误。
这听起来是一个完全不同的要求。你能为你的新要求提出一个新问题吗?哦,好的,没问题谢谢你的帮助!
<input class="gLFyf gsfi" maxlength="2048" name="q" type="text" jsaction="paste:puy29d" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" autofocus="" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search" data-ved="0ahUKEwiq_ZvUyJvrAhXhzjgGHXBjBx4Q39UDCAQ">
driver.FindElement(By.Name("q")).SendKeys("gbqfq");
driver.FindElement(By.CssSelector("[name='q']")).SendKeys("APPLES");
driver.FindElement(By.XPath("//*[@name='q']")).SendKeys("APPLES");
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("q"))).SendKeys("APPLES");
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("[name='q']"))).SendKeys("APPLES");
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@name='q']"))).SendKeys("APPLES");