Python Selenium操作弹窗下拉菜单报Element not Interactable错误解决
爬虫弹窗下拉菜单交互问题及解决办法
在公司网站运行网络爬虫用于每月生成对应元素,测试时全流程正常,仅在与弹窗菜单交互时出现问题。
原始代码与首次报错
弹窗交互的原始代码如下:
driver.get("www.website.com") driver.find_element(By.ID, "ButtonCreatePeriod").click() time.sleep(1) driver.find_element(By.ID, "NoExpiration").click() time.sleep(1) driver.find_element_by_css_selector('[class="k-widget k-dropdown shorterDropDown"]').click() time.sleep(3) ddelement2 = driver.find_element_by_xpath("//*[text()='December 2021']") action2 = ActionChains(driver) action2.click(on_element=ddelement2).perform()
运行到action2.click(on_element=ddelement2).perform()时抛出错误:
"Message: element not interactable: [object HTMLLIElement] has no size and location"
问题背景
点击ButtonCreatePeriod打开弹窗后,点击class="k-widget k-dropdown shorterDropDown"对应的下拉菜单,需要选中文本为“December 2021”的选项。受网站其他UI对应值不固定的限制,无法使用ID或数值定位,且下拉选项支持滚动,可能是问题诱因。相同代码在网站其他无弹窗的滚动下拉UI中可正常运行。
调试阶段报错
调整方案调试后抛出新错误:
Message: element click intercepted: Element <span title="" class="k widget k-dropdown shorterDropDown k-state-disabled ic-dropdown-readonly" unselectable="on" role="listbox" aria-haspopup="listbox" aria- expanded="false" tabindex="0" aria- owns="MfrCoreTermEffectivePeriodKey_listbox" aria-live="polite" aria- disabled="false" aria-readonly="true" aria-busy="false" aria- activedescendant="l581310d-64f8-4bbc-9354-71b6f041d0e3" style="">... </span> is not clickable at point (434, 383). Other element would receive the click: <p>...</p> (Session info: chrome=93.0.4577.82)
最终解决方案
梳理HTML代码结构后,使用更精准的xpath定位下拉菜单内的列表元素即可解决问题,修复后的代码如下:
driver.find_element_by_css_selector('[class="k-widget k-dropdown shorterDropDown"]').click() time.sleep(1) driver.find_element_by_xpath("//div[@id='MfrCoreTermExpirationPeriodKey-list']//li[text()='December 2021']").click()
内容的提问来源于stack exchange,提问作者datoro
相关产品推荐
相关产品推荐

