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
  • 34,719
  • 24
  • 185
  • 251

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
  • 1,948
  • 2
  • 11
  • 26
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
  • 151,581
  • 34
  • 225
  • 281
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