硒以后不会加载元素 [英] Selenium does not get elements loaded later

查看:87
本文介绍了硒以后不会加载元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试制作一个python脚本,该脚本将登录到我的路由器页面,记录所有已连接的设备,然后按其已连接名称显示它们. 我制作了一个脚本,如下所示:

I have been trying to make a python script that will log into my router's page, log all the connected devices, and then display them by their connected names. I have made a script that looks like this:

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在检查元素时给我的路径

The verify line was to make sure the webpage was fully loaded but here is the problem, while webpage loads, it first loads a default menu items (Such as connection status, Networking settings, troubleshooting etc) then loads the devices that are currently connected. Webdriver somehow does not recognize the connected devices section and gives an "unable to locate element" error. I have tried xpath and css selector methods but both gives me the same result. Sorry, I can't paste the html fully but here is the path that chrome gives me when I inspect the element

html body div.index div div #mainframe html body div div #contentPanel #mapView div div table tbody tr td div #wlInfo li div a

推荐答案

找到了另一种获取已连接设备列表的方法. 调制解调器有一个ConnectionStatus页面,在此页面中我可以看到完整列表,其中包括单个字符串中的mac地址和其他详细信息. 现在我需要解析它们.还会对此产生另一个问题.

Found another way to get connected devices list. Modem has a ConnectionStatus page where it gives me a full list including mac addresses and other details in a single string. Now I need to parse them. Will create another question about that.

这篇关于硒以后不会加载元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