我一直在尝试创建一个python脚本,它将登录到我的路由器页面,记录所有连接的设备,然后按连接的名称显示它们。我制作了一个如下所示的脚本:
from requests import session
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
url = "http://192.168.1.1/login/login.html"
browser = webdriver.Chrome()
wait = WebDriverWait(browser, 100)
browser.get(url)
kad = browser.find_element_by_id("AuthName")
password = browser.find_element_by_id("AuthPassword")
kad.send_keys("MyRouterLoginName")
password.send_keys("MyRouterLoginPassword")
buton = browser.find_element_by_xpath("/html/body/div[2]/div[2]/div[2]/div/ul/li/div[3]/form/fieldset/ul/li[6]/input") #this is my login button
buton.click()
homepage = "http://192.168.1.1/index.html"
browser.get(homepage) #Router asks for changing default password, i skip it like that
sleep(5)
verify = browser.find_element_by_css_selector('body')
print(verify.text) #see my later explanation
xpathmethod = browser.find_element_by_xpath("/html/body/div[3]/div/div/div/div[3]/div/table/tbody/tr/td[3]/div/ul[1]/li[1]/div[2]/a")
print(xpathmethod.text)
print("Finding by css")
content = browser.find_element_by_css_selector('.addmenu')
print(content.text)
验证线是为了确保网页已满载,但问题是,当网页加载时,它首先加载默认菜单项(如连接状态,网络设置,故障排除等),然后加载当前连接的设备。 Webdriver以某种方式无法识别连接的设备部分并给出“无法定位元素”错误。我尝试过xpath和css选择器方法,但两者都给了我相同的结果。抱歉,我无法完全粘贴html,但这是我检查元素时chrome给我的路径
html body div.index div div #mainframe html body div div #contentPanel #mapView div div table tbody tr td div #wlInfo li div a
你需要这样的东西:
try:
# Load page
browser.get("http://192.168.1.1/index.html")
# Wait 10 seconds before element can be located on the page
# For example, <div class="example"> -- first div with class "example"
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, ".//div[@class='example'][1]")))
except Exception as e:
# Catch an exception in case of element unavailability
print ("Page load error: {}".format(e.message))
找到另一种获取连接设备列表的方法。调制解调器有一个ConnectionStatus页面,它给我一个完整的列表,包括单个字符串中的mac地址和其他详细信息。现在我需要解析它们。将创建另一个问题。