Search code examples
seleniumselenium-idehtmlelements

how to find element using xpath selenium


Here is my html of my element which i am able to find

<span data-bind="text: myAge()" xpath="1">Showing 1 of 25 people</span>

i could find above element using code like

 [FindsBy(How = How.XPath, Using = "//*[@data-bind='text: myAge()']")]

I have another element in Html like which get dynamic ids

<input class="form-control" data-bind="textInput: code, attr: { id: 'myTable_code_' + $index() }" type="text" id="myTable_code_0" >

i tried to find same as above

 [FindsBy(How = How.XPath, Using = "//*[@data-bind='textInput: code']")]

But i get error that "It is not able to find element"

How to fix this?


Solution

  • The data-bind attribute is "textInput: code, attr: { id: 'myTable_code_' + $index() }", you need to use all of it

    [FindsBy(How = How.XPath, Using = "//*[@data-bind='textInput: code, attr: { id: 'myTable_code_' + $index() }']")]
    

    Or partial attribute

    [FindsBy(How = How.XPath, Using = "//*[contains(@data-bind, 'textInput: code')]")]