单击时,Selenium 不会保持下拉打开状态

Selenium won't keep a drop down open when it clicks it

我正在用 selenium 编写 python 代码,试图使某些过程自动化。我已经掌握了其中的大部分内容,但我遇到了一个我以前见过的问题。当我使用 selenium 单击打开下拉菜单的按钮时,它会单击按钮并打开菜单,但只有一毫秒,问题是下拉列表中的元素需要可见才能被单击,所以事实上,selenium 不会将下拉列表保持足够长的时间以触发下一个命令(这是一个 WebDriverWait 命令,等待元素可单击,然后单击它。否则它会给出一个元素不可交互的错误。)。

有谁知道我做错了什么或者为什么 selenium 不会让下拉菜单保持打开状态。

我的python代码是

browser.find_element(by=By.XPATH, value='//*[@id="ext4-ext-gen1136"]').click()

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@class='x4-list-plain']//li[@class='x4-boundlist-item' and text()='Show All Events and Traces']"))).click()

并且html代码是

<table id="mavthreatdetailcombo-1029-triggerWrap" class="x4-form-trigger-wrap" cellpadding="0" cellspacing="0" role="presentation" style="table-layout: auto;">
  <tbody role="presentation">
    <tr role="presentation">
      <td id="mavthreatdetailcombo-1029-inputCell" class="x4-form-trigger-input-cell" role="presentation">
      <div class="x4-hide-display x4-form-data-hidden" role="presentation" id="ext4-ext-gen1138"></div>
      <input id="mavthreatdetailcombo-1029-inputEl" type="text" role="combobox" class="x4-form-field x4-form-text x4-trigger-noedit x4-form-focus x4-field-form-focus x4-field-default-form-focus" autocomplete="off" value="Show Latest Event" name="mavthreatdetailcombo-1029-inputEl" readonly="readonly" data-errorqtip="" style="width: 159px;">
    </td>
  <td role="presentation" valign="top" class=" x4-trigger-cell x4-unselectable" style="width:17px;" id="ext4-ext-gen1137">
    <div class="x4-trigger-index-0 x4-form-trigger x4-form-arrow-trigger x4-form-trigger-first" role="presentation" id="ext4-ext-gen1136">
  </div>
</td>
</tr>
</tbody>
</table>

我在我的代码中使用的 XPATH 是来自后面 html 代码的 XPATH。这是向下箭头按钮的代码,单击该按钮将下拉菜单。

<div class="x4-trigger-index-0 x4-form-trigger x4-form-arrow-trigger x4-form-trigger-first" role="presentation" id="ext4-ext-gen1136">

检查下拉项的样式 类,可能 mouse:hover 会出现下拉项,并且单击只会显示选中的项。如果是这样,您可以更改样式以保持显示。

我想通了大家。看起来这只是某种错误,我复制并粘贴了代码以单击下拉菜单,第二次单击它时,它保持打开状态,我不确定为什么,但是如果有人在他们的下拉菜单中遇到同样的问题down 立即关闭,只需尝试单击它两次。