欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 养生 > 在 Selenium 中使用 Chrome 浏览器的用户数据目录

在 Selenium 中使用 Chrome 浏览器的用户数据目录

2024/10/24 1:56:04 来源:https://blog.csdn.net/sinat_41883985/article/details/142556130  浏览:    关键词:在 Selenium 中使用 Chrome 浏览器的用户数据目录

要在 Selenium 中使用 Chrome 浏览器的用户数据目录,可以通过添加 --user-data-dir 参数来指定 Chrome 的配置文件路径。你可以先手动启动 Chrome 浏览器,并创建一个配置文件,然后在 Selenium 脚本中使用这个配置文件。

创建 ChromeProfile

  1. 手动启动 Chrome 并创建配置文件
    打开终端或命令提示符,运行以下命令启动 Chrome 并指定用户数据目录:

    chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\selenium\ChromeProfile"
    

    这会启动 Chrome 浏览器并创建一个新的用户数据目录 C:\selenium\ChromeProfile

  2. 自定义 ChromeProfile
    你可以在启动的 Chrome 浏览器中进行各种自定义设置(如登录、安装扩展等)。这些设置会保存在 C:\selenium\ChromeProfile 目录中。

在 Selenium 中使用 ChromeProfile

现在你已经创建了一个 ChromeProfile,可以在你的 Selenium 脚本中使用这个配置文件。以下是一个示例代码,展示如何在 Selenium 中使用 --remote-debugging-port--user-data-dir 参数:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManagerchrome_options = Options()
chrome_options.add_argument("--remote-debugging-port=9222")
chrome_options.add_argument(r'--user-data-dir=C:\selenium\ChromeProfile')# 其他选项
chrome_options.add_argument("--headless")  # 如果需要无头模式
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=chrome_options)# 测试代码
driver.get("http://www.google.com")
print(driver.title)
driver.quit()

说明

  1. --remote-debugging-port=9222:启用远程调试端口。这对于调试和开发非常有用。
  2. --user-data-dir=C:\selenium\ChromeProfile:指定用户数据目录。请确保这个路径存在,并且是你之前创建的配置文件路径。
  3. 其他选项--headless--no-sandbox--disable-dev-shm-usage 等选项可以根据需要添加。

查找现有的 ChromeProfile

如果你已经有一个现有的 Chrome 配置文件,可以通过以下步骤找到它的位置:

  1. Windows

    • 默认路径:C:\Users\[YourUsername]\AppData\Local\Google\Chrome\User Data
    • 你可以将上述路径用于 --user-data-dir 参数。
  2. macOS

    • 默认路径:/Users/[YourUsername]/Library/Application Support/Google/Chrome
    • 你可以将上述路径用于 --user-data-dir 参数。
  3. Linux

    • 默认路径:/home/[YourUsername]/.config/google-chrome
    • 你可以将上述路径用于 --user-data-dir 参数。

总结

通过使用 --remote-debugging-port--user-data-dir 参数,你可以在 Selenium 中使用定制的 Chrome 配置文件,从而保持登录状态和其他自定义设置。这对于需要在自动化测试中保持状态的场景非常有用。


要在 Microsoft Edge 中使用特定的用户数据目录和远程调试端口,你需要使用 seleniumwebdriver_manager 库,并相应地配置 Edge 浏览器的选项。以下是如何修改代码以适应 Edge 浏览器的示例。

创建 EdgeProfile

首先,你需要手动创建一个 Edge 浏览器的用户数据目录。你可以通过命令行启动 Edge 并指定用户数据目录:

msedge.exe --remote-debugging-port=9222 --user-data-dir="C:\selenium\EdgeProfile"

这会启动 Edge 浏览器并创建一个新的用户数据目录 C:\selenium\EdgeProfile

在 Selenium 中使用 EdgeProfile

然后,你可以在 Selenium 脚本中使用这个配置文件。以下是一个示例代码,展示如何在 Selenium 中使用 --remote-debugging-port--user-data-dir 参数:

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from webdriver_manager.microsoft import EdgeChromiumDriverManageredge_options = Options()
edge_options.add_argument("--remote-debugging-port=9222")
edge_options.add_argument(r'--user-data-dir=C:\selenium\EdgeProfile')# 其他选项
edge_options.add_argument("--headless")  # 如果需要无头模式
edge_options.add_argument("--no-sandbox")
edge_options.add_argument("--disable-dev-shm-usage")
edge_options.add_experimental_option("excludeSwitches", ["enable-automation"])
edge_options.add_experimental_option('useAutomationExtension', False)service = Service(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=service, options=edge_options)# 测试代码
driver.get("http://www.google.com")
print(driver.title)
driver.quit()

说明

  1. --remote-debugging-port=9222:启用远程调试端口。
  2. --user-data-dir=C:\selenium\EdgeProfile:指定用户数据目录。请确保这个路径存在,并且是你之前创建的配置文件路径。
  3. 其他选项--headless--no-sandbox--disable-dev-shm-usage 等选项可以根据需要添加。

查找现有的 EdgeProfile

如果你已经有一个现有的 Edge 配置文件,可以通过以下步骤找到它的位置:

  1. Windows

    • 默认路径:C:\Users\[YourUsername]\AppData\Local\Microsoft\Edge\User Data
    • 你可以将上述路径用于 --user-data-dir 参数。
  2. macOS

    • 默认路径:/Users/[YourUsername]/Library/Application Support/Microsoft Edge
    • 你可以将上述路径用于 --user-data-dir 参数。
  3. Linux

    • 默认路径:/home/[YourUsername]/.config/microsoft-edge
    • 你可以将上述路径用于 --user-data-dir 参数。

完整示例代码

以下是一个完整的示例代码,结合了上述所有方法来配置 Edge 浏览器:

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from webdriver_manager.microsoft import EdgeChromiumDriverManager
import time
import randomdef human_like_delay():time.sleep(random.uniform(1, 3))edge_options = Options()
edge_options.add_argument("--remote-debugging-port=9222")
edge_options.add_argument(r'--user-data-dir=C:\selenium\EdgeProfile')# 其他选项
edge_options.add_argument("--headless")  # 如果需要无头模式
edge_options.add_argument("--no-sandbox")
edge_options.add_argument("--disable-dev-shm-usage")
edge_options.add_experimental_option("excludeSwitches", ["enable-automation"])
edge_options.add_experimental_option('useAutomationExtension', False)service = Service(EdgeChromiumDriverManager().install())
driver = webdriver.Edge(service=service, options=edge_options)# 测试代码
driver.get("http://www.google.com")
human_like_delay()
search_box = driver.find_element_by_name("q")
search_box.send_keys("Selenium")
human_like_delay()
search_box.submit()
human_like_delay()print(driver.title)
driver.quit()

通过这些方法,你可以在 Edge 浏览器中使用定制的配置文件和远程调试端口,从而保持登录状态和其他自定义设置。这对于需要在自动化测试中保持状态的场景非常有用。希望这些方法对你有所帮助!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com