0

Here is a button on the page:

<button data-purpose="add-section-btn" type="button" 
        class="ellipsis btn btn-default btn-block">
   <span class="a3 udi udi-plus-square"></span>
   <!-- react-text: 255 --> <!-- /react-text -->
   <!-- react-text: 256 -->Add Section<!-- /react-text -->
</button>

When I try to find it using the following code:

var btns = _driver.FindElements(By.TagName("button"));
var sectionTitle = btns.Where(x => x.GetAttribute("data-purpose") == "add-section-btn");

It returns null.

If I try the following XPath:

var btn = _driver.FindElement(By.XPath("//button[data-purpose=\"add-section-btn\"]"));

then I get an exception.

How to find such a button?

yong
  • 13,357
  • 1
  • 16
  • 27
EngineerSpock
  • 2,575
  • 4
  • 35
  • 57

2 Answers2

0

Try with,

var btn = _driver.FindElement(By.XPath("//button[@data-purpose='add-section-btn']"));
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
0

As per the HTML you have shared to find the element with text as Add Section as the element is a React Element you have to induce WebDriverwait for the element to be visible and you can use either of the following Locator Strategies:

  • CssSelector:

    var btn = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("button.ellipsis.btn.btn-default.btn-block[data-purpose='add-section-btn']")));
    
  • XPath:

    var btn = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//button[@class='ellipsis btn btn-default btn-block' and @data-purpose='add-section-btn'][normalize-space()='Add Section']")));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352