我有这个代码:
from selenium import webdriver
from selenium.webdriver.chrome.service import service
from time import sleep
# create a service object by specifying the path to chromedriver
chrome_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 web
driver.get("https://web.whatsapp.com/")
sleep(10)
当我运行此代码时,chrome 浏览器会打开,但 whatsapp 网页不会打开。如何解决这个问题?
我尝试使用几种不同的方法,例如:
from webdriver_manager.chrome import chromedrivermanager
driver = 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 webdriver
from time import sleep
options = 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 Web
driver.get("https://web.whatsapp.com/")
sleep(10)