Python 个问题,机械化机器人
Python issues, mechanize 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:
图片二:
来源如下:
https://github.com/SharkiPy/Code-Whosebug/blob/master/Source
这是使用 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()
我正在尝试制作一个机器人以便轻松地重置我的路由器,所以我正在使用 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:
图片二:
来源如下:
https://github.com/SharkiPy/Code-Whosebug/blob/master/Source
这是使用 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()