user2105555 user2105555 - 1 year ago 81
Python Question

Python - Select item from list within a list in Selenium

First, I'd like to apologize for the confusing title.

To provide a description, I have a list of lists, each of which contains unique values. I'd like to run selenium to click-through the right list and click on the right link. Let's assume that my list looks like this:

Africa
- South Africa
- Uganda
- etc
Asia
- China
- India
- etc
South America
- Brazil
- Argentina
- etc


I'd like to write an interactive code that the user can interface with, such that one is provide a list of continents to choose from, and depending on the selection, is provided with conditional list of countries. Let's assume this is the first section of my code:

geographic_area = input('\n\n\Select a geographic area: South America, Africa, Asia')
driver.find_element_by_css_selector('input[value="{}"]'.form‌​at(geographic_area)).click()


Here, the user is provided a list of continents to choose from, and once it is selected, a corresponding dropdown would be clicked.

But how would I write the second portion of the code? I'd like to use the
input()
feature again, but I would have 3 different lists. For example, let's assume I want to select Brazil: To do so, I have to first select South America, and then Brazil. How would I go about doing this?

Here is some information about South America item (really long):

<div class="node" id="rg1-41-South America">


<input id="fieldareaList__rg1-41" name="fieldareaList" value="rg1-41" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__rg1-41" name="_origareaList" value="true" type="hidden">



<label for="fieldareaList__rg1-41">South America</label> <span onclick="javascript:showHideChildren(this.parentNode)" style="position: relative; top: -0.125em; cursor: crosshair; cursor: ns-resize;">&nbsp;↕&nbsp;</span>



<div class="node" id="cnt-12-" style="">



<input id="fieldareaList__cnt-12" name="fieldareaList" value="cnt-12" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__cnt-12" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__cnt-12" title="ISO code: ">Argentina</label> <span onclick="javascript:showHideChildren(this.parentNode)" style="position: relative; top: -0.125em; cursor: crosshair; cursor: ns-resize;">&nbsp;↕&nbsp;</span>






<div class="node" id="bn1-1155-Buenos Aires">








<input id="fieldareaList__bn1-1155" name="fieldareaList" value="bn1-1155" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1155" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1155">Buenos Aires</label>
</div>

<div class="node" id="bn1-1156-Buenos Aires D.f.">








<input id="fieldareaList__bn1-1156" name="fieldareaList" value="bn1-1156" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1156" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1156">Buenos Aires D.f.</label>
</div>

<div class="node" id="bn1-1157-Catamarca">








<input id="fieldareaList__bn1-1157" name="fieldareaList" value="bn1-1157" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1157" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1157">Catamarca</label>
</div>

<div class="node" id="bn1-1158-Chaco">








<input id="fieldareaList__bn1-1158" name="fieldareaList" value="bn1-1158" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1158" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1158">Chaco</label>
</div>

<div class="node" id="bn1-1159-Chubut">








<input id="fieldareaList__bn1-1159" name="fieldareaList" value="bn1-1159" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1159" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1159">Chubut</label>
</div>

<div class="node" id="bn1-1160-Cordoba">








<input id="fieldareaList__bn1-1160" name="fieldareaList" value="bn1-1160" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1160" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1160">Cordoba</label>
</div>

<div class="node" id="bn1-1161-Corrientes">


<input id="fieldareaList__bn1-1161" name="fieldareaList" value="bn1-1161" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1161" name="_origareaList" value="true" type="hidden">

<label for="fieldareaList__bn1-1161">Corrientes</label>
</div>

<div class="node" id="bn1-1162-Entre Rios">


<input id="fieldareaList__bn1-1162" name="fieldareaList" value="bn1-1162" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1162" name="_origareaList" value="true" type="hidden">



<label for="fieldareaList__bn1-1162">Entre Rios</label>
</div>

<div class="node" id="bn1-1163-Formosa">


<input id="fieldareaList__bn1-1163" name="fieldareaList" value="bn1-1163" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1163" name="_origareaList" value="true" type="hidden">



