我正在制作一个 Whatsapp 机器人,但它不允许我在不扫描二维码的情况下打开 Whatssapp 网页。我在 Stackoverflow 上看到过其他有同样问题的问题。它说你需要用 chrome 打开你的 chrome 配置文件。
我已经尝试更新Selenium、Pip、Pycharm等,但仍然不起作用。另外,尝试使用 user-data-dir 。但还是什么都没有。
使用 chrome://version 您可以找到 chrome 配置文件的配置文件路径,我复制了该路径并将其添加为参数,但仍然无法使用 Selenium 打开我的 chrome 配置文件。
此外,制作了第二个 chrome 配置文件并将文件夹存储在我电脑上的其他位置,但这也不起作用。尝试了很多方法,但似乎没有任何效果,其他人也遇到了这些问题。
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.add_argument("/Users/maxbenenn/Library/Application Support/Google/Chrome")
options.add_argument("--profile-directory=Profile 2")
driver = webdriver.Chrome(service=Service("/Users/maxbenenn/Desktop/chromedriver"), options = options)
driver.get("https://web.whatsapp.com/")
提及您的个人资料路径,如下所示:
from selenium.webdriver.chrome.service import Service
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:\\Users\\<User>\\AppData\\Local\\Google\\Chrome\\User Data\\")
options.add_argument("--profile-directory=Default") #if you want to use default profile, otherwise mention that specific profile's dir name here
driver = webdriver.Chrome(service=Service("/Users/maxbenenn/Desktop/chromedriver"), options = options)
driver.get("https://web.whatsapp.com/")
https://github.com/ultrafunkamsterdam/unDetected-chromedriver/issues/820
这是您问题的解决方案。使用
undetected_chromedriver
(由 selenium 提供支持)登录 Google 个人资料。
如果您使用裸露的 Selenium,我建议使用它的框架。
以下是如何使用“Botasaurus”作为 Selenium 框架:
首先,使用pip安装Botasaurus:
python -m pip install botasaurus
创建一个名为
main.py
的新文件并写入以下代码:
from botasaurus import BaseTask, BotasaurusDriver, BrowserConfig
# Define a custom scraping Task
class Task(BaseTask):
# Set the browser configuration
browser_config = BrowserConfig(profile='my-profile')
def run(self, driver: BotasaurusDriver, data):
# Visit the Omkar Cloud website
driver.get("https://www.omkar.cloud/")
# Extract the text from the heading element
heading = driver.text("h1")
# Return the data which will be saved as a JSON file
return {
"heading": heading
}
if __name__ == "__main__":
task = Task()
task.execute()
导航到
main.py
所在目录并执行脚本:
python main.py
运行脚本后,将自动创建浏览器配置文件并保存在
profiles/
目录中,名称为“my-profile”。