Python+selenium Backup Device Configuration

Source: Internet
Author: User

Since 2016 know Python, will only use Telnet or SSH to operate the network device implementation of automatic command, the Web interface of the device basically helpless, until recently job-hopping company is all convinced that the service equipment, the background is ssh, sorry, not open to customers, SNMP OIDs only provide so few, to bulk operation, deep convincing advice is to buy the central manager, unified device version, I go nm ... Later found that Python can use crawlers, and then found that there are selenium such a thing, automatic on the device on various points, the following is a device operation code

#coding: Utf-8

Import OS

Import time

Import Shutil

From selenium import Webdriver

Options = Webdriver. Chromeoptions ()

Prefs = {' profile.default_content_settings.popups ': 0, ' download.default_directory ': ' e:/python/file/'}

Print Prefs

Options.add_experimental_option (' prefs ', prefs)

Driver = Webdriver. Chrome (executable_path= ' E:\python\soft\chromedriver.exe ', chrome_options=options)

Time.sleep (5)

Driver.get (' https://10.0.0.1 ')

Time.sleep (5)

Username = driver.find_element_by_id (' user ')

Username.send_keys (' admin ')

Password = driver.find_element_by_id (' password ')

Password.send_keys (' xxxxxx ')

button = driver.find_element_by_id (' button ')

Button.Click ()

Time.sleep (5)

Driver.implicitly_wait (10)

Driver.switch_to_window (Driver.window_handles[-1])

Driver.find_element_by_xpath ("//*[contains (Text (), ' System Configuration ')]"). Click ()

Time.sleep (5)

Driver.find_element_by_xpath ("//*[contains (Text (), ' Configure Backup and Restore ')"). Click ()

Time.sleep (5)

Driver.find_element_by_xpath ("//*[contains () (Text (), ' Click to download Configuration ')"). Click ()

Time.sleep (60)

Os.chdir (R ' E:\python\file ')

Conffilename = Os.listdir (' E:\python\\file ') [0]

Os.rename (conffilename, ' HOSTNAME.BCF ')

Shutil.move (' hostname.bcf ', ' E:\python\\backup ')

Driver.quit ()


PS. Deeply convinced to develop writing front-end level really is no language, various tags no id ... Fortunately, the Python has find_element_by_xpath fuzzy query, you can directly locate the desired tag and then give a click () action ...

The rest is a variety of equipment operation, and then fit the code ...


Driver.find_element_by_xpath ("//*[contains (Text (), ' label text ')]")#根据标签文本定位标签

Driver.find_element_by_xpath ("//*[contains (@id, ' extend ')]")#根据标签id定位标签

Driver.find_element_by_xpath ("//*[starts-with (@id, ' ex ')]")#定位标签 (tag ID starts with ex)

Driver.find_element_by_xpath ("//*[end-with (@id, ' nd ')]")#定位标签 (label ID with ND end)

Driver.find_element_by_xpath ("//*[matchs (Text (), ' text ')]") #匹配正则表达式


Python+selenium Backup Device Configuration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.