5
  <a href="javascript:;" class="btn green request-action-btn" data-request-status="Approved" data-id="212"> Approve </a>

How can I get this element in selenium C#. I tried by:href ,Xpath, ID, class name, text name but I couldn't get this element

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

4

you can click by Link text

driver.FindElement(By.LinkText("Approve")).Click();

or by CSS Selector

.btn.green.request-action-btn
Prany
  • 2,078
  • 2
  • 13
  • 31
1

To identify the element you can use the following Locator Strategy:

  • CssSelector:

    driver.FindElement(By.CssSelector("a.btn.green.request-action-btn[data-request-status='Approved']"))
    
  • XPath:

    driver.FindElement(By.XPath("//a[@class='btn green request-action-btn' and @data-request-status='Approved']"))
    
  • XPath using contains():

    driver.FindElement(By.XPath("//a[@class='btn green request-action-btn'][contains(.,'Approve')]"))
    
  • XPath using normalize-space():

    driver.FindElement(By.XPath("//a[@class='btn green request-action-btn'][normalize-space()='Approve']"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

It can be found by using tagName as below:

ReadOnlyCollection<IWebElement> anchrTags= driver.FindElements(By.TagName("a"));
IWebElement roWebElement = anchrTags.Where(x => x.Text == "Approve").FirstOrDefault();

Otherwise if the anchor tag exists in the frames, it will not found. try switching to that frame:

IWebElement iframe = driver.FindElements(By.Id("iframeId"));
driver.SwitchTo().Frame(iframe);
IWebElement roWebElement = anchrTags.Where(x => x.Text == "Approve").FirstOrDefault();
Sankar G
  • 1
  • 1