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

标签 python python-3.x selenium webdriver

更新/解决方案

我决定稍微修改一下代码。我最终使用 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://)。这会导致您看到错误消息。您能验证一下情况是否如此吗?

关于python - 从函数添加 driver.get() 值时,Selenium 参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57483690/

相关文章:

python - 为什么全局关键字需要访问在外部范围内定义的变量?

c# - 如何在 webdriver.quit() 之后停止运行 Selenium 测试?

php - 包括安装 PEAR/PHPUnit 的路径问题

java - Selenium 错误地滚动到 View 中

python - 如何将网页中的 JSON 转换为 Python 脚本

python - 如何在 Python 中获取 Linux 控制台窗口的宽度

Python 3 - 列表到字典。按单词排序

Python urllib2。网址错误 : <urlopen error [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted>

python - 正则表达式捕获所有导入语句

python-3.x - 如何检查panda dataframe组是否具有相同的数据