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

Python 从函数中添加driver.get()值时,Selenium参数无效,python,python-3.x,selenium,webdriver,Python,Python 3.x,Selenium,Webdriver,更新/解决方案 我决定稍微修改一下代码。我最终使用pandas read_csv打开url.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_nam

更新/解决方案

我决定稍微修改一下代码。我最终使用pandas read_csv打开url.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)的第一行出现错误:


我怀疑您的q_类型在URL前面没有前导的http://(或https://)。这将导致您看到的错误消息。你能核实一下情况吗?

没有。这是一个完整的https://url。当我在测试函数中打印它时,我可以单击控制台中的输出,它会直接带我到需要加载驱动程序的页面。get()你能将驱动程序的名称添加到问题中吗(即Chrome/FireFox驱动程序)?如果您使用的是ChromeDriver--verbose和--log level=DEBUG选项可能会为您提供更多信息。只是在问题中添加了我的驱动程序信息。我不懂Python,但这看起来不对:…'%s']%report\u name为了排除这种可能性,您可以删除options=chrome\u options参数,看看这是否有什么不同吗?@jmq不是同样的事情。请尝试在
驱动程序中对url进行硬编码。从
打印(q\u类型)
中获取(“硬编码url”)
。如果您能够使用引发此错误的URL更新问题,这将有所帮助。此外,您只需设置
驱动程序。隐式地等待(3)
一次,它是隐式的,因此一旦设置,它将等待所有
查找元素
调用。它不充当
time.sleep()
。如果你能把你的整个
stacktrace
放到一个同样有用的问题中,我想看看你使用的chrome和chromedriver的组合是什么。
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)
# 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)