Selenium VBA无法选取含hidden类型input的下拉菜单选项怎么办
问题原因分析
你之前定位的<input type="hidden" value="person">是隐藏域元素,Selenium默认无法对不可见元素执行点击、输入操作,所以会抛出ElementNotVisibleError和Element Not Interactable报错。
适配调整方案
从提供的HTML可以看出,下拉的触发入口是role="button属性的div元素(显示People文本的点击区域+右侧下拉箭头都属于这个触发区),先点击这个元素展开下拉列表,再选择目标选项即可:
' 点击下拉触发按钮,展开选项列表 .FindElementByXPath("//div[@data-qa-id='entity-search-filters']//div[@role='button']").Click ' 等待下拉选项加载完成,可根据实际页面速度调整等待时长 .Wait 500 ' 点击Keywords选项,可根据实际渲染的下拉选项HTML调整XPath规则,优先用data-value、data-qa-id这类专属属性定位更稳定 .FindElementByXPath("//div[text()='Keywords']").Click
备选方案(无交互直接修改值,适合下拉选项定位困难的场景)
如果下拉选项加载不稳定,也可以通过执行JS直接修改隐藏input的value值,跳过模拟点击的步骤:
' 定位隐藏域元素 Set hidden_input = .FindElementByXPath("//div[@data-qa-id='entity-search-filters']//input[@type='hidden']") ' 执行JS修改值为keyword .ExecuteScript "arguments[0].value = 'keyword';", hidden_input
内容的提问来源于stack exchange,提问作者jadeolver
相关产品推荐
相关产品推荐

