简体   繁体   English

Selenium / C#WebDriverWait不等待

[英]Selenium / C# WebDriverWait Not Waiting

I have a website with a jQuery slide down at the top with a list of all the languages I want to use: http://testing.bestshippers.com/net/index.aspx , the slider is "language selection" button at the top. 我有一个网站,上面有一个jQuery幻灯片,其顶部是我想使用的所有语言的列表: http : //testing.bestshippers.com/net/index.aspx ,滑块是“语言选择”按钮最佳。

I can click it, but I get and error when trying to click on the elements inside of the slide down. 我可以单击它,但是在单击幻灯片中的元素时出现错误。 I believe it is because I need to pause for a few seconds before I try to select them. 我相信这是因为在尝试选择它们之前,我需要暂停几秒钟。 Could be wrong? 可能是错的吗? I'm relatively new, but I have read all kinds of things about the WebDriverWait and changing focus. 我相对较新,但是我已经阅读了有关WebDriverWait和改变焦点的各种内容。

 //check spanish
 driver.FindElement(By.XPath("//*[@id='openCloseWrap']/img")).Click();
 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
 driver.FindElement(By.Id("ButtonSPFlag")).Click();

 String check = driver.FindElement(By.XPath("html/body/form/div[5]/div/div[1]/div/p")).Text;
 Console.Out.WriteLine(check);

The above code clicks on the openCloseWrap (Language Selection Button) and then I am trying to pause for seconds (100) and then trying to click on the SP flag to change the language. 上面的代码单击了openCloseWrap(语言选择按钮),然后我试图暂停几秒钟(100),然后尝试单击SP标志来更改语言。

Anyone able to provide any assistance with why my wait will not pause? 任何人都可以提供帮助,为什么我的等待不会暂停?

You are initiating the Wait, but not waiting for anything. 您正在启动等待,但不等待任何东西。 You probably want to do something similar to: 您可能想做类似的事情:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100));
//    wait.Until(By.Id("ButtonSPFlag"));
IWebElement element = wait.Until(driver => driver.FindElement(By.Id("ButtonSPFlag")));
element.Click();

Alternatively you can set implicit wait, but I would go with the first one. 另外,您可以设置隐式等待,但是我会选择第一个。

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

I would upvote you both, but I don't have enough rep points yet. 我都赞成你们,但我的代表点还不够。

I worked on this for 4 days, issue seems to be with the way that the language bar is displayed. 我为此工作了4天,问题似乎与语言栏的显示方式有关。 I can't seem to reference anything whether by CSS, XPath or naming. 我似乎无法通过CSS,XPath或命名来引用任何内容。 I'm going to keep moving on my project for now. 我现在将继续进行我的项目。 I can check to make sure it exists, and that passes (see below) but the actual interaction fails for some reason. 我可以检查以确保它存在,并且可以通过(请参阅下文),但是实际的交互由于某种原因而失败。 I am going to keep working and I will revisit. 我将继续工作,并且我将重新审视。 Thank you both for your time and effort! 感谢您的时间和精力!

DoesElementExistX(driver, sw, "//*[@id='openCloseWrap']/img");
DoesElementExistX(driver, sw, "//*[@id='ButtonUSFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtoFRFlag']");
DoesElementExistX(driver, sw, "//*[@id='ImageButton1']");
DoesElementExistX(driver, sw, "//*[@id='ButtonDEFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtonITFlag']");
DoesElementExistX(driver, sw, "//*[@id='ButtonSPFlag']");

    #region[DoesElementExistX]
    public static void DoesElementExistX(IWebDriver driver, StreamWriter sw, String id)
    {
        var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(2));
        try
        {
            wait.Until(ExpectedConditions.ElementExists(By.XPath(id)));
        }
        catch (WebDriverTimeoutException)
        {
            sw.WriteLine("FAILED - " + id);
        }
    }
    #endregion

I looked into this some more. 我进一步研究了这一点。

You're trying to click on the Language element by id . 您尝试通过id单击Language元素。 This is a problem, because Selenium will click in the center of the element (unless otherwise told differently), and the center of the element is to the left of the Language area. 这是一个问题,因为Selenium将单击元素的中心(除非另有说明),并且元素的中心位于“语言”区域的左侧。

I think this will accomplish what you need: 我认为这将满足您的需求:

driver.FindElement(By.CssSelector(".topMenuAction")).Click();
driver.FindElement(By.Id("ButtonSPFlag")).Click();
String Check = driver.FindElement(By.CssSelector("#loginBoxInner>p")).Text;

You may need to add a Sleep() , or use WebDriverWait() . 您可能需要添加Sleep()或使用WebDriverWait()

Could it be that the language selection bar is always in the DOM, and that when you click "expand" it is simply showing the html rather than creating or injecting it? 可能是语言选择栏始终位于DOM中,并且当您单击“展开”时,它只是显示html而不是创建或注入html?

If so, then your "wait" will return straight away because you are saying "wait until the element" is in the HTML, and in your case, it is always there. 如果是这样,那么您的“等待”将立即返回,因为您说的是“等待直到元素”在HTML中,并且在您的情况下,它始终存在。

You need to make your waiting smarter such as "wait until displayed" or perhaps even "wait until displayed and stationary" as you also need to wait for the animation to finish 您需要使等待更加智能,例如“等待直到显示”,甚至可能是“等待直到显示并静止”,因为您还需要等待动画完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM