Anuttam Adhwaryu
Anuttam Adhwaryu

Reputation: 65

How to uniquely identify xpath for multiple tags and multiple values

I have following html:

<div class=‘content active’>
    <div>
        <div class=‘var’>
            <div class=‘field var-field’>
                <label>Interface Name</label>
                <div class=‘ui input’>
                    <input type=‘input’ placeholder=‘.*’ value> ==$0
                </div>
            </div>
        </div>
    </div>
    <div>
        <div class=‘var’>
            <div class=‘field var-field’>
                <label>Neighbor Id</label>
                <div class=‘ui input’>
                    <input type=‘input’ placeholder=‘.*’ value> ==$0
                </div>
            </div>
        </div>
    </div>
</div>

I need to send text to the text box with label: Interface Name. Is there a way to uniquely write the xpath to send the text to the textbox. Note that the only way to identify uniquely is wrt the label. The other fields in the tag is same for both. I tried using AND operator. No luck. Please help me out here.

Upvotes: 0

Views: 725

Answers (3)

Ankur Singh
Ankur Singh

Reputation: 1279

You can use this XPATH :- //*[text()='Interface Name']/following-sibling::div/input"

Upvotes: -1

undetected Selenium
undetected Selenium

Reputation: 193048

To send text to the <input> element with respect to the <label> tag you can create a function as follows :

def test_me(myText):
        driver.find_element_by_xpath("//label[.='" + myText + "']//following::div[1]/input").send_keys("hello")

Now, you can call this function from anywhere within your script as follows :

test_me("Interface Name")
# or
test_me("Neighbor Id")

Upvotes: 0

cruisepandey
cruisepandey

Reputation: 29362

Try this :

//label[text()='Interface Name']/following-sibling::div/child::input 

Upvotes: 2

Related Questions