Brandon
Brandon

Reputation: 645

Not sure how to select this element

I am currently working on automating a prototype website, and am having a devil of a time finding this element.

<div id="cart-number">
   <span>3</span>
</div>
<i class="step fi-shopping-cart size-30"></i> 

When I inspect it, I can obviously see it in the markup. When I try to interact with it in my coded test however, it cannot find it.

[Test]
public void ClickOnShoppingCart()
{
   try
   {
      driver.Navigate().GoToUrl(@"http://m.vdevint4.costco.com/");
      IWebElement shoppingCart = driver.FindElement(By.CssSelector(@"i.step.fi-shopping-cart.size-30"));
      shoppingCart.Click();
      driver.Close();
   }

   catch (Exception exc)
   {
      Console.SetWindowSize(200, 88);
      Console.WriteLine(exc.Message);
      Console.ReadKey();
   }
}

At this point, I have tried By.CssSelector(), By.Id() and By.XPath(). So, my questions are, what am I missing? Is there a better way to go about this? I did try researching online prior to asking but, my GoogleFu is weak today.

EDIT: I should also note, that this isn't the only element I am having this issue with. I am just presuming that an answer to this question would also work for the other two (since they follow the same form).

Upvotes: 1

Views: 85

Answers (2)

alb-i986
alb-i986

Reputation: 345

I think the problem is that the i element has width and height = 0, therefore it is not clickable.

The javadocs for WebElement#click say:

There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0.

http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebElement.html#click()

I guess it's a mistake that C# docs don't say that too, but I'm not sure. If you find that this is why you can't click the element, then it must be a mistake.

In order to verify if this is the case, you may try to write something in the 'i' element, e.g.

<i class="step fi-shopping-cart size-30">asd</i>

If you can now click the element, then that's it.

Btw, are you sure you need to click the 'i' element? It's weird to me. You typically have a 'button', or 'input' or 'a' element; not an empty 'i'.

Another thing you may try is using different browsers. What have you been using so far?

Upvotes: 0

briba
briba

Reputation: 2987

You can wait a little for the element:

new WebDriverWait(driver, TimeSpan.FromMinutes(1)).Until(d => d.FindElements(By.XPath("//i[contains(@class, 'fi-shopping-cart')]")).Any());

And then, search for it:

var btn = driver.FindElement(By.XPath("//i[contains(@class, 'fi-shopping-cart')]"));

if (btn != null)
{
    btn.Click();
}

XPath explanation: http://www.w3schools.com/XPath/xpath_syntax.asp

Upvotes: 1

Related Questions