无法使用selenium python webdriver chrome单击或选中复选框

时间:2018-03-25 16:49:55

标签: python-3.x xpath selenium-webdriver css-selectors

在所有不成功的尝试之后,我不得不寻求帮助。 问题是我想创建自动登录的脚本,转到特定选项卡(" WAN"),单击相应的复选框(禁用NAT)并注销。 (它实际上是华为GPON路由器HG8245)..

我的代码在复选框之前就可以了,所以......

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

driver=webdriver.Chrome(r"C:\chromedriver.exe") #load the driver => OK
driver.get("http://192.168.100.1") #go to this web page => OK
driver.find_element_by_id("txt_Username").send_keys("root") #username=root => OK
driver.find_element_by_id("txt_Password").send_keys("root") #password=root => OK
driver.find_element_by_xpath(".//*[@id='button']").click() #click Submit button => OK
driver.find_element_by_xpath(".//*[@id='headerTab']/ul/li[2]/div[2]").click() #Go to the 'WAN' tab => OK

点击复选框我尝试:

#1
driver.find_element_by_xpath("//input[@value='InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANPPPConnection.1']").click()  #=> NOT OK
#2
driver.find_element_by_xpath("//*[@id='record_1']/td[1]/input").click() #=> NOT OK
#3
driver.find_element_by_css_selector("#record_1 > td > input[name=\"rml\"]").click() #=> NOT OK

当然所有的xpath都通过控制台(在Mozilla中)检查chrome,它们看起来很好! 任何想法如何解决这个问题?

HTML代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
   <body>[![enter image description here][1]][1]
      <div id="main">
      <div id="header">
      <div id="center" style="height: 495px;">
      <div id="nav" style="height: 495px;">
      <div id="content" style="height: 495px;">
      <div id="topNav">
      <div id="frameWarpContent" style="height: 470px;">
      <iframe id="frameContent" marginheight="0" marginwidth="0" scrolling="no" src="html/network/wan.asp" style="height: 470px;" height="100%" frameborder="0" width="100%">
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html id="Page" xmlns="http://www.w3.org/1999/xhtml" dir="ltl">
         <head>
         <body class="mainbody">
            <script language="JavaScript" src="../../resource/common/util.js?1103405">
            <script language="JavaScript" src="../../resource/english/jsdiff.js?1103405">
            <script language="JavaScript" src="../../resource/common/tabdes.js?1103405">
            <script language="javascript" src="../common/manage_mode.asp">
            <script language="javascript" src="../common/user_info.asp">
            <script language="javascript" src="../common/topo_info.asp">
            <script language="javascript" src="../common/feature_info.asp">
            <script language="javascript" src="../common/wan_prefix_acquire.asp">
            <script language="javascript" src="../common/wan_address_acquire.asp">
            <script language="javascript" src="../common/wan_dns.asp">
            <script language="javascript" src="../common/wan_list.asp">
            <script language="javascript" src="../common/wlan_list.asp">
            <script language="javascript" src="../common/lanmode_list.asp">
            <script language="javascript" src="../common/policyroute_list.asp">
            <script language="javascript" src="wan_language.html?1103405">
            <script language="javascript" src="../common/wan_pageparse.html?1103405">
            <script language="javascript" src="../common/wan_databind.html?1103405">
            <script language="javascript" src="../common/wan_control.html?1103405">
            <script language="javascript" src="../common/wan_check.html?1103405">
            <script language="JavaScript" src="../../resource/english/bbspdes.html?1103405">
            <script>
            <div id="PromptPanel">
            <script>
            <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <tbody>
            <tr>
            <tr>
            <td id="Wan Connection">
            <table id="wanInstTable" class="tabal_bg" width="100%" cellspacing="1">
            <tbody>
            <tr class="tabal_title">
            <tr id="record_0" class="tabal_01" onclick="selectLine(this.id);">
            <tr id="record_1" class="tabal_01" onclick="selectLine(this.id);">
            <td align="center">
            <input name="rml" value="InternetGatewayDevice.WANDevice.1.WANConnectionDevice.2.WANPPPConnection.1" onclick="" type="checkbox">
            </td>
            <td align="center">
            <td align="center">
            <td title="RealName :HSI Status :Connected IPAddress :10.10.10.10" align="center">
            </tr>
            </tbody>
            </table>
            <script>
            <form id="ConfigForm">
            </td>
            </tr>
            </tbody>
            </table>
            </body>
            </html>
            </iframe>
            </div>
            </div>
            </div>
            <div id="footer">
            <div id="fresh">
            </div>
            </body>
            </html>

*编辑:修改代码标记和IP值

1 个答案:

答案 0 :(得分:1)

考虑提供的HTML代码示例,在处理复选框之前需要switch to iframe

driver.switch_to.frame("frameContent")
driver.find_element_by_xpath("//tr[@id='record_1']//input").click()