简体   繁体   English

无法使用 C# 在 selenium 浏览器中单击

[英]Not able To click in selenium browser with C#

Button type is Input.按钮类型为输入。

<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit btn solid">

I want to click this Button using code, try to click on Button By ClassName.我想使用代码单击此按钮,尝试按类名单击按钮。

My code is as per below but not get any success.我的代码如下所示,但没有成功。

string url1 = "myUrl";
IWebDriver driver = new ChromeDriver(Application.StartupPath);
driver.Navigate().GoToUrl(url1);    
driver.FindElement(By.ClassName("wpcf7-form-control .wpcf7-submit btn solid")).Click();

This throws the following error :这会引发以下错误:

Compound class names not allowed.不允许使用复合类名。 Cannot have whitespace in class name.类名中不能有空格。 Use CSS selectors instead.请改用 CSS 选择器。 and not use single class because there is a other class with same name.并且不使用单个类,因为还有一个同名的类。

@Kishan - I have checked your code and finally, I found the solution @Kishan - 我检查了你的代码,最后,我找到了解决方案

Try below code :试试下面的代码:

driver.FindElement(By.XPath("//input[@type='submit']")).Click();

I would try to avoid using XPath, Instead I would add an Id to your input.我会尽量避免使用 XPath,而是会在您的输入中添加一个 Id。 The reason being XPath is more likely to be changed over time than Id.原因是 XPath 比 Id 更有可能随着时间的推移而改变。 Also if you add another element with the same HTML, selenium will not be able to differentiate between them and will always select the first element.此外,如果您添加另一个具有相同 HTML 的元素,selenium 将无法区分它们并始终选择第一个元素。 Using Id also makes your code more readable.使用 Id 还可以使您的代码更具可读性。 You can try this:你可以试试这个:

<input type="submit" value="Send" Id="YourButtonId" class="wpcf7-form-control wpcf7-submit btn solid">

And

 driver.FindElement(By.Id("YourButtonId")).Click();

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

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