Search code examples
vbaselenium-webdriverxpathcss-selectorswebdriver

Find element and click using VBA Selenium


I was trying to select the highlighted Xpath in this Photo below. Afterwards I was trying to click on the element.

Code trials:

On Error Resume Next
Set Element = GC.FindElementByXPath("//*a[@data-id='_VIEW--ALL_COMPLAINTS_VIEW_P']")
Set Element1 = GC.FindElementByXPath("//*/div/a[contains(@data-id,'_VIEW--ALL_COMPLAINTS_VIEW_P')]]")
test = Element1.Value
Element.Click
Element1.Click
Set Element3 = GC.FindElementByCss("a[data-id='_VIEW--OPEN_BY_CODE_P']")
Element3.Click
GC.FindElementById("_VIEW--OPEN_BY_CODE_P").Click

But on both tryouts I wasn't able to do it. First Element is Empty second Element not.

The element is at the bottom and is the blue highlighted line.

enter image description here

PS: I hoped there is some unknown thing like an Iframe which you can solve by switch to. Perhaps this "<!--->".

HTML Snapshot:

enter image description here


Solution

  • You were close enough. Given the snapshot of the HTML there is no iframe. The element:

    VIEW--OPEN_BY_CODE_P

    is within an <a> tag with data-id attribute as _VIEW--ALL_COMPLAINTS_VIEW_P and text as All Complaints by Number


    Solution

    To locate the element you can use either of the following locator strategies:

    • Using FindElementByCss:

      Set Element = GC.FindElementByCss("a[data-id='_VIEW--ALL_COMPLAINTS_VIEW_P']")
      
    • Using FindElementByXPath and data-id attribute:

      Set Element = GC.FindElementByXPath("//a[@data-id='_VIEW--ALL_COMPLAINTS_VIEW_P']")
      
    • Using FindElementByXPath and the text All Complaints by Number:

      Set Element = GC.FindElementByXPath("//a[text()='All Complaints by Number']")
      
    • Using FindElementByXPath data-id attribute and the text All Complaints by Number:

      Set Element = GC.FindElementByXPath("//a[@data-id='_VIEW--ALL_COMPLAINTS_VIEW_P' and text()='All Complaints by Number']")