【问题标题】:Selenium selecting from Dropdown PythonSelenium 从下拉 Python 中选择
【发布时间】:2020-07-10 16:27:02
【问题描述】:

我在 python 中使用 selenium,我希望从下面选择选项 Male:

<div class="formelementcontent">
          <select aria-disabled="false" class="Width150" id="ctl00_Gender" name="ctl00$Gender" onchange="javascript: return doSearch();" style="display: none;">
           <option selected="selected" title="" value="">
           </option>
           <option title="Male" value="MALE">
            Male
           </option>
           <option title="Female" value="FEM">
            Female
           </option>
          </select>

在从下拉列表中选择之前,我需要切换到 iframe

driver.switch_to.frame(iframe)

我尝试了很多选项并进行了广泛的搜索。这让我大部分时间。

driver.find_element_by_id("ctl00_Gender-button").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "ctl00_Gender")))
select=Select(driver.find_element_by_id("ctl00_Gender"))
check=select.select_by_visible_text('Male')

如果我使用 WebDriverWait 会超时。

我尝试通过可见文本和索引进行选择,都给出:

ElementNotInteractableException:元素无法滚动到视图中

【问题讨论】:

    标签: python selenium drop-down-menu


    【解决方案1】:

    根据您共享的 HTML,&lt;select&gt; 标记的 style 属性值设置为 display: none;。所以使用SeleniumWebElement 进行交互会很困难。

    如果您通过 访问网页的DOM Tree,想必您会在&lt;ul&gt; 节点中找到与&lt;option&gt; 节点等效的几个&lt;li&gt; 节点。您可能需要与这些互动。

    您可以在以下位置找到一些相关的详细讨论:

    【讨论】:

      【解决方案2】:

      尝试以下解决方案:

      解决方案 1:

      select = Select(driver.find_element_by_id('ctl00_Gender'))
      select.select_by_value('MALE')
      

      注意将以下导入添加到您的解决方案中:

      from selenium.webdriver.support.ui import Select
      

      解决方案 2:

      driver.find_element_by_xpath("//select[@id='ctl00_Gender']/option[text()='Male']").click()
      

      【讨论】:

      • 试过了,但仍然出现错误,无法将元素滚动到视图中。还编辑了原始帖子以添加我在选择之前切换到 iframe
      • 使用 Action class/javascript 移动到元素,然后从下拉列表中选择一个值
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 2023-03-25
      • 1970-01-01
      • 2022-11-11
      • 2017-03-23
      • 1970-01-01
      相关资源
      最近更新 更多