tinneko tinneko - 1 year ago 57
Python Question

Locating a child of a child in Selenium (Python)

I'm trying to locate two input fields on an unordered menu list, but Selenium is unable to find them. So far I've attempted to locate them by xpath and class name with an ordinal identifier

("//input[@class=x-form-text x-form-field][4]")

but it either doesn't locate the element or it says it is improperly formatted. The only success I've had is if I use the id, but the number on the end changes every time the page loads.

Is there any way to have it locate the menu list, then the list item, and then the input field? I am totally stumped.

Notes about the menu list: It changes size based on resolution and if it becomes much smaller a down arrow icon will appear and the fields towards the bottom of the list will disappear unless that down button is selected.

Here's an example of the html:

<ul id="ext-gen406" class="x-menu-list">
<li id="ext-comp-1237" class="list-item ">
<li id="ext-comp-1238" class="list-item ">
<li id="ext-comp-1239" class="list-item ">
<li id="ext-comp-1240" class="list-item ">
<li id="ext-comp-1241" class="list-item ">
<li id="ext-comp-1242" class="list-item ">
<li id="ext-comp-1207" class="list-item sep-li">
<li id="ext-comp-1243" class="list-item ">
<li id="ext-comp-1244" class="list-item ">
<li id="ext-comp-1208" class="list-item sep-li">
<li id="ext-comp-1245" class="list-item ">
<li id="ext-comp-1246" class="list-item ">
<li id="ext-comp-1247" class="list-item ">
<div class='xtb-text'>Text Field Label</div>
<li id="ext-comp-1248" class="list-item ">
<div id="ext-gen424" class="x-form-field-wrap x-form-field-trigger-wrap">
<input id="ext-comp-1248" class="x-form-text x-form-field"name="ext-comp-1248"
<li id="ext-comp-1249" class="list-item ">
<li id="ext-comp-1250" class="list-item ">
<div class="x-form-field-wrap x-form-field-trigger-wrap" id="ext-gen426">
<input id="ext-comp-1250" name="ext-comp-1250" class="x-form-text x-form-field"
<li id="ext-comp-1251" class="list-item ">
<li id="ext-comp-1252" class="list-item ">

Answer Source

Ok, so... I finally figured it out!

Here is what worked for me:

driver.find_element_by_xpath("//li/div[contains(text(), 'Text labeling input field')]/../following-sibling::li/div/input")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download