繁体   English   中英

从函数添加 driver.get() 值时,Selenium 无效参数

[英]Selenium Invalid Argument when added driver.get() value from a function

更新/解决方案

我决定稍微修改一下代码。 我最终使用 pandas read_csv 打开 urls.csv 并使用 iterrows() 遍历 df 列。 现在一切正常。 下面是更新后的代码片段。

df = pd.read_csv(urls, header=0, encoding="utf8", index_col=False)

for index, row in df.iterrows():
    report_type = row[0]
    report_name = row[1]
    file_name = row[2]
    download_report(report_type, report_name, file_name)

----

我正在使用 Selenium 自动化一些报告下载。 我写的原始 python 脚本太重复了,所以我决定把东西组合成一个函数。 此功能导航到系统中的特定位置,通过匹配名称生成报告,下载报告并移动/重命名它。

def download_report(q_type, report_name, file):
    driver.get(q_type)
    driver.find_element_by_xpath("//select[@name='SavedQueriesDropDownList']/option[text()='%s']" % report_name).click()
    driver.implicitly_wait(3)
    driver.find_element_by_xpath("//input[@type='submit' and @value='Run Query']").click()
    driver.implicitly_wait(3)
    driver.find_element_by_id('exportsLinksDiv').click()
    driver.implicitly_wait(3)
    driver.find_element_by_id('ListToolbarRAWEXCELExportLink').click()
    time.sleep(5)
    filename = max([path + "\\" + f for f in os.listdir(path)], key=os.path.getctime)
    print(filename)
    os.rename(filename, out_path + file)

我在一个包含三列的 csv 文件中拥有该函数需要的所有数据:q_type,它是起始 URL 路径,report_name,它告诉驱动程序要选择哪个报告,file 是我希望下载的文件的文件名改名为.

我将所需的值传递给函数,如下所示:

with open(urls, encoding="utf8") as csvfile:
      reader = csv.reader(csvfile, delimiter=',', quotechar='|')
      for row in reader:
          report_type = row[0]
          report_name = row[1]
          file_name = row[2]
          download_report(report_type, report_name, file_name)

当我运行脚本时,函数 driver.get(q_type) 的第一行出现错误:

Traceback (most recent call last):
  File "C:/nf4.py", line 52, in <module>
    download_report(report_type, report_name, file_name)
  File "C:/nf4.py", line 10, in download_report
    driver.get(q_type)
  File "C:\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
  (Session info: chrome=76.0.3809.100)

为了测试,我从函数中打印出 q_type 的值,并可以确认它从 csv 文件中提取 url 并将其作为字符串提取。 真的不确定错误来自哪里。

我正在使用以下驱动程序设置:

# Setup Chrome Driver
chrome_path = r'C:\drivers\chromedriver.exe'
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : r'C:\data-in\raw'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_path, options=chrome_options)

我怀疑您的 q_type 在 URL 前没有领先的 http://(或 https://)。 这会导致您看到的错误消息。 您能否验证是否是这种情况?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM