-1

I have this HTML code and I want to click the button:

<div class="col-md-3 col-sm-5">
                <button type="button" class="btn btn-success btn-lg" data-toggle="modal" data-target="#SignInModal" style="width:100%">
                    Sign in            
                </button>
            </div>

this is what I tried:

driver.FindElement(By.ClassName("btn-success")).Click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Daniel
  • 31
  • 3
  • So, *does* it click? If not, is there an error or an Exception being thrown? – haim770 Oct 08 '18 at 19:48
  • 2
    Welcome to StackOverflow. Your code looks good to me, but frequently problems like this arise due to timing. For example, maybe the page hasn't loaded or rendered yet. You may need to add a Wait. See https://stackoverflow.com/q/6992993/120955 for details. – StriplingWarrior Oct 08 '18 at 19:49
  • 1
    You only tried once? Did you do any research on other ways to click that element? Did you try any of those? What happened when you tried this? When you used that method, did you got an exception or what didn't work? Please add any error message to your question and properly format it. – JeffC Oct 08 '18 at 22:24
  • @Danied use XPath instead of the class name. Use this link as a reference and understand how to find locators. https://www.guru99.com/xpath-selenium.html – Dhru 'soni Oct 09 '18 at 05:06

1 Answers1

0

To invoke click() on the button with text as Sign in you can use either of the following solutions:

  • CssSelector:

    driver.FindElement(By.CssSelector("button.btn.btn-success.btn-lg[data-toggle='modal'][data-target$='SignInModal']")).Click();
    
  • XPath:

    driver.FindElement(By.XPath("//button[@class='btn btn-success btn-lg'][normalize-space()='Sign in']")).Click();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    You don't have to use every attribute on the element to find it. Others have mentioned this many times. Doing this just makes it more brittle, not better at finding the element. – JeffC Oct 08 '18 at 22:21