隐式等待命令不起作用-Selenium WebDriver C# [英] Implicit wait Command Not Working-selenium webdriver C#

查看:674
本文介绍了隐式等待命令不起作用-Selenium WebDriver C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我已经开始研究硒Web驱动程序.您可以假设我是一个初学者.目前,在我的代码(C#)中实现隐式等待命令时遇到了困难.它无法正常运行,并且由于找不到元素而导致异常,但是当我添加"Thread.Sleep(3000)时,代码可以完美执行.我一直在互联网上寻找解决方案,但无法解决问题.下面我提到了示例代码.

Guys, I have started to work on selenium web driver. You can assume I am a beginner. At the moment I am having difficulties in implementing the implicit wait command in my code (C#). it is not working as it should and result in an exception due to Element not found, however when I add the "Thread.Sleep(3000) the code get executed flawlessly. I have been looking for the solution all over the internet but not able to resolve the problem. Below I have mentioned sample code.

class Entrypoint
{

static void Main()

{
 IWebDriver driver = new ChromeDriver();
    **driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);**
    driver.Navigate().GoToUrl("https://r1.netrevelation.com:8443/mcba-cms/pages/flight-transfer.cab");
    driver.Manage().Window.Maximize();


    driver.FindElement(By.Id("loginlink")).Click();
    driver.FindElement(By.Id("headerSubView:inputUserName:input")).SendKeys("st001");
    driver.FindElement(By.Id("headerSubView:inputPassword:input")).SendKeys("hello321" + Keys.Enter);

    driver.FindElement(By.Id("dateOfFlight:input")).Click();**//This Step does not get Executed , it throws exception element not found.**
    driver.FindElement(By.Id("ui-datepicker-div")).Click(); 
    driver.FindElement(By.XPath(".//*[@id='ui-datepicker-div']/div/a[2]/span")).Click(); 
    driver.FindElement(By.LinkText("28")).Click(); 
    IWebElement Flightno = driver.FindElement(By.Id("selectedFlight:input"));
    Flightno.SendKeys("BA901" + Keys.Enter);
    IWebElement Flighttick = driver.FindElement(By.Id("flightTickImg"));


    driver.Quit();

请注意,目前我不想使用显式等待,因为隐式将满足我的需要(如果它开始工作).上面的代码以某种超音速运行,以某种方式设法成功登录到系统,但之后每次失败,都是因为一旦发出登录请求,系统就会暂停2-3秒.请提供您对此的评论.

Please note that at the moment I don't want to use explicit wait, as implicit will serve my need (if it starts working). The above code Run in supersonic speed for somehow it manages to Login into the system but afterward it Fails every time reason being once Login request is made system pauses for 2-3 seconds. Please provide your comment in this regard.

推荐答案

根据文档隐含等待是要告诉 WebDriver 在尝试 HTML DOM >或find all elements()(如果它们不立即可用).但是 DOM树中元素的可用性不能保证

As per the documentation, an Implicit Wait is to tell the WebDriver to poll the HTML DOM for a certain amount of time when trying to find an element() or find all elements() if they are not immediately available. But availability of an element in the DOM Tree doesn't guarantees that the ElementToBeClickable as you have tried in your code block. Hence you face the exception as Element not found.

因此,解决您的问题的方法是诱使 ExpectedConditions 子句为 ElementToBeClickable 不仅会确认可用性 HTML DOM 中元素的元素,但还要确保 Element是Clickable 的,即 Element可以显示并启用,如下所示:

So the solution to your issue is to induce Explicit Wait i.e. WebDriverWait with ExpectedConditions clause as ElementToBeClickable which will not only confirm the availability of an element in the HTML DOM but also ensure that the Element is Clickable i.e. Element is Displayed and Enabled as follows:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("loginlink")));

这篇关于隐式等待命令不起作用-Selenium WebDriver C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