簡體   English   中英

如何使用Selenium WebDriver從下拉框中選擇

[英]How to select from drop down box using selenium webdriver

我正在嘗試使用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"}

我在代碼中缺少什么嗎?

是的,請嘗試使用xpath

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

更新

因此,在這種情況下,您必須切換到框架(請注意,我是一個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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM