我要选择的下拉菜单存储在 div 中,因此我无法使用 Select() 类。
<div waccolumn="" id="segmentoSelect" class="is-3 column"><label class="label">Canal/Segmento</label><div class="control"><ft-segmentos><div class="control"><wac-select><ng-select class="select-custom ng-select ng-select-single ng-select-searchable ng-select-clearable ng-valid ng-touched ng-dirty ng-select-bottom"><div class="ng-select-container"><div class="ng-value-container"><div class="ng-placeholder"></div><!----><!----><div role="combobox" aria-haspopup="listbox" class="ng-input" aria-expanded="false"><input aria-autocomplete="list" type="text" autocorrect="off" autocapitalize="off" autocomplete="ae8bad186fc7" class="user-success"></div></div><!----><!----><span class="ng-arrow-wrapper"><span class="ng-arrow"></span></span></div><!----></ng-select></wac-select></div></ft-segmentos></div></div>
我尝试使用在标签名称上具有“输入”的不同 xpath:
<input aria-autocomplete="list" type="text" autocorrect="off" autocapitalize="off" autocomplete="ae8bad186fc7" class="user-success" aria-activedescendant="ae8bad186fc7-0" aria-controls="ae8bad186fc7">
并且还尝试发送我想要作为此路径键的选项的名称:
rota = automate.bot.find_element_by_xpath("/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[1]/div/ft-segmentos/div/wac-select/ng-select/div/div/div[2]/input")
rota.clear
rota.send_keys("ROTA")
它似乎适用于第一个菜单,但适用于第二个:
vigencia = automate.bot.find_element_by_xpath("/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[2]/div/wac-select/ng-select/div/div/div[2]/input")
vigencia.clear
vigencia.send_keys("ROTA_1_12_2021")
我收到此错误:
ElementNotInteractableException: Message: element not interactable
(Session info: headless chrome=95.0.4638.69)
如何选择选项以及如何知道 click() 方法是否有效?
编辑:
找到了 div 问题的解决方案:
使用 Selenium 和 Python 选择带有 <div> 标记的组合框选项
现在我正在选择下拉选项的元素并单击该选项,但仍然无法在 selenium 中打开该操作单击选项将生成的页面。
在我的机器人类中创建了一个函数来选择:
def click_elem(self,path):
bot = self.bot
element = bot.find_element_by_xpath(path)
bot.implicitly_wait(10) # seconds
element.click()
drop_down_rota = "/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[1]/div/ft-segmentos/div/wac-select/ng-select/div"
drop_down_vigencia = "/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[2]/div/wac-select/ng-select/div"
drop_down_unidade = "/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[3]/div/wac-select/ng-select/div"
rota = '/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div[1]/div[1]/div/ft-segmentos/div/wac-select/ng-select/ng-dropdown-panel/div/div[2]/div[2]'
vigencia = '/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[2]/div/wac-select/ng-select'
unidade = "/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[3]/div/wac-select/ng-select"
filtrar = "/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div/div[4]/wac-button/button"
automate.bot.implicitly_wait(10) # seconds
automate.bot.get("https://freightech.ambev.com.br/#/escolha-segmento")
automate.click_elem(drop_down_rota)
automate.click_elem(rota)
automate.click_elem(drop_down_vigencia)
automate.click_elem(vigencia)
automate.click_elem(drop_down_unidade)
automate.click_elem(unidade)
automate.click_elem(filtrar)
在我 click() on filter selenium 之后会显示这些选项:
但是当我尝试查找这些元素时,它会返回一个空列表或“find_element_by_xpath”错误:
automate.bot.find_element_by_xpath('/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div[2]/div/div[1]/wac-card-fav/div')
> NoSuchElementException: Message: no such element: Unable to locate
> element:
> {"method":"xpath","selector":"/html/body/app-root/ft-home/div/main/div/wac-escolha-seguimento/ft-escolha-segmento/div[2]/div/div[1]/wac-card-fav/div"}