简体   繁体   English

Selenium WebDriver(节点js / C#)-元素不可见/尝试将值发送到搜索框时没有此类元素异常

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

相关问题 Selenium :: WebDriver :: Error :: ElementNotVisibleError:使用Chrome浏览器时调用send_key函数时元素不可见 - Selenium::WebDriver::Error::ElementNotVisibleError: element not visible while calling send_key function when Chrome browser is used Selenium Webdriver:元素当前不可见 - Selenium Webdriver: Element is not currently visible Selenium-webdriver JS - 如何等待元素可见 - Selenium-webdriver JS - how to wait until an element is visible 如何使元素在Selenium WebDriver中可见? - How to make element visible in selenium webdriver? OpenQA.Selenium.ElementNotVisibleException:元素不可见C#硒 - OpenQA.Selenium.ElementNotVisibleException : element not visible c# selenium 如何使用Selenium WebDriver,NUnit和C#获取元素属性的子属性值 - How to get child property value of a element property using selenium webdriver, NUnit and C# selenium-webdriver node.js:等待超时后如何处理缺少的元素(似乎无法捕获到异常) - selenium-webdriver node.js: how to handle missing element after wait timeout (exception can't seem to be caught) Selenium Python滑块bouton元素不可见异常 - Selenium python slider bouton Element Not Visible Exception 硒元素在已触发区域中不可见异常 - Selenium Element is Not Visible Exception on Already Triggered Area 在Selenium Webdriver ruby​​脚本中获取元素值 - Get element value in Selenium webdriver ruby script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM