【问题标题】:How to select from drop down box using selenium webdriver如何使用 selenium webdriver 从下拉框中进行选择
【发布时间】:2016-05-30 14:13:31
【问题描述】:

我正在尝试使用 selenimum webdriver (Python) 从下拉框中选择网页中的 频道。 HTML 如下:

   <iframe id="page" frameborder="0" src="DashBoard.htm" onfocus="this.blur()" scrolling="no" cellspacing="0" border="0" name="page" style="width: 1076px; height: 525px; margin-left: 30px; margin-top: 0px; visibility: visible;">
   <!DOCTYPE html>
   <html>
      <head>
      <body class="page-body" onresize="change_size();" onload="change_size();initScrn();setKeys();showWpsAlert();highLightMenu('setup_header', 'wireless_set');">
         <img class="cover-image" src="img/cover-image_noh.gif" style="display: none;">
         <img class="body-image" src="img/subhead2-background_noh.jpg" style="width: 1076px; height: 495px; position: absolute; top: 5px;">
         <div id="full-page-container">
         <form id="target" action="wireless.cgi?id=1484693214" method="POST">
            <input type="hidden" name="buttonHit">
            <input type="hidden" name="buttonValue">
            <img class="subtop-image" src="img/subhead2-top_noh.gif" style="width: 1076px; height: 32px;">
            <div class="subhead2"> Wireless Setup</div>
            <table class="subhead2-table" border="0" style="height: 405px; position: relative; top: -3px; width: 1049px;">
               <tbody>
                  <tr valign="middle" align="left">
                  <tr>
                     <td class="scrollpane-table-seperate-border" colspan="2">
                        <div class="scroll-pane" style="height: 405px; width: 1049px; overflow: auto;">
                           <table style="border-collapse:collapse;width:97%">
                              <tbody>
                                 <tr>
                                 <tr>
                                 <tr>
                                 <tr>
                                    <td colspan="2">
                                       <div id="setting_2G" style="display: block;">
                                          <table cellspacing="0" cellpadding="0" border="0" width="100%" <table="">
                              <tbody>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <td nowrap="">Channel:</td>
                              <td nowrap="">
                              <select size="1" name="w_channel">
                              </td>
                              </tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              <tr>
                              </tbody>
                           </table>
                           </div>
                     </td>
                  </tr>
                  <tr>
                  <tr>
                  <tr>
                  <tr>
                  <tr>
                  <tr>
                  <tr>
               </tbody>
            </table>
            </div>
            </td>
            </tr>
            <tr valign="middle" align="center">
            </tbody>
            </table>
            <img class="subfooter-image" src="img/subhead2-bottom.gif" style="width: 1076px; height: 24px; position: relative; top: -3px;">
            <div class="subhead2-bottom" style="width: 1076px;">
               <input type="hidden" value="0" name="tempSetting">
               <input type="hidden" value="5" name="tempRegion">
               <input type="hidden" value="17" name="setRegion">
               <input type="hidden" value="0" name="wds_enable">
               <input type="hidden" value="0" name="wds_enable_an">
               <input type="hidden" value="0" name="only_mode">
               <input type="hidden" value="0" name="show_wps_alert">
               <input type="hidden" value="WPA-AUTO-PSK" name="security_type_2G">
               <input type="hidden" value="WPA-AUTO-PSK" name="security_type_5G">
               <input type="hidden" value="WPA-AUTO-PSK" name="init_security_type_2G">
               <input type="hidden" value="WPA-AUTO-PSK" name="init_security_type_5G">
               <input type="hidden" value="11" name="initChannel">
               <input type="hidden" value="automatic" name="initAuthType">
               <input type="hidden" value="0" name="initDefaultKey">
               <input type="hidden" value="161" name="initChannel_an">
               <input type="hidden" value="automatic" name="initAuthType_an">
               <input type="hidden" value="0" name="initDefaultKey_an">
               <input type="hidden" value="1" name="telec_dfs_ch_enable">
               <input type="hidden" value="0" name="ce_dfs_ch_enable">
               <input type="hidden" value="0" name="fcc_dfs_ch_enable">
               <input type="hidden" value="1" name="auto_channel_5G">
               <input type="hidden" value="1" name="support_ac_mode">
               <input type="hidden" value="U12H270T00_NETGEAR" name="board_id">
               <input type="hidden" value="0" name="enable_band_steering">
               <input type="hidden" value="SKU_WW" name="fw_sku">
               <input type="hidden" value="0.0.0.0" name="wla_radius_ipaddr">
               <input type="hidden" value="0.0.0.0" name="wlg_radius_ipaddr">
               <input type="hidden" value="WPA-AUTO" name="wla_ent_secu_type">
               <input type="hidden" value="WPA-AUTO" name="wlg_ent_secu_type">
               <input type="hidden" value="192.168.0.100" name="wan_ipaddr">
               <input type="hidden" value="255.255.255.0" name="wan_netmask">
               <a name="helpframe-anchor"></a>
         </form>
         </div>
         <meta content="R7000" name="description">
         <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
         <meta content="text/css" http-equiv="Content-Style-Type">
         <meta content="no-cache" http-equiv="Pragma">
         <meta content="no-cache" http-equiv="Cache-Control">
         <meta content="Mon, 06 Jan 1990 00:00:01 GMT" http-equiv="Expires">
      </body>
   </html>
</iframe>

我有以下 python 代码:

   driver.find_element_by_css_selector("#WLS_menu > span").click()
   Select(driver.find_element_by_name("w_channel")).select_by_visible_text("12")
   driver.find_element_by_name("Apply").click()

我收到以下错误:

File "netgear", line 23, in test_netge Select(driver.find_element_by_name("w_channel")).select_by_visible_text("12")
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
        raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"name","selector":"w_channel"}

我的代码中是否缺少某些内容?

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    是的,请尝试使用 xpath

    driver.find_element_by_xpath("//*[@id='setting_2G']/table/tbody/tr[5]/td[2]/select").send_keys("12")
    

    更新

    所以在这种情况下你必须切换到框架然后(请注意我是一个java人所以下面的答案是在java中为python将它更新为python)

    WebDriver driver = new FirefoxDriver();
    driver.get("‪C:\\Users\\rajnish\\Desktop\\mytest.html");
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    driver.switchTo().frame("page");
    driver.findElement(By.xpath("//*[@id='setting_2G']/table/tbody/tr[5]/td[2]/select")).sendKeys("12");
    

    【讨论】:

    • 从错误看来,它无法使用名称“w_channel”首先找到元素。
    • 嗨,谢谢,我已经更新了我的答案,请看看现在是否有效
    • 不工作。它抛出此错误:NoSuchElementException:消息:无法找到元素:{“method”:“xpath”,“selector”:“//*[@id='setting_2G']/table/tbody/tr[5]/td [2]/选择"}
    • 好的,我用 html 源代码尝试了同样的方法,它工作正常,无论如何请看看那个下拉框是否在任何 iframe 内?
    • 是的,有 iframe。我已经编辑了问题并从 iframe 开始粘贴了 html。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多