简体   繁体   English

如何使用 Selenium 和 C# 单击登录按钮

[英]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.我正在尝试创建这个简单的测试,您可以前往 URL,输入您的登录凭据,然后单击按钮进行登录。除了单击按钮之外,它正在执行所有操作。 I am trying to doing it by calling up ClassName.我试图通过调用 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:使用 CSS 选择器,如下所示:

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

Each dot represents a class.每个点代表一个 class。

See this page for more info and here is an example from that page:有关更多信息,请参阅页面,以下是该页面的示例:

.name1.name2 .name1.name2

Selects all elements with both name1 and name2 set within its class attribute选择在其 class 属性中设置了 name1 和 name2 的所有元素

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 :要单击SIGN IN按钮,您必须为所需的ElementToBeClickable()诱导WebDriverWait并且您可以使用以下任一定位器策略

  • CssSelector : CssSelector

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

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

Try making use of the button xpath.尝试使用按钮 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. 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 :然后在您的代码FindElement替换为FindElementByXPath

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

Given your shared html block, the following XPath will suffice.鉴于您共享的 html 块,以下 XPath 就足够了。

//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? XPath 是独一无二的吗? 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.将 xpath 粘贴到 chrome 的 devtools 搜索框中的检查元素选项卡中,并确保提供的 xpath 以您想要的元素为目标。 If this is not the case then you should make the xpath more unique.如果不是这种情况,那么您应该使 xpath 更加独特。
  2. Is the element in an iframe? iframe 中的元素是什么? if the element is in an iframe the driver won't be able to locate it by default.如果该元素在 iframe 中,则默认情况下驱动程序将无法找到它。 In such cases you will need to first switch to the iframe and then attempt to locate and interact with the element.在这种情况下,您需要首先切换到 iframe,然后尝试定位该元素并与之交互。
  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.要检查这些属性,首先找到元素并存储在单独的变量中,然后检查所述属性是否为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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