C # Selenium, how can I use click button without id

C # Selenium, how can I implement click button without id

Here is the html:

<div class="fe-margin"> <button class="btn btn-default" data-bind="click: $root.addParameter, enable: $root.selectedParameter() == null" type="button"/> Add parameter button </div> 
+10
source share
8 answers

Yes you can do. Using the xpath class name, you can perform the click action.

Example:

 driver.FindElement(By.ClassName("AddContentBTN")).Click(); 
+11
source

Selenium offers various search options to find an item. Check the documentation for more details.

These are commonly used search options.

  • Class Name
  • CSSSelector
  • I'd
  • Linktext
  • title
  • PartialLinkText
  • Tag name
  • XPath

Usage:

 var element = driver.FindElement(By.ClassName("btn-default")); // works for your case. var element = driver.FindElement(By.Id("closeButton")); var element = driver.FindElement(By.XPath("//*[@class='menu_bg']/ul/li[3]")); var element = driver.FindElement(By.CssSelector("#import>a")); 

As soon as you get an element using any of the selectors, you just need to Click on it.

 element.Click(); 
+6
source

You can click on buttons that are not displayed using JavaScript

 IJavaScriptExecutor executor = driver as IJavaScriptExecutor; executor.ExecuteScript("arguments[0].click();", ElementToClick) 
+2
source

You can do this with ClassName with one of the classes

 driver.FindElement(By.ClassName("btn-default")).Click(); // or driver.FindElement(By.ClassName("btn")).Click(); 

Or via CssSelector with both classes

 driver.FindElement(By.CssSelector(".btn.btn-default")).Click(); 

Edit

Error

The item is not available for click at a point.

means that Selenium cannot see the button, so it cannot click on it. First you need to scroll it

 IWebElement button = driver.FindElement(By.ClassName("btn-default")); // locate the button, can be done with any other selector Actions action = new Actions(driver); action.MoveToElement(button).Perform(); // move to the button button.Click(); 
0
source

Use this XPath to find your button in the text:

 "//div[normalize-space(.)='Add parameter button']/button" 
0
source

The answers presented here offer most of the ways to do this work. That is, according to the selection classes, Selenium also provides β€œmanually” JavaScript. Sometimes I came across strange implementations of buttons and menus where none of these methods worked for me. What I did then was move the cursor over the button and make a free click. Selenium provides a way to do this:

 Actions action = new Actions(Driver); action.MoveToElement(/IWebElement here/).Perform(); action.Click(); or action.SendKeys(OpenQA.Selenium.Keys.Enter).Perform(); 

If this fails, my last game to play is an image recognition library, for example, some of the packages with the Ciku shell on github wrapped in C #.

0
source

IWebElement btn = Firefox.FindElement(By.ClassName("btn")); btn.Click();

That should do it. I found that using the ClassName part does its job.

0
source

Try this Xpath -

 var element = driver.FindElement(By.XPath("//button[@class ='btn btn-default']")); element.Click(); 

thanks

0
source

Source: https://habr.com/ru/post/1245605/


All Articles