简体   繁体   中英

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. 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

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. The reason being XPath is more likely to be changed over time than 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. Using Id also makes your code more readable. 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();

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