<label for="fieldareaList__bn1-1163">Formosa</label>
</div>

<div class="node" id="bn1-1164-Jujuy">



<input id="fieldareaList__bn1-1164" name="fieldareaList" value="bn1-1164" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1164" name="_origareaList" value="true" type="hidden">



<label for="fieldareaList__bn1-1164">Jujuy</label>
</div>

<div class="node" id="bn1-1165-La Pampa">



<input id="fieldareaList__bn1-1165" name="fieldareaList" value="bn1-1165" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1165" name="_origareaList" value="true" type="hidden">


<label for="fieldareaList__bn1-1165">La Pampa</label>
</div>

<div class="node" id="bn1-1166-La Rioja">


<input id="fieldareaList__bn1-1166" name="fieldareaList" value="bn1-1166" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1166" name="_origareaList" value="true" type="hidden">
<label for="fieldareaList__bn1-1166">La Rioja</label>
</div>

<div class="node" id="bn1-1167-Mendoza">


<input id="fieldareaList__bn1-1167" name="fieldareaList" value="bn1-1167" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1167" name="_origareaList" value="true" type="hidden">


<label for="fieldareaList__bn1-1167">Mendoza</label>
</div>

<div class="node" id="bn1-1168-Misiones">


<input id="fieldareaList__bn1-1168" name="fieldareaList" value="bn1-1168" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1168" name="_origareaList" value="true" type="hidden">



<label for="fieldareaList__bn1-1168">Misiones</label>
</div>

<div class="node" id="bn1-1169-Neuquen">



<input id="fieldareaList__bn1-1169" name="fieldareaList" value="bn1-1169" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1169" name="_origareaList" value="true" type="hidden">


<label for="fieldareaList__bn1-1169">Neuquen</label>
</div>

<div class="node" id="bn1-1170-Rio Negro">



<input id="fieldareaList__bn1-1170" name="fieldareaList" value="bn1-1170" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1170" name="_origareaList" value="true" type="hidden">


<label for="fieldareaList__bn1-1170">Rio Negro</label>
</div>

<div class="node" id="bn1-1171-Salta">


<input id="fieldareaList__bn1-1171" name="fieldareaList" value="bn1-1171" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1171" name="_origareaList" value="true" type="hidden">


<label for="fieldareaList__bn1-1171">Salta</label>
</div>

<div class="node" id="bn1-1172-San Juan">








<input id="fieldareaList__bn1-1172" name="fieldareaList" value="bn1-1172" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1172" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1172">San Juan</label>
</div>

<div class="node" id="bn1-1173-San Luis">








<input id="fieldareaList__bn1-1173" name="fieldareaList" value="bn1-1173" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1173" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1173">San Luis</label>
</div>

<div class="node" id="bn1-1174-Santa Cruz">








<input id="fieldareaList__bn1-1174" name="fieldareaList" value="bn1-1174" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1174" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1174">Santa Cruz</label>
</div>

<div class="node" id="bn1-1175-Santa Fe">








<input id="fieldareaList__bn1-1175" name="fieldareaList" value="bn1-1175" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1175" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1175">Santa Fe</label>
</div>

<div class="node" id="bn1-1176-Santiago Del Estero">








<input id="fieldareaList__bn1-1176" name="fieldareaList" value="bn1-1176" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1176" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1176">Santiago Del Estero</label>
</div>

<div class="node" id="bn1-1177-Tierra Del Fuego">








<input id="fieldareaList__bn1-1177" name="fieldareaList" value="bn1-1177" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1177" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1177">Tierra Del Fuego</label>
</div>

<div class="node" id="bn1-1178-Tucuman">








<input id="fieldareaList__bn1-1178" name="fieldareaList" value="bn1-1178" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1178" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1178">Tucuman</label>
</div>

</div>

<div class="node" id="cnt-33-" style="">








<input id="fieldareaList__cnt-33" name="fieldareaList" value="cnt-33" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__cnt-33" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__cnt-33" title="ISO code: ">Bolivia</label>






