使用Python脚本单击启用/禁用WiFi

时间:2018-10-27 18:01:08

标签: python html beautifulsoup python-requests urllib

我想创建一个简单的python脚本,它将登录到我的路由器网站,关闭wifi,然后应用更改,然后执行相反的操作(打开wifi并应用更改),不幸的是,我对html标签的了解和python还不够好,所以请帮助我:)

这是我到目前为止创建的内容:

from bs4 import BeautifulSoup as soup
from urllib.request import urlopen as uReq
import requests

my_url = 'http://router/WLG_adv.htm'
response = requests.get(my_url, auth=requests.auth.HTTPBasicAuth('My_username', 'My_password'))
c = response.content
uClient = uReq(my_url)
soup = BeautifulSoup(c)
rows = soup.findAll('tr')[4::5]
soup.find("button", "button-apply")

...由于我能够使用凭据登录到正确的页面并获得正确的代码进行操作:

这是“ wifi启用复选框”(我相信它是check_wifi_sche())

[<tr>
<td colspan="2">
<input checked="" name="enable_ap" onclick="check_wifi_sche()" type="checkbox" value="enable_ap"/> Enable Wireless Router Radio </td></tr>,   <tr>
<td colspan="2">
<div id="wifi_sche_div1">
<input name="wifi_onoff" type="checkbox" value="wifi_onoff"/> Turn off wireless signal by schedule 
</div>
</td>
</tr>, <tr><td colspan="2" height="12"><div style="background-image:url('liteblue.gif');width:100%"> </div></td></tr>, <tr>
<td colspan="2">
<input checked="" name="wsc_config" type="checkbox"/>
<font>  Keep Existing Wireless Settings  </font>
</td>
</tr>]

这是“应用按钮部分”:

>>> soup.find("button", "button-apply")

<button class="button-apply" name="Apply" onclick="buttonClick(this,'Apply');return checkData();" type="submit" value="Apply"> <span class="roundleft_apply">Apply <span class="apply-icon">    </span></span><span class="roundright_apply">   </span></button>

谢谢。

1 个答案:

答案 0 :(得分:0)

我终于设法解决了这个问题。感谢您的技巧开发,这确实有所帮助。 所以我的任务是做一个简单的脚本来启用/禁用Wifi,然后单击路由器NETGEAR WNR3500Lv2的Apply按钮。

如果有人需要,我会发布信息该怎么做:) (请注意,在路由器地址行中(您必须放置路由器凭据,我在其中放置X和Y。

# -*- coding: latin-1 -*-                   
# Ranorex selocity do chrome'a               <<< Very helpful tool for chrome to extract paths from websites.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()   # Remember to have your chrome driver in $PATH (download driver from google)
base_url = "http://XXXXXXXXXXXX:YYYYYYYYYY@ROUTERS-IP-ADDRESS/WLG_adv.htm"            # Place your login:password here:
driver.get(base_url)

# Wifi click
wifi_button = driver.find_elements_by_xpath("//input[@name='enable_ap' and @value='enable_ap']")[0]
wifi_button.click()

# Apply click
apply_button = driver.find_element_by_xpath("//form[@id='target']/table[@class='subhead2-table']//button[@name='Apply']/span[@class='roundleft_apply']")
apply_button.click()

# Closing page (don't worry, it will complete the task, even when it's closed fast.
driver.stop_client()
driver.close()