PHP前端开发

selenium webdriver 打开chrome但打不开whatsappWeb

百变鹏仔 1天前 #Python
文章标签 打不开
问题内容

我有这个代码:

from selenium import webdriverfrom selenium.webdriver.chrome.service import servicefrom time import sleep# create a service object by specifying the path to chromedriverchrome_service = service(executable_path="pathcrome.exe")options = webdriver.chromeoptions()options.add_argument("user-data-dir=c:\path_file")# pass the service object to webdriver.chrome()driver = webdriver.chrome(service=chrome_service, options=options)# open whatsapp webdriver.get("https://web.whatsapp.com/")sleep(10)

当我运行此代码时,chrome 浏览器会打开,但 whatsapp 网页不会打开。如何解决这个问题?

我尝试使用几种不同的方法,例如:

from webdriver_manager.chrome import chromedrivermanagerdriver = webdriver.chrome(chromedrivermanager().install())

和:

options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")

但是没有一个起作用


正确答案


您不再需要指定 chrome 可执行路径。安装 chrome 并将其设置为默认浏览器就足够了。

我测试了这个,它工作正常:

from selenium import webdriverfrom time import sleepoptions = webdriver.ChromeOptions()options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")#options.add_argument("user-data-dir=C:\path_file")# Pass the Service object to webdriver.Chrome()driver = webdriver.Chrome(options=options)# Open WhatsApp Webdriver.get("https://web.whatsapp.com/")sleep(10)