Home > Backend Development > Python Tutorial > selenium webdriver opens chrome but cannot open whatsappWeb

selenium webdriver opens chrome but cannot open whatsappWeb

WBOY
Release: 2024-02-08 23:03:03
forward
789 people have browsed it

selenium webdriver 打开chrome但打不开whatsappWeb

问题内容

我有这个代码:

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="path\crome.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)
Copy after login

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

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

from webdriver_manager.chrome import chromedrivermanager

driver = webdriver.chrome(chromedrivermanager().install())
Copy after login

和:

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")
Copy after login

但是没有一个起作用


正确答案


您不再需要指定 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)
Copy after login

The above is the detailed content of selenium webdriver opens chrome but cannot open whatsappWeb. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template