如何使用selenium webdriver从下拉框中选择

时间:2016-05-30 14:13:31

标签: python selenium selenium-webdriver

我正在尝试使用selenimum webdriver(Python)从Drop Down Box中选择网页中的频道。 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"}

我的代码中遗漏了什么?

1 个答案:

答案 0 :(得分:0)

是的,请尝试使用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");