[英]Selenium WebDriver (node js/ C#)-Element not visible/No such element exception while trying to send value to a search box
Using Selenium WebDriver, trying to test the search feature on website https://www.healthdirect.gov.au/ . 使用Selenium WebDriver,尝试在https://www.healthdirect.gov.au/网站上测试搜索功能。 I get "no such element/element not visible" exception everytime, same code works for other searches like google. 我每次都会收到“没有这样的元素/元素不可见”异常,相同的代码可用于其他搜索(例如Google)。
Javascript(Node js) snippet: Javascript(Node js)代码段:
driver.get('https://www.healthdirect.gov.au/');
var search = driver.findElement(By.css('#header-search'));//tried with id,name,xpath elements and got same exception
search.sendKeys('fever');
C# snippet(with added wait): C#代码段(添加了等待):
string searchEngine = "https://www.healthdirect.gov.au/";
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(searchEngine);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(25));
wait.Until(ExpectedConditions.ElementExists(By.Id("header-search")));
driver.FindElement(By.Id("header-search")).SendKeys("fever");
Code from the website to find elements: 从网站上的代码中查找元素:
<ul class="dropdown-menu hda-head_menu-row-tablet-search-list" role="listbox" position="position">
</ul>
Whenever such error occurs, make sure there not multiple matches of your object. 每当发生此类错误时,请确保您的对象没有多个匹配项。 I just checked Firebug and you can see there are 3 matching nodes and the one your are interested in is the second one 我刚刚检查了Firebug,您可以看到有3个匹配的节点,而您感兴趣的是第二个
Expected solution is to loop through the elements 预期的解决方案是遍历元素
var elems = driver.FindElement(By.Css("#header-search"))
Loop through them and check IsDisplayed
and click the one which is displayed. 遍历它们并检查IsDisplayed
,然后单击显示的一个。 Because they will change based on Mobile or Desktop resolution 因为它们会根据移动或桌面分辨率而变化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.