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

求助:使用ConfigFileParser时触发Selenium InvalidArgumentException错误

Selenium InvalidArgumentException 问题排查与解决

调用Selenium的driver.get()方法时触发selenium.common.exceptions.InvalidArgumentException: Message: invalid argument错误,以下是相关代码及排查解决方案:

相关代码

TestLogin类代码

class TestLogin:
config = ConfigFileParser('login.ini')

def test_login(self, setup):
    self.driver = setup
    self.driver.get(self.config.getUrl())

ConfigFileParser.py代码

class ConfigFileParser:
cfgFileDir = 'TestData'
config = configparser.ConfigParser()

def __init__(self, cfg):
    self.cfgFile = cfg
    self.base_dir = Path(__file__).resolve().parent.parent
    self.config_file = self.base_dir.joinpath(self.cfgFileDir).joinpath(self.cfgFile)
    self.config.read(self.config_file)

def getUrl(self):
    return self.config['login']['uatenv']

配置文件内容

配置文件login.ini内容如下:

[login]
uatenv = https://xxx.xxx.com/

排查与解决步骤

  1. 验证URL有效性
    先打印getUrl()的返回值,确认是否为合法URL:

    print(self.config.getUrl())
    

    如果输出为空、格式错误(比如缺少http/https前缀),直接修正配置文件里的uatenv值。

  2. 检查配置文件加载状态
    在ConfigFileParser的__init__方法里添加打印,确认配置文件路径是否正确、文件是否存在:

    def __init__(self, cfg):
        self.cfgFile = cfg
        self.base_dir = Path(__file__).resolve().parent.parent
        self.config_file = self.base_dir.joinpath(self.cfgFileDir).joinpath(self.cfgFile)
        print(f"配置文件路径:{self.config_file}")
        print(f"文件是否存在:{self.config_file.exists()}")
        self.config.read(self.config_file)
    

    如果文件不存在,调整路径拼接逻辑或cfgFileDir的值,确保能定位到login.ini。

  3. 修复类属性共享问题
    当前config是类属性,多实例共享会导致读取异常,改为实例属性:

    class ConfigFileParser:
        cfgFileDir = 'TestData'
    
        def __init__(self, cfg):
            self.cfgFile = cfg
            self.base_dir = Path(__file__).resolve().parent.parent
            self.config_file = self.base_dir.joinpath(self.cfgFileDir).joinpath(self.cfgFile)
            self.config = configparser.ConfigParser()  # 改为实例属性
            self.config.read(self.config_file)
    
        def getUrl(self):
            return self.config['login']['uatenv']
    
  4. 添加异常防护
    避免因配置项缺失抛出错误:

    def getUrl(self):
        try:
            return self.config.get('login', 'uatenv', fallback='')
        except Exception as e:
            print(f"读取URL失败:{e}")
            return ''
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 20:35:24