[英]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:如果驱动程序仍然无法单击,则应考虑以下事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.