【问题标题】:Selenium "Element not interactable"Selenium“元素不可交互”
【发布时间】:2021-08-02 08:35:09
【问题描述】:

我在一个 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')

我想选择“垃圾服务提供商未收集的内部垃圾箱”选项,但每次运行此脚本时,光标直到悬停在最后一个元素上,这意味着无法找到或操纵该元素?

任何想法都非常感谢!

【问题讨论】:

    标签: java python selenium web element


    【解决方案1】:

    我不会等待元素的存在,而是尝试等待它可见——不是在执行单击时导致该错误。等待的代码是这样的

    wait = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH,collection_arrangement)))
    

    【讨论】:

    • 嗨,Peck,谢谢你的建议,我改变了等待条件,但是这次我收到了超时和与上面相同的错误代码,我可以尝试其他方法吗?谢谢!
    • 我想我需要更多关于/指向您正在测试的页面的链接的信息,以弄清楚到底发生了什么。不确定您是否可以提供它或者它是否是私人的。
    • 谢谢 Peck,我已经提供了更多信息的答案。希望这能有所帮助,谢谢!
    • 首先,您应该在问题中发布附加信息,而不是创建另一个答案。第二——不,不幸的是,这还不足以查看发生了什么,需要完整的 HTML 或链接。
    【解决方案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')
    

    如有必要,请随时等待。希望对您有所帮助

    【讨论】:

    • 嗨,马特,感谢您的建议,但是仍然会出现无法操作元素的情况。错误代码是“元素不可交互:元素当前不可见并且可能无法操作”。而且我可以看到脚本光标仍然出现在之前的元素上,所以这可能不是我正在寻找的实际元素吗?看起来这是来自 html 代码的那个...
    • 嘿西蒙,所以如果你尝试正常找到元素,那么你能做到吗?例如: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'driver.find_element_by_xpath(collection_arrangement)
    • 嗨,马特,不幸的是,这不起作用。光标仍然没有移动到输入框。这可能不是实际元素吗?
    【解决方案3】:

    感谢您的回复,很遗憾我无法提供链接。下面是页面的一些截图,这会有帮助吗? Html code for this section

    Selections

    Input Box

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-08
      • 2021-12-16
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      • 2019-04-19
      • 1970-01-01
      相关资源
      最近更新 更多