【问题标题】:how to enter value in dropdownlist(using AJAX) that is not visible in selenium by python如何在下拉列表中输入值(使用 AJAX)在 python 中不可见的 selenium
【发布时间】:2013-12-23 15:26:05
【问题描述】:

我是 selenium 新手,我想在下拉列表中输入值,并尝试了各种方法并在网络上搜索,我已经看到 STACKOVER 流程中的许多帖子对我没有任何帮助

这是 HTML 代码

我试过了

1)    x=Select(driver.find_element_by_id("expiry-date-month"))

       x.select_by_value("4")


2) element = driver.find_element_by_id('expiry-date-month')
    for option in element.find_elements_by_tag_name('option'):
          if option.text=='4':
             option.click() 

当我尝试收到错误消息时,我想选择月份复选框值,因为“元素当前不可见,因此可能无法与之交互”,请告诉我一个解决方案

HTML 代码

<form id="form-credit-card" method="get" enctype="application/x-www-form-urlencoded" novalidate="novalidate">
<div class="row-fluid"/>
<div class="row-fluid">
<div class="row-fluid card-wrap">
<div class="row-fluid name-wrap">
<div class="row-fluid dob-wrap hide" style="opacity: 0;">
<div class="row-fluid maestro-wrap hide" style="opacity: 0;">
<div class="row-fluid">
<div class="span3 iframe expiry-wrap">
<label for="expiry-date">Expiration date</label>
<select id="expiry-date-month" class="expirydatemonth selectBox" size="1" name="expirydatemonth" style="display: none;">
<option value="">Month</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<a class="selectBox expirydatemonth selectBox-dropdown selectBox-active" style="width: 30px; display: inline-block; -moz-user-select: none;" title="" tabindex="0">
<span class="selectBox-label" style="width: 0px;">Month</span>
<span class="selectBox-arrow"/>
</a>
<span class="slash">/</span>
<select id="expiry-date-year" class="expirydateyear selectBox" size="1" name="expirydateyear" style="display: none;">
<a class="selectBox expirydateyear selectBox-dropdown" style="width: 30px; display: inline-block; -moz-user-select: none;" title="" tabindex="0">
</div>

【问题讨论】:

  • 您的第一次尝试 1 将是执行此操作的正常方式。尝试这样做时会出现什么错误或异常?
  • “元素当前不可见,因此可能无法与之交互”这是我使用的任何方法都会收到的错误消息
  • 我正在使用 selenium 2.37.2 和 Firefox 版本 20
  • driver.execute_script("document.getElementById('expiry-date-month').style.displa‌​y = 'block'") time.sleep(5) try: x = Select(driver .find_element_by_id("expiry-date-month")) x.select_by_visible_text("4") print "entered text" except NoSuchElementException: print "Error", 但是我得到了一个新的下拉列表并且没有在其中输入任何值像这样尝试过

标签: ajax python-2.7 selenium selenium-webdriver


【解决方案1】:

作为一种解决方法,您需要使用 JavaScript 为该下拉菜单启用显示,因为显示设置为“无”。

下面的代码应该适合你:

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('expiry-date-month').style.display='block';");

//Then Select required value
x=Select(driver.find_element_by_id("expiry-date-month"))

x.select_by_value("4")

【讨论】:

  • 我确实喜欢你提到的像这样的书面代码 driver.execute_script("document.getElementById('expiry-date-month').style.display = 'block'") time.sleep(5 )尝试: x = Select(driver.find_element_by_id("expiry-date-month")) x.select_by_visible_text("4") print "entered text" except NoSuchElementException: print "Error" but iam getting a new dropdown list 除此之外,没有在其中输入任何值
  • 嗨..关于这个的任何想法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2015-03-03
  • 1970-01-01
  • 2021-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多