[英]How to open a Chrome Profile through Python
My script I have been writing has been working great.我一直在写的剧本一直工作得很好。 I just added the option so it would open a profile on chrome using this code.我刚刚添加了该选项,因此它将使用此代码在 chrome 上打开一个配置文件。
options = webdriver.ChromeOptions
browser = webdriver.Chrome(executable_path=r"C:\Users\princess\AppData\Local\Programs\Python\Python36-32\chromedriver.exe", chrome_options=options)
options.add_argument(r'user-data-dir=C:\Users\princess\AppData\Local\Google\Chrome\User Data')
options.add_argument('--profile-directory=Profile 1')
When used, I get this error code.使用时,我收到此错误代码。
C:\Users\Princess\Desktop>CHBO.py
Traceback (most recent call last):
File "C:\Users\Princess\Desktop\CHBO.py", line 12, in <module>
browser = webdriver.Chrome(executable_path=r"C:\Users\princess\AppData\Local\Programs\Python\Python36-32\chromedriver.exe", chrome_options=options)
File "C:\Users\Princess\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 59, in __init__
desired_capabilities = options.to_capabilities()
TypeError: to_capabilities() missing 1 required positional argument: 'self'
How can I fix this?我该如何解决这个问题?
To create and open a new Chrome Profile you need to follow the following steps :要创建并打开新的Chrome 配置文件,您需要按照以下步骤操作:
chrome://settings/
opens up.打开Chrome浏览器,点击侧边菜单上,并点击该URL设置chrome://settings/
打开。Get the absolute path of the profile-directory in your system as follows :获取系统中配置文件目录的绝对路径,如下所示:
C:\\\\Users\\\\Otaku_Wiz\\\\AppData\\\\Local\\\\Google\\\\Chrome\\\\User Data\\\\Profile 2
Now pass the value of profile-directory through an instance of Options with add_argument()
method along with key user-data-dir as follows :现在通过带有add_argument()
方法和 key user-data-dir的Options实例传递profile-directory的值,如下所示:
from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument("user-data-dir=C:\\\\Users\\\\AtechM_03\\\\AppData\\\\Local\\\\Google\\\\Chrome\\\\User Data\\\\Profile 2") driver = webdriver.Chrome(executable_path=r'C:\\path\\to\\chromedriver.exe', chrome_options=options) driver.get("https://www.google.co.in")
Execute your Test
执行你的Test
You can use options = Options()
or options = webdriver.ChromeOptions()
at place of options = webdriver.ChromeOptions
您可以使用options = Options()
或options = webdriver.ChromeOptions()
代替options = webdriver.ChromeOptions
Otherwise you are pointing at an object (namely webdriver.ChromeOptions
), and not making an instance of that object by including the needed parenthesis否则,您将指向一个对象(即webdriver.ChromeOptions
),而不是通过包含所需的括号来创建该对象的实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.