我在一个 web 项目中使用了 python selenium,到目前为止它与其他 web 元素完美配合,但对于这个元素它总是返回 “元素不可交互:元素当前不可见且不可操作 (会话信息:chrome=90.0.4430.93)"
我要交互的元素的编码是
<select id="collection-arrangement" name="collectionArrangement" class="form-control ng-dirty ng-valid-parse ng-touched ng-empty ng-invalid ng-invalid-required" required="" ng-model="Bin.CollectionArrangement" ng-change="onCollectionArrangementChange(Bin.CollectionArrangement.id)" ng-options="type as type.value for type in CollectionArrangements track by type.value" aria-invalid="true" style=""><option value="" class="" selected="selected">-- select an option --</option><option label="Site pays for collection " value="Site pays for collection " selected="selected">Site pays for collection </option><option label="Waste Service Provider purchases materials" value="Waste Service Provider purchases materials">Waste Service Provider purchases materials</option><option label="Internal bin not collected by Waste Service Provider" value="Internal bin not collected by Waste Service Provider" selected="selected">Internal bin not collected by Waste Service Provider</option></select>
我的代码是
collection_arrangement = '/html/body/div/div[2]/div/section/div/div[1]/div/section/div[3]/div/div[3]/div/div[3]/section/form/table/tbody/tr[5]/td[1]/div/div/div/select'
wait = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,collection_arrangement)))
select = Select(driver.find_element_by_id(collection_arrangement))
driver.find_element_by_xpath(collection_arrangement).click()
select.select_by_value('Internal bin not collected by Waste Service Provider')
我想选择“垃圾服务提供商未收集的内部垃圾箱”选项,但每次运行此脚本时,光标一直悬停在最后一个元素上,这意味着无法找到或操作此元素?
>任何想法都非常感谢!
答案 0 :(得分:2)
看起来您正在将 collection_arrangement
设置为 select
元素的 XPATH,然后尝试将其用于 find_element_by_id
方法而不是实际的 id
.
您也不应该需要手动单击该元素,因此可以尝试以下操作:
select = Select(driver.find_element_by_id('collection-arrangement'))
select.select_by_visible_text('Internal bin not collected by Waste Service Provider')
如有必要,请随时等待。希望有帮助
答案 1 :(得分:0)
我不会等待元素的存在,而是尝试等待它可见——它不是在执行点击时出现,这会导致该错误。等待的代码是这样的
wait = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,collection_arrangement)))
答案 2 :(得分:0)