我正在尝试获取{否则不存在” https://www.example.com/mcqRegionList的HTML代码,但是该HTML代码是根据页面https://www.example.com/mcqLevelList的“选择级别”下拉菜单中选择的值生成的
https://www.example.com/mcqLevelList的HTML代码的快照类似于:
<script>
$(function() {
$("#mcqLevel").change(function() {
var levelCode= $('#mcqLevel option:selected').val();
$("#levelCode").val(levelCode);
$('#mcqMasterForm').attr('action', 'mcqRegionList');
$("#mcqMasterForm").submit();
});
$("#mcqRegion").change(function() {
var levelCode= $('#mcqLevel option:selected').val();
var regionCode= $('#mcqRegion option:selected').val();
$("#levelCode").val(levelCode);
$("#regionCode").val(regionCode);
$('#mcqMasterForm').attr('action', 'mcqCityList');
$("#mcqMasterForm").submit();
});
$("#mcqCity").change(function() {
var levelCode= $('#mcqLevel option:selected').val();
var regionCode= $('#mcqRegion option:selected').val();
var cityCode= $('#mcqCity option:selected').val();
$("#levelCode").val(levelCode);
$("#regionCode").val(regionCode);
$("#cityCode").val(cityCode);
$('#mcqMasterForm').attr('action', 'mcqCenterList');
$("#mcqMasterForm").submit();
});
});
function downloadMcqFile(levelCode,regionCode,cityCode,centerCode)
{
$("#levelCode").val(levelCode);
$("#regionCode").val(regionCode);
$("#cityCode").val(cityCode);
$("#centerCode").val(centerCode);
$('#mcqMasterForm').attr('action', "downloadMcqwisePdfFile");
$("#mcqMasterForm").submit();
}
$('#example').DataTable( {
scrollY: '29vh',
scrollX: true,
"searching": false,
"ordering": false,
scrollCollapse: true,
paging: false,
bInfo: false
} );
</script>
然后我们有:
<thead>
<tr align='center'>
<th style="width: 30%">Select Level</th>
<th style="width: 30%"> Select Region</th>
<th style="width: 30%"> Select City</th>
</tr>
<tr align='center'>
<th>
<select name="levelCode" id="mcqLevel">
<option value="-1">Select Level</option>
<option value="05">UPPER</option>
<option value="03">LOWER</option>
<option value="01">MIDDLE</option>
<option value="02">LEFT</option>
<option value="04">RIGHT</option>
</select>
</th>
<th>
<select>
<option value="">Select Region</option>
</select>
</th>
<th>
<select>
<option value="">Select City</option>
</select>
</th>
</tr>
</thead>
到目前为止,我已经尝试过:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
Stdriver = webdriver.Firefox()
Stdriver.get('https://www.example.com/mcqLevelList')
Select(Stdriver.find_element_by_tag_name("select")).select_by_index(2)
Stdriver.get('https://www.example.com/mcqRegionList')
Stdriver.find_element_by_id('mcqRegion').select_by_index(2)
但是,在最后一行收到错误消息:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="mcqRegion"]