selenium - Selenium/Python - 键盘无法访问元素
问题描述
我正在尝试自动填写表格。我用 Selenium 录制了一个脚本。
要填充的字段之一是邮政编码。当我开始输入代码时,会打开一个新窗口以建议适当的选项(javascript 自动填充)
我需要选择 ul 的第一项(参见下面的 html)
我对 Selenium 很陌生,虽然我一直在阅读 Selenium/html 文档,但我在这方面完全被困了将近 1 个月......
非常感谢您的支持
我的代码如下,我收到错误消息“键盘无法访问元素”
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
first_option = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CLASS_NAME, "selected")))
first_option.send_keys(Keys.RETURN)
**HTML**
<div id="localisation_left">
<div class="line toDisable">
<label for="location_p" class="label">Ville ou code postal *</label>
<div class="field-wrapper location-container">
<div class="inputWrapper">
<i id="browserGeoloc" class="icon-geoloc icon-2x blue"></i>
<div class="loaderGif-small hidden"></div>
<input class="nude" name="location_p" id="location_p" autocomplete="off" value="Paris 75010" type="text">
<input name="zipcode" value="" type="hidden">
<input name="city" value="" type="hidden">
<script type="text/javascript">
var numberOfLocation = 1, numberOfAuthorizedLocation = 1;
var cityNewadMultipleLocation = new MultipleLocationNewad('input[name="location_p"]', numberOfLocation, numberOfAuthorizedLocation);
cityNewadMultipleLocation.cityAndZipcodeAreSelected = true;
</script>
<input name="region" value="" type="hidden">
<input name="dpt_code" value="" type="hidden">
</div>
<ul class="location-list visible" style="top: 43px;">
<li data-region="12" data-dpt-code="75" class="selected">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75011</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75015</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75009</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75010</span>
</li>
<li data-region="12" data-dpt-code="75">
<span class="city" title="Paris">Paris</span> <span class="zipcode">75017</span>
</li>
解决方案
您可以click
选择第一个选项,而不是Enter
按键
elem = driver.find_element_by_id("location_p")
elem.send_keys("75")
condition = EC.visibility_of_element_located((By.CSS,
"label[for='location_p'] + div ul.location-list > li"))
first_option = WebDriverWait(driver, 15).until(condition)
first_option.click()
推荐阅读
- php - 按另一个数组对 laravel 集合进行排序
- c# - 如何在 C# 中将 OpenXML WordprocessingDocument 转换为 Word Interop 的文档
- android - 错误:找不到属性“com.xyz.abc:itemBackground”
- javascript - 下载带有 url React 的图片
- jquery - 带有 IF 语句的实时计算 JQuery
- php - FosRestBundle 单个数组和可选的关联数组
- python - 使用 ShellExecute 无法打开超链接失败(错误 2)
- botframework - Microsoft Bot 与 Whatsapp 的集成
- c# - 从 datagridview 中删除多行后,索引超出范围?C#
- d3.js - 在 div 框底部显示条形图