如何用Selenium WebDriver和Python点击TP-Link modem的Interface Setup按钮
问题:无法点击TP-Link modem设置页面的Interface Setup按钮
我需要点击TP-Link modem设置页面(192.168.1.1)顶部菜单中的Interface Setup按钮,但尝试多次后均失败。
相关HTML代码
<td width="85" bgcolor="#FFFFFF"><a href="navigation-basic.html" onclick="javascript:top.main.location= "../basic/home_wan.htm""> <font color="#666666">Interface<br>Setup</font></a></td>
我的Python代码
driver = webdriver.Chrome(service_log_path=devnull) driver.get('http://192.168.1.1') driver.find_element(By.NAME, 'Login_Name').send_keys('admin') driver.find_element(By.NAME, 'Login_Pwd').send_keys('admin') driver.find_element(By.NAME, 'texttpLoginBtn').click() # 尝试点击按钮的报错代码 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='navigation-basic'][onclick*='home_wan']"))).click()
错误信息
[8500:6912:0726/130142.901:ERROR:device_event_log_impl.cc(214)] [13:01:42.909] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F) [8500:6912:0726/130142.919:ERROR:device_event_log_impl.cc(214)] [13:01:42.918] Bluetooth: bluetooth_adapter_winrt.cc:1074 Getting Default Adapter failed. Traceback (most recent call last): File "d:\NetBlocker\NetBlocker.py", line 33, in <module> WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href^='navigation-basic'][onclick*='home_wan']"))).click() File "C:\--\--\--\--\--\Python310\site-packages\selenium\webdriver\support\wait.py", line 90, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace: Ordinal0 [0x00665FD3+2187219] Ordinal0 [0x005FE6D1+1763025] Ordinal0 [0x00513E78+802424] Ordinal0 [0x00541C10+990224] Ordinal0 [0x00541EAB+990891] Ordinal0 [0x0056EC92+1174674] Ordinal0 [0x0055CBD4+1100756] Ordinal0 [0x0056CFC2+1167298] Ordinal0 [0x0055C9A6+1100198] Ordinal0 [0x00536F80+946048] Ordinal0 [0x00537E76+949878] GetHandleVerifier [0x009090C2+2721218] GetHandleVerifier [0x008FAAF0+2662384] GetHandleVerifier [0x006F137A+526458] GetHandleVerifier [0x006F0416+522518] Ordinal0 [0x00604EAB+1789611] Ordinal0 [0x006097A8+1808296] Ordinal0 [0x00609895+1808533] Ordinal0 [0x006126C1+1844929] BaseThreadInitThunk [0x760DFA29+25] RtlGetAppContainerNamedObjectPath [0x773A7A9E+286] RtlGetAppContainerNamedObjectPath [0x773A7A6E+238] PS D:\NetBlocker> [8500:1368:0726/130237.390:ERROR:util.cc(127)] Can't create base directory: C:\Program Files\Google\GoogleUpdater [7988:4812:0726/130337.193:ERROR:gpu_init.cc(486)] Passthrough is not supported, GL is disabled
页面截图

解决建议
处理框架嵌套:从HTML里的
top.main.location能看出页面用了框架(frame/iframe),按钮不在主文档中,先切换到对应框架再定位:# 登录后切换到包含按钮的框架(替换为实际框架的name/id) driver.switch_to.frame("main") # 再定位点击 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'Interface') and contains(text(), 'Setup')]"))).click()调整定位规则:换更精准的定位方式,比如通过文本或父元素定位:
# 匹配换行文本 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Interface\nSetup"))).click() # 或通过父td元素定位 WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[@bgcolor='#FFFFFF' and @width='85']/a"))).click()模拟JS点击:如果元素存在但无法正常点击,直接执行JS触发跳转:
element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a[href='navigation-basic.html']"))) driver.execute_script("arguments[0].click();", element) # 或者直接执行onclick里的跳转代码 driver.execute_script("top.main.location='../basic/home_wan.htm'")忽略无关报错:错误信息里的USB、蓝牙、GPU相关提示不影响功能,重点解决元素定位超时和框架切换问题。
内容的提问来源于stack exchange,提问作者Erfan Najafzadeh
相关产品推荐
相关产品推荐

