首先,我想为这个令人困惑的头衔道歉。
为了提供描述,我有一个列表列表,每个列表都包含唯一值。我想运行selenium点击右侧列表,然后点击右侧链接。我们假设我的列表如下所示:
Africa
- South Africa
- Uganda
- etc
Asia
- China
- India
- etc
South America
- Brazil
- Argentina
- etc
我想编写一个用户可以与之交互的交互式代码,以便提供一个可供选择的大陆列表,并根据选择提供条件国家/地区列表。我们假设这是我的代码的第一部分:
geographic_area = input('\n\n\Select a geographic area: South America, Africa, Asia')
driver.find_element_by_css_selector('input[value="{}"]'.format(geographic_area)).click()
此处,向用户提供可供选择的大陆列表,并且一旦选择,将点击相应的下拉列表。
但是我如何编写代码的第二部分?我想再次使用input()
功能,但我会有3个不同的列表。例如,让我们假设我想选择巴西:为此,我必须首先选择南美洲,然后选择巴西。我该怎么做呢?
以下是有关南美项目的信息(真的很长):
<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;"> ↕ </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;"> ↕ </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;"> ↕ </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;">
答案 0 :(得分:0)
尝试点击具有确切文字的元素:
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()="{}"]'.format(geographic_area)).click()
driver.find_element_by_xpath('//label[text()="{}"]'.format(country)).click()