我正在学习 selenium ,但似乎无法单击它的Google搜索按钮。我正在尝试:
IWebDriver driver = new ChromeDriver();
driver.Url = "https://google.com";
IWebElement searchBar = driver.FindElement(By.Name("q"));
searchBar.SendKeys("Hello world!");
IWebElement searchButton = driver.FindElement(By.Name("btnK"));
searchButton.Click();
它正确地用hello world填充了搜索栏,但是单击按钮时出现了异常:OpenQA.Selenium.ElementNotInteractableException:'元素不可交互
在学习过程中,我认为这是一个很好的练习,您可以在其中使用 findElements
列表项然后与之交互。
我可以给您这个主意,然后您必须自行实现,这将为您提供更多信息,而不仅仅是获得答案。
您必须使用findElements
,findElement
因为有2个元素具有相同的确切name
属性值。
driver.FindElements(By.Name("btnK"))
这将为您提供元素列表。然后检查大小为2。现在单击第二个元素。
或者,您可以只使用JavaScript单击searchButton
原始帖子中的。
谢谢,这对我有用。只需单击最后一个元素。var searchButton = driver.FindElements(By.Name(“ btnK”)); searchButton.Last()。Click();
您可以对此表示感谢,也可以帮助将来的访问者选择哪个答案有效。