1

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;">
  • 1
    Can you share HTML for the whole "Continent" element including its countries? – Andersson Aug 17 '17 at 04:43
  • How would I do that? Do I right click and hit inspect? –  Aug 17 '17 at 04:48
  • Yep. Then you can right click on required element and choose "Copy Outer HTML" – Andersson Aug 17 '17 at 04:49
  • ` ↕ ` If it helps, clicking on this item simply opens up the dropdown list of countries. It doesn't click on all countries. –  Aug 17 '17 at 04:50
  • I mean the whole block of nodes. Copy it and update your question ("Edit" button). It's not quite clear why you show HTML of `span` element for continent while working selector is for `input` element... [how to copy HTML source](https://stackoverflow.com/questions/23343191/copying-html-code-in-google-chromes-inspect-element/44705304#44705304) – Andersson Aug 17 '17 at 04:53
  • I just updated it. I just read your comment and I agree with you in that the element shouldn't contain `span` element but `input` element. My guess is that the html containing South America should answer your question about `span` and not `input` element. –  Aug 17 '17 at 05:04

1 Answers1

0

Try to click on elements with exact text:

continents_countries = {"Asia": ["China", "India", ...], 
                        "Africa": ["South Africa", "Uganda", ...],
                        "South America": ["Brazil", "Argentina", ...]}

geographic_area = input('\n\n\Select a geographic area: South America, Africa, Asia')
country = input('\n\n\Select a country: %s' % ",".join(continents_countries[geographic_area])
driver.find_element_by_xpath('//label[text()="{}"]'.form‌​at(geographic_area)).click()
driver.find_element_by_xpath('//label[text()="{}"]'.form‌​at(country)).click()
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I think it should work, but I would have to provide the entire list of countries across the three continents. –  Aug 17 '17 at 05:20
  • 1
    How else your user will know which country to select? :) – Andersson Aug 17 '17 at 05:22
  • Fair enough. I was secretly hoping I could get some conditional `input` code. But I guess I could just provide the entire list with `\n\n` separating each set of countries. –  Aug 17 '17 at 05:31
  • 1
    Oh.. You mean to specify only countries for exact continent? I think its quite easy to do. I will update the answer in a minute – Andersson Aug 17 '17 at 05:40
  • I just got `SyntaxError: invalid character in identifier ` error when I entered in the code. Not sure what's wrong. –  Aug 17 '17 at 05:45
  • 1
    There could be some hidden characters when you copy/paste code from SO. It seem to be in "format". Try to re-write the word "format" manually – Andersson Aug 17 '17 at 05:48
  • The code didn't work because instead of opening the dropdown list of countries in the continent, I selected the continent (i.e. all the countries in the continent). –  Aug 17 '17 at 05:53
  • 1
    You have to click `
    ` to open drop-down?
    – Andersson Aug 17 '17 at 05:54
  • ` ↕ ` to open the drop-down. :) :) –  Aug 17 '17 at 05:59
  • 1
    Hm... ok. Try `driver.find_element_by_xpath('//label[text()="{}"]/following-sibling::span'.format(geographic_area)).click()` – Andersson Aug 17 '17 at 06:01
  • `selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //label[text()="Caribbean"]/following‌​-sibling::span because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//label[text()="Caribbean"]/following‌​-sibling::span' is not a valid XPath expression. (Session info: chrome=60.0.3112.101) (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64) ` error =/ –  Aug 17 '17 at 06:13
  • Selecting continent worked, but selecting country gave this error: `selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//label[text()="Bahamas"]/following-sibling::span"} (Session info: chrome=60.0.3112.101) (Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64) ` –  Aug 17 '17 at 06:23
  • 1
    You might need to use [ExplicitWait](http://selenium-python.readthedocs.io/waits.html#explicit-waits) to get dynamically generated element – Andersson Aug 17 '17 at 07:10
  • Got this error `driver.find_element_by_css_selector('//label[text()="{}"]/following-sibling::span'.format(geographic_area)).click() ^ SyntaxError: invalid syntax ` Not sure what's wrong. –  Aug 17 '17 at 14:59
  • 1
    Same problem: re-write words "fo‌​llowing", "geographic‌​_area" and "span"- they contain hidden invalid characters – Andersson Aug 17 '17 at 15:31
  • Thanks for the quick response! I was able to figure out that the issue lies in `driver.find_element_by_xpath('//label[text()="{}"]/following-sibling::span'.format(country)).click()`. Specifically, the item clicked here isn't a dropdown menu, but a box to check off. Here's an example `driver.find_element_by_xpath('//*[@id="fieldareaList__cnt-259"]').click()` –  Aug 17 '17 at 15:44
  • I figured out the issue. This code worked: `driver.find_element_by_xpath('//label[text()="{}"]'.format(country)).click()` –  Aug 17 '17 at 17:07
  • Now, I need to unzip the downloaded file, save one of its content as a csv and rename it as function of the variables selected: https://stackoverflow.com/questions/45741549/python-unzip-downloaded-file-and-save-an-item-as-csv –  Aug 17 '17 at 17:58