Python Selenium调用Chrome打开网页失败,显示空白页并报错
问题解决:Selenium Chrome打开空白页并触发InvalidArgumentException
错误根源
代码中传入driver.get()的URL格式非法:
driver.get("'https://ynet.co.il")里的URL开头多了一个单引号',导致浏览器无法识别有效地址,触发InvalidArgumentException- 注释中的WhatsApp链接也存在同样的格式错误,且字符串拼接方式不正确
修正方案
修复测试URL
去掉URL开头多余的单引号,确保字符串格式正确:driver.get("https://ynet.co.il")正确拼接WhatsApp链接
注释里的链接写法错误,若要动态拼接手机号和消息,使用f-string或.format():# 先定义变量x(手机号)和message(消息内容) x = "1234567890" message = "测试消息" driver.get(f"https://web.whatsapp.com/send?phone={x}&text={message}")修正导入拼写错误
代码中ChromeDriverManagerz多了一个字母z,改为正确的ChromeDriverManager:from webdriver_manager.chrome import ChromeDriverManager
修正后的完整代码
import pandas as pd import webbrowser from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager options = Options() options.add_experimental_option("detach", True) driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) # 访问测试网站 driver.get("https://ynet.co.il") # 可选:访问WhatsWeb发送消息(需先定义x和message) # x = "目标手机号" # message = "要发送的内容" # driver.get(f"https://web.whatsapp.com/send?phone={x}&text={message}") import time
内容的提问来源于stack exchange,提问作者Vadim Nave
相关产品推荐
相关产品推荐

