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

更新/解决方案

我决定稍微修改一下代码。我最终使用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,它告诉驱动程序要选择哪个报告;文件是我想要下载的文件的文件名被重命名为。

我通过以下命令将所需的值传递给函数:

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)

python参考方案

我怀疑您的q_type在URL前面没有开头的http://(或https://)。这将导致您看到的错误消息。您可以验证是否是这种情况吗?

不带括号的调用函数的目的python - python

考虑以下:class objectTest(): def __init__(self,a): self.value = a def get_value(self): return self.value class execute(): def __init__(self): a = objectTest(1) b = objectTest(1) print(…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python:如何根据另一列元素明智地查找一列中的空单元格计数? - python

df = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

将字符串分配给numpy.zeros数组[重复] - python

This question already has answers here: Weird behaviour initializing a numpy array of string data                                                                    (4个答案)         …