1

I have tried a lot to find a solution to a link that has onclick attribute but I could noot find a clue The HTML for the element like that

<a href="javascript:void(0);" onclick="dealSideLinkPage('/lawyerViews/lawOrders/newCase.jsp')">أمر أداء جديد</a>

I tried to use ExecuteScript but got error. I am using selenium in VBA Trying this line doesn't work with me

.ExecuteScript "document.getElementByXPath(""//*[@id='SrvTabs1']/div/div/div[1]/nav/div/ul/li[3]/a"").click()"
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
YasserKhalil
  • 9,138
  • 7
  • 36
  • 95

2 Answers2

3

You can try any of these XPaths -

//a[text()='أمر أداء جديد']

//a[contains(text(),'أمر أداء جديد')]

Note: Onclick and style attributes are not suggested to write XPath.

1

To click() on the element with text as أمر أداء جديد using the onclick attribute you can use either of the following Locator Strategies:

  • Using FindElementByLinkText:

    driver.FindElementByLinkText("أمر أداء جديد").click
    
  • Using FindElementByCss:

    driver.FindElementByCss("a[onclick^='dealSideLinkPage'][onclick*='newCase']").click
    
  • Using FindElementByXPath:

    driver.FindElementByXPath("//a[starts-with(@onclick, 'dealSideLinkPage') and text()='أمر أداء جديد']").click
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I think the problem is related to the position of the element. For the first time in loop, it works well with no problem but in the second loop, the problem occurs `element click intercepted`. so how can I get to the top of the web page each time at the start of the loop? – YasserKhalil Dec 12 '20 at 19:34
  • @YasserKhalil Add some wait and retest please. – undetected Selenium Dec 12 '20 at 19:35
  • No the problem is not in the wait I am sure. When I go manually to the top of the webpage, the line works very well. I have tested that myself more than once. – YasserKhalil Dec 12 '20 at 19:37
  • 1
    How about implementing [ScrollIntoViewCenter()](https://stackoverflow.com/questions/65213243/scroll-element-in-selenium-vba) – undetected Selenium Dec 12 '20 at 19:39
  • I have found a better way which uses ExecuteScript too and this solved the problem `.ExecuteScript "arguments[0].click();", .FindElementByXPath("//a[starts-with(@onclick, 'dealSideLinkPage') and text()='أمر أداء جديد']")`. Thank you very much for the great support. – YasserKhalil Dec 12 '20 at 19:42
  • @YasserKhalil Well :) honestly `ExecuteScript` should be your last option to fall back. – undetected Selenium Dec 12 '20 at 19:43
  • The problem is that the website is heavily javascript and I have no alternative. – YasserKhalil Dec 12 '20 at 19:44