</div>

<div class="node" id="cnt-37-" style="">








<input id="fieldareaList__cnt-37" name="fieldareaList" value="cnt-37" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__cnt-37" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__cnt-37" title="ISO code: ">Brazil</label> <span onclick="javascript:showHideChildren(this.parentNode)" style="position: relative; top: -0.125em; cursor: crosshair; cursor: ns-resize;">&nbsp;↕&nbsp;</span>






<div class="node" id="bn1-1382-Acre" style="display: none;">








<input id="fieldareaList__bn1-1382" name="fieldareaList" value="bn1-1382" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1382" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1382">Acre</label>
</div>

<div class="node" id="bn1-1383-Alagoas" style="display: none;">








<input id="fieldareaList__bn1-1383" name="fieldareaList" value="bn1-1383" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1383" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1383">Alagoas</label>
</div>

<div class="node" id="bn1-1384-Amapa" style="display: none;">








<input id="fieldareaList__bn1-1384" name="fieldareaList" value="bn1-1384" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1384" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1384">Amapa</label>
</div>

<div class="node" id="bn1-1385-Amazonas" style="display: none;">








<input id="fieldareaList__bn1-1385" name="fieldareaList" value="bn1-1385" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1385" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1385">Amazonas</label>
</div>

<div class="node" id="bn1-1386-Bahia" style="display: none;">








<input id="fieldareaList__bn1-1386" name="fieldareaList" value="bn1-1386" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1386" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1386">Bahia</label>
</div>

<div class="node" id="bn1-1387-Ceara" style="display: none;">








<input id="fieldareaList__bn1-1387" name="fieldareaList" value="bn1-1387" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1387" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1387">Ceara</label>
</div>

<div class="node" id="bn1-1388-Distrito Federal" style="display: none;">








<input id="fieldareaList__bn1-1388" name="fieldareaList" value="bn1-1388" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1388" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1388">Distrito Federal</label>
</div>

<div class="node" id="bn1-1389-Espirito Santo" style="display: none;">








<input id="fieldareaList__bn1-1389" name="fieldareaList" value="bn1-1389" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1389" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1389">Espirito Santo</label>
</div>

<div class="node" id="bn1-1390-Goias" style="display: none;">








<input id="fieldareaList__bn1-1390" name="fieldareaList" value="bn1-1390" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1390" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1390">Goias</label>
</div>

<div class="node" id="bn1-1391-Maranhao" style="display: none;">








<input id="fieldareaList__bn1-1391" name="fieldareaList" value="bn1-1391" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1391" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1391">Maranhao</label>
</div>

<div class="node" id="bn1-1392-Mato Grosso" style="display: none;">








<input id="fieldareaList__bn1-1392" name="fieldareaList" value="bn1-1392" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1392" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1392">Mato Grosso</label>
</div>

<div class="node" id="bn1-1393-Mato Grosso Do Sul" style="display: none;">








<input id="fieldareaList__bn1-1393" name="fieldareaList" value="bn1-1393" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1393" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1393">Mato Grosso Do Sul</label>
</div>

<div class="node" id="bn1-1394-Minas Gerais" style="display: none;">








<input id="fieldareaList__bn1-1394" name="fieldareaList" value="bn1-1394" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1394" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1394">Minas Gerais</label>
</div>

<div class="node" id="bn1-1395-Para" style="display: none;">








<input id="fieldareaList__bn1-1395" name="fieldareaList" value="bn1-1395" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1395" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1395">Para</label>
</div>

<div class="node" id="bn1-1396-Paraiba" style="display: none;">








<input id="fieldareaList__bn1-1396" name="fieldareaList" value="bn1-1396" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1396" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1396">Paraiba</label>
</div>

<div class="node" id="bn1-1397-Parana" style="display: none;">








<input id="fieldareaList__bn1-1397" name="fieldareaList" value="bn1-1397" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1397" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1397">Parana</label>
</div>

<div class="node" id="bn1-1398-Pernambuco" style="display: none;">








