-1

I need help getting the xpath or link from this html

<div class="g-btn g-hyperlink " id="s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1"><a onmousemove="window.status='';" onmouseout="window.status='';" name="" href="javascript:;" id="s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId" onfocus="DCT.Util.setFocusField('s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId');" onclick="DCT.Util.customOnClick(this,&quot;&quot;,{InsertTextAtCursorAction:0,showReminderMessages:0,skipScrollToTop:0});DCT.Util.setFocusField('s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId');DCT.Util.processInterviewButtonAnchor('a_s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_div',&quot;&quot;,&quot;&quot;);" onblur=""><div class="g-btn-l"></div><div class="g-btn-m"><span class="g-btn-text" ext:qtip="" data-tip="" fieldref="Done" xmlns:ext="urn:ext">Cancel</span></div><div class="g-btn-r"></div></a></div>

I'm trying to click in a link but I'm having a hard time getting the xpath .

I have try this

Driver.FindElement(By.XPath(@"onclick,s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId")).Click();
    }

and this

 {Driver.FindElement(By.LinkText("Done")).Click();}

When I do this { Driver.FindElement(By.XPath(@"onclick,s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId")).Click();

I get this error

Unable to locate an element with the xpath expression onclick,s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string 'onclick,s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId' is not a valid 
XPath expression.

When I try this

{

     Driver.FindElement(By.LinkText("Done")).Click();}

I get this error

OpenQA.Selenium.ElementClickInterceptedException: element click intercepted: Element <a onmousemove="window.status='';" onmouseout="window.status='';" name="" href="javascript:;" id="sF6195287351B43A7BC7610F2A63626FA263_2_1_anchorId" onfocus="DCT.Util.setFocusField('sF6195287351B43A7BC7610F2A63626FA263_2_1_anchorId');" onclick="DCT.Util.customOnClick(this,&quot;&quot;,{InsertTextAtCursorAction:0,showReminderMessages:0,skipScrollToTop:0});DCT.Util.setFocusField('sF6195287351B43A7BC7610F2A63626FA263_2_1_anchorId');DCT.Util.processInterviewButtonAnchor('a_sF6195287351B43A7BC7610F2A63626FA263_2_1_div',&quot;&quot;,&quot;&quot;);" onblur="">...</a> is not clickable at point (310, 605). Other element would receive the click: <div id="s_p900F519A82CE4AE6BA9F48EAF71BBF95144_3_1" class="downLayout x_ statusBar">...</div>

Mayi007
  • 1
  • 3

3 Answers3

1

UPDATE

Added WebDriverWait

You can try following xpath:

//*[@class='g-btn-text' and text()='Cancel']
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
IWebElement elmt = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@class='g-btn-text' and text()='Cancel']"));
elmt.Click();
frianH
  • 7,295
  • 6
  • 20
  • 45
1

Use below Xpath:

//div[@class='g-btn g-hyperlink ']//span[@class='g-btn-text' and contains(.,'Cancel')]

So your code will be like:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement button = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='g-btn g-hyperlink ']//span[@class='g-btn-text' and contains(.,'Cancel')]"));
button.Click();
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • 1
    Seem to be a dynamic element and you may have to induce a waiter – undetected Selenium Aug 29 '19 at 08:16
  • I try this but I'm getting this error OpenQA.Selenium.ElementClickInterceptedException: element click intercepted: Element Cancel is not clickable at point (253, 605). Other element would receive the click:
    ...
    (Session info: chrome=76.0.3809.100)
    – Mayi007 Aug 29 '19 at 12:42
  • Mayi thats a totally different issue, thats means now your xpath issue is resolve and for this issue you can refer DebanjanB answer as : https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-x-y-other-elem – Shubham Jain Aug 29 '19 at 13:15
  • if nothing work use javascript executor .. refer : https://stackoverflow.com/questions/15231955/selenium-webdriver-how-to-deal-with-javascript-onclick-in-c-sharp – Shubham Jain Aug 29 '19 at 13:16
0

Your locator is not correct, if you really want to use the onclick attribute you should amend it to

  • mention <div> tag
  • use XPath contains() function:

    Driver.FindElement(By.XPath("//div[contains(@onclick,'s99AB6D3E3FBF404BA01A344322BFA0EE263_2_1_anchorId')]")).Click();
    

More information:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133