Question

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.

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.

 //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.

Anyone able to provide any assistance with why my wait will not pause?

Was it helpful?

Solution

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));

OTHER TIPS

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. I can't seem to reference anything whether by CSS, XPath or naming. 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. 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.

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().

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?

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top