简体   繁体   English

如何使用Selenium WebDriver从下拉框中选择

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

I am trying to select Channel in a webpage from Drop Down Box using selenimum webdriver (Python). 我正在尝试使用selenimum webdriver(Python)从下拉列表框中选择网页中的频道 HTML is given below: 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>

I have following python code: 我有以下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()

I am getting following error: 我收到以下错误:

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"}

Am I missing something in my code? 我在代码中缺少什么吗?

yes please try with xpath 是的,请尝试使用xpath

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

UPDATE 更新

so in that case you have to switch to frame then (Please note i am a java guy so below answer is in java for python update it into python) 因此,在这种情况下,您必须切换到框架(请注意,我是一个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.

相关问题 如何使用 select 这个非 select 下拉菜单中的元素使用 Selenium Webdriver 和 ZA7F57F35426B9387411 - How to select this element from this non select drop-down menu using Selenium Webdriver and Python 如何使用 python 从 selenium webdriver 的下拉列表中选择一个值? - How do I select a value from drop down in selenium webdriver using python? 如何使用python或java在selenium webdriver中选择kendo-drop - how to select kendo-drop down in selenium webdriver using python or java 如何使用硒从下拉菜单中随机选择? - How to randomly select from a drop-down menu using selenium? 如何使用 Selenium 和 Python 从下拉菜单中的 select 项目? - How to select item from drop down menu using Selenium with Python? 使用Selenium Webdriver从python的下拉列表中选择选项 - Selecting options from drop down list in python using Selenium Webdriver 如何使用 selenium webdriver python 单击呼叫下拉列表? - How to click the call drop down using selenium webdriver python? 如何使用带有Python的Selenium Webdriver从ul li下拉列表中获取所有选项? - How to get all options from ul li drop-down using Selenium Webdriver with Python? 即使元素可见,也无法使用python selenium webdriver从下拉列表中选择值 - Unable to select value from drop down with python selenium webdriver even when elements are visible 无法使用python中的Selenium从下拉列表中选择一个值 - Cant select a value from a drop down list using Selenium in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM