简体   繁体   中英

How to click on the SIGN IN button using Selenium and C#

I am trying to create this simple test where you head to the URL, enter your login credentials and then click the button to sign in. It is doing everything, except for clicking the button. I am trying to doing it by calling up ClassName. Can anyone look at my test and see what I am doing wrong?

public void test_search()
{
    var driver2 = new ChromeDriver(@"C:\Users\MyName\Desktop\NUnitTestProject1\NUnitTestProject1\bin\Debug\netcoreapp2.1");
    driver2.Navigate().GoToUrl("https://portal.crushdata.com/");
    driver2.FindElement(By.Name("Email")).SendKeys("email@email.com");
    driver2.FindElement(By.Name("Password")).SendKeys("Password");
    driver2.FindElement(By.ClassName("btn bg-teal btn-block btn-lg waves-effect")).Click();
}

This is my classname for my button.

在此处输入图像描述

Use CSS selector as shown below:

By.ClassName("btn.bg-teal.btn-block.btn-lg.waves-effect")

Each dot represents a class.

See this page for more info and here is an example from that page:

.name1.name2

Selects all elements with both name1 and name2 set within its class attribute

To click on the SIGN IN button you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies :

  • CssSelector :

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("button.btn.bg-teal.btn-block.btn-lg.waves-effect"))).Click();
  • XPath :

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[text()='SIGN IN']"))).Click();

Try making use of the button xpath. Open the dev tools. Right click on the button you want to be clicked > Select Inspect >Then right click the html in the dev tools window and Copy Xpath from the Copy option.

Then in you code replace FindElement with FindElementByXPath :

driver2.FindElementByXPath("//*xpath/goes/here")).Click();

Given your shared html block, the following XPath will suffice.

//div[contains(@class = "text-center")]//button[contains(@class, 'btn bg-teal btn-block btn-lg waves-effect') and @type = 'submit']

If the driver is still unable to click you should consider the following:

  1. Is the XPath unique? paste the xpath in chrome's devtools search box in the inspect element tab and make sure the provided xpath is targeting the element you are intending. If this is not the case then you should make the xpath more unique.
  2. Is the element in an iframe? if the element is in an iframe the driver won't be able to locate it by default. In such cases you will need to first switch to the iframe and then attempt to locate and interact with the element.
  3. Is the element clickable, visible and enabled? To check these properties first find the element and store in a separate variable and then check the said properties are true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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