<input id="fieldareaList__bn1-1398" name="fieldareaList" value="bn1-1398" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1398" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1398">Pernambuco</label>
</div>

<div class="node" id="bn1-1399-Piaui" style="display: none;">








<input id="fieldareaList__bn1-1399" name="fieldareaList" value="bn1-1399" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1399" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1399">Piaui</label>
</div>

<div class="node" id="bn1-1400-Rio De Janeiro" style="display: none;">








<input id="fieldareaList__bn1-1400" name="fieldareaList" value="bn1-1400" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1400" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1400">Rio De Janeiro</label>
</div>

<div class="node" id="bn1-1401-Rio Grande Do Norte" style="display: none;">








<input id="fieldareaList__bn1-1401" name="fieldareaList" value="bn1-1401" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1401" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1401">Rio Grande Do Norte</label>
</div>

<div class="node" id="bn1-1402-Rio Grande Do Sul" style="display: none;">








<input id="fieldareaList__bn1-1402" name="fieldareaList" value="bn1-1402" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1402" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1402">Rio Grande Do Sul</label>
</div>

<div class="node" id="bn1-1403-Rondonia" style="display: none;">








<input id="fieldareaList__bn1-1403" name="fieldareaList" value="bn1-1403" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1403" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1403">Rondonia</label>
</div>

<div class="node" id="bn1-1404-Roraima" style="display: none;">








<input id="fieldareaList__bn1-1404" name="fieldareaList" value="bn1-1404" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1404" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1404">Roraima</label>
</div>

<div class="node" id="bn1-1405-Santa Catarina" style="display: none;">








<input id="fieldareaList__bn1-1405" name="fieldareaList" value="bn1-1405" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1405" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1405">Santa Catarina</label>
</div>

<div class="node" id="bn1-1406-Sao Paulo" style="display: none;">








<input id="fieldareaList__bn1-1406" name="fieldareaList" value="bn1-1406" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1406" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1406">Sao Paulo</label>
</div>

<div class="node" id="bn1-1407-Sergipe" style="display: none;">








<input id="fieldareaList__bn1-1407" name="fieldareaList" value="bn1-1407" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1407" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1407">Sergipe</label>
</div>

<div class="node" id="bn1-1408-Tocantins" style="display: none;">








<input id="fieldareaList__bn1-1408" name="fieldareaList" value="bn1-1408" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1408" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1408">Tocantins</label>
</div>

<div class="node" id="bn1-1409-Brazil - Name Unknown" style="display: none;">








<input id="fieldareaList__bn1-1409" name="fieldareaList" value="bn1-1409" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1409" name="_origareaList" value="true" type="hidden">






<label for="fieldareaList__bn1-1409">Brazil - Name Unknown</label>
</div>

<div class="node" id="bn1-1410-Brazil - Name Unknown" style="display: none;">


<input id="fieldareaList__bn1-1410" name="fieldareaList" value="bn1-1410" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1410" name="_origareaList" value="true" type="hidden">


<label for="fieldareaList__bn1-1410">Brazil - Name Unknown</label>
</div>

<div class="node" id="bn1-1411-Brazil - Name Unknown" style="display: none;">


<input id="fieldareaList__bn1-1411" name="fieldareaList" value="bn1-1411" type="checkbox" onclick="javascript:cbChange(this)" checked="checked">
<input id="_origareaList__bn1-1411" name="_origareaList" value="true" type="hidden">


<label for="fieldareaList__bn1-1411">Brazil - Name Unknown</label>
</div>

<div class="node" id="bn1-1412-Brazil - Name Unknown" style="display: none;">

Answer Source

Try to click on elements with exact text:

geographic_area = input('\n\n\Select a geographic area: South America, Africa, Asia')
country = input('\n\n\Select a country: Brazil, Argentina, ...')
driver.find_element_by_xpath('//label[text()="{}"]'.form‌​at(geographic_area)).click()
driver.find_element_by_xpath('//label[text()="{}"]'.form‌​at(country)).click()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download