Python问题,机械化机器人

时间:2018-05-25 11:50:30

标签: python request bots mechanize

我正在尝试制作一个机器人,以便轻松地重置我的路由器,所以我正在使用机械化来完成这项任务。

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)
response = br.open("http://192.168.0.1/")

br.select_form(nr=0)
br.form['loginUsername']='support'
br.form['loginPassword']='71689637'
response=br.submit()

if(response.read().find("wifi")) != -1:
    # ????? 

如果找到字符串'wifi',则表示机器人已登录,但这里是我卡住的地方,因为重启按钮位于另一个标签页(另一页,我猜是来自同一个对象,指示新的URL它应该能够遵循重定向URL而无需注销)。但是,该选项卡中的按钮是一个按钮,但不是表单。

图片1:

pic 1

图2:

pic 2

这是来源:

https://github.com/SharkiPy/Code-stackoverflow/blob/master/Source

1 个答案:

答案 0 :(得分:0)

以下是使用Selenium的代码的开头,隐藏浏览器。您只需添加浏览路由器时所执行的操作即可。我希望它可以让你开始!

import time
from selenium import webdriver
from selenium.common.exceptions import WebDriverException, NoSuchElementException,InvalidElementStateException,ElementNotInteractableException, StaleElementReferenceException, ElementNotVisibleException
from selenium.webdriver.common.keys import Keys
# There may be some unnecessary import above

from selenium.webdriver.chrome.options import Options

options_chrome = webdriver.ChromeOptions()
options_chrome.binary_location = 'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe' # PATH to your chrome driver (you can also use firefox or any other browser, but options below will not be exactly the same
prefs = {"profile.default_content_setting_values.notifications" : 2} # disable notification by default
options_chrome.add_experimental_option("prefs",prefs)

#### below are options for headless chrome
#options_chrome.add_argument('headless')
#options_chrome.add_argument("disable-gpu")
#options_chrome.add_argument("--start-maximized")
#options_chrome.add_argument("--no-sandbox")
#options_chrome.add_argument("--disable-setuid-sandbox")
#### You should uncomment these lines when your code will be working

# starting browser : 
browser = webdriver.Chrome( options=options_chrome)

# go to the router page :
browser.get("http://192.168.0.1/")

# connect 
elem = browser.find_element_by_id("loginUsername")
elem.send_keys('support')
elem = browser.find_element_by_id("loginPassword")
elem.send_keys('71689637')
elem.send_keys(Keys.RETURN)

# here you need to find your button and click it
button = browser.find_element_by_[Whatever works for you]
button.click()