Locating a web element



  • I am having a bit of trouble locating a particular button in one of the ecommerce applications I am writing automation for. Here is a snippet of the mark-up:

    <button class="btn btn-primary btn-sm" style="margin-right:0" data-href="/LABaaS/LABaaSShoppingCart" data-toggle="modal" data-target="#confirm-delete" id="btnAddtoCart">
        <span class="glyphicon glyphicon-shopping-cart"></span> Add to Cart
    </button>
    

    As you can see, it has an id right there in the mark-up so, I tried:

    [FindsBy(How = How.Id, Using = "btnAddtoCart"), CacheLookup]
    public IWebElement btnLaBaaSStorageDetailsAddToCart { get; set; }
    

    That didn't work so, I tried the CssSelector:

    [FindsBy(How = How.CssSelector, Using = "button#btnAddtoCart.btn.btn-primary.btn-sm"), CacheLookup]
    public IWebElement btnLaBaaSStorageDetailsAddToCart { get; set; }
    

    That also failed so, I gave XPath its requisite turn:

    [FindsBy(How = How.XPath, Using = "//*[@id=\"btnAddtoCart\"]"), CacheLookup]
    public IWebElement btnLaBaaSStorageDetailsAddToCart { get; set; }
    

    If you guessed that this approach failed too, you win (something?)! So, as a last-ditch effort, I bravely attempted to use the ClassName:

    [FindsBy(How = How.ClassName, Using = "btn btn-primary btn-sm"), CacheLookup]
    public IWebElement btnLaBaaSStorageDetailsAddToCart { get; set; }
    

    I am well and truly stumped now. I am guessing there is a way to find it via some custom XPath but, my knowledge is limited on that subject. Does anyone have any suggestions?



  • Note: I am adding this as an answer since I don't have enough rep to add a comment.

    So, how do you say that none of these locators worked? Did you get any error message when executing this code or while trying to interact with the located Element? If yes, please provide the error message as well.

    I'm new to C# port of selenium but the locators work just fine. Here's a JSFiddle of the HTML Code you provided. Sample HTML for Selenium locator

    The ID locator itself works. Tried others too and that too works. enter image description here



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2