You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebDriver自定义URL输入报错求助:ip变量未定义(NameError)

问题排查与解决

报错原因

你遇到的NameError: name 'ip' is not defined是因为代码中使用的ip变量从未被定义或赋值。另外注意到你贴出的代码里写的是ip_of_the_tested_device,但报错栈里的代码用的是ip,这说明你实际运行的代码和你贴的代码不一致,变量名没统一。

具体解决步骤

  • 定义并赋值IP变量:先给要使用的IP变量赋值,可以选择让用户手动输入,或者直接指定固定IP(根据测试需求):
    # 方式1:手动输入IP
    ip_of_the_tested_device = input("请输入测试设备的IP地址:")
    # 方式2:直接指定IP
    ip_of_the_tested_device = "192.168.1.100"  # 替换为实际测试设备的IP
    
  • 统一变量名:确保driver.get()里的变量名和你定义的完全一致,同时补全WebDriver初始化等必要代码:
    from selenium import webdriver
    from selenium.common.exceptions import TimeoutException
    
    # 初始化WebDriver(以Chrome为例,需提前配置好驱动)
    driver = webdriver.Chrome()
    try:
        # 使用正确的URL格式:http://或https://前缀 + IP
        driver.get(f'http://{ip_of_the_tested_device}')
        # 如果目标设备用https协议,改成f'https://{ip_of_the_tested_device}'
    except TimeoutException:
        print("无法访问该URL")
    finally:
        # 操作完成后关闭浏览器,释放资源
        driver.quit()
    
  • 修正URL格式:你提到的“https/IP_adress”格式是错误的,正确的URL必须包含://协议前缀(如http://192.168.1.1),否则WebDriver无法正确识别并访问目标地址。

内容的提问来源于stack exchange,提问作者Basma Sbaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 09:47:10