1

Is there a possibility to find an element by ID with an wildcard? I have something like this:

stat = GC.FindElementByXPath("//*[@id='C29_W88_V90_V94_admin_status']").Value
stat = GC.FindElementByXPath("//*[@id='C26_W88_V90_V94_admin_status']").Value
stat = GC.FindElementByXPath("//*[@id='C29_W88_V12_V94_admin_status']").Value

The admin_status part won't change but the values before. Sometimes I have the same problem with another element with values around it. So the best thing would be to find the element with some wildcard like this:

stat = GC.FindElementByXPath("//*[@id='*admin_status*']").Value
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
protter
  • 119
  • 7

2 Answers2

1

As the value of the id attribute contains dynamic values, you need to counstruct dynamic locator strategies as follows:

  • Using FindElementByCss and contains clause:

    stat = GC.FindElementByCss("[id*='admin_status']").Value
    
  • Using FindElementByCss and ends with clause:

    stat = GC.FindElementByCss("//*[id$='admin_status']").Value
    
  • Using FindElementByXPath and contains clause::

    stat = GC.FindElementByXPath("//*[contains(@id, 'admin_status')]").Value
    

References

You can find a couple of relevant detailed discussions related to Dynamic Locator Strategies in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

This code is tested in Excel VBA using Selenium.

Option Explicit
Sub sbXPathContains()
    Dim driver As ChromeDriver
    Set driver = New ChromeDriver
    Dim sURL As String
    sURL = "https://davetallett26.github.io/table.html"
    Call driver.Start("edge")
    driver.get (sURL)
    driver.Window.Maximize
    sbDelay (100000)
    MsgBox driver.FindElementByXPath("//*[contains(@id, 'ctl03_txtCash')]").Attribute("outerHTML")
    '                                 //* = any element  contains  @id = within id  'ctl03_txtCash' = string to find   .Attribute("outerHTML") = return HTML of the element
    sbDelay (100000)
    driver.Quit
End Sub

Sub sbDelay(delay As Long): Dim i As Long: For i = 1 To delay:  DoEvents: Next i: End Sub
user10186832
  • 423
  • 1
  • 9
  • 17