在任何PC上使用默认的Firefox配置文件进行Selenium(python)Webdriver授权

时间:2018-08-14 16:43:30

标签: python selenium firefox selenium-webdriver

使用硒我需要使用当前配置文件登录到该站点。 我需要该脚本才能用于任何 random.default 配置文件。 配置文件具有一个随机名称​​ random.default ,为此,我使用os.listdir ()。但是显然有问题。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
default_username = os.getlogin()
ffprofile = os.listdir('C:/users/'+default_username+'/AppData/Roaming/Mozilla/Firefox/Profiles/')
fp = webdriver.FirefoxProfile('C:/users/'+default_username+'/AppData/Roaming/Mozilla/Firefox/Profiles/'+ffprofile)
driver = webdriver.Firefox(fp)
driver.get("http://google.com")

收到的错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-15-1fd6227ccf4f> in <module>()
      4 default_username = os.getlogin()
      5 ffprofile = os.listdir('C:/users/'+default_username+'/AppData/Roaming/Mozilla/Firefox/Profiles/')
----> 6 fp = webdriver.FirefoxProfile('C:/users/'+default_username+'/AppData/Roaming/Mozilla/Firefox/Profiles/'+ffprofile)
      7 driver = webdriver.Firefox(fp)
      8 driver.get("http://google.com")

TypeError: must be str, not list

1 个答案:

答案 0 :(得分:0)

TypeError所示,问题是os.listdir()返回list,而不是str。因此,当您尝试将ffprofile附加到调用webdriver.FirefoxProfile()的字符串中时,将抛出TypeError。我不确定您的确切用例是什么,但这应该可以帮助您使其正常工作而不会出现错误。