我的电脑上的chrome自动更新到最新版本,再从https://googlechromelabs.github.io/chrome-for-testing/#stable \处下载稳定版chromedriver程序,稳定版和最新版本的版本号接近。chromedriver.exe放在chrome程序的工作目录下,再在脚本里面指明chromedriver.exe的路径。
没有input()阻塞,浏览器访问网站十秒后就关闭窗口了。
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.service import Service
class Body():
def __init__(self):
self.options = webdriver.ChromeOptions()
self.options.add_argument('disable-infobars')
self.options.add_experimental_option("excludeSwitches", ['enable-automation'])
self.options.add_argument('--disable-blink-features')
self.options.add_argument('--disable-blink-features=AutomationControlled')
self.options.add_argument('--disable-gpu')
service = Service('C:/Program Files/Google/Chrome/Application/chromedriver.exe')
self.browser = webdriver.Chrome(service=service, options=self.options)
self.browser.maximize_window()
self.browser.implicitly_wait(5)
self.action_chains = ActionChains(self.browser)
def do(self):
ls=['https://taobao.com','https://baidu.com']
self.browser.get(ls[0])
driver_wait = WebDriverWait(self.browser, 10)
for i in ls[1:]:
newTab=f'window.open("{i}")'
self.browser.execute_script(newTab)
input()
if __name__ == '__main__':
b = Body()
b.do()
域名前需要加上https,否则在linux系统上报错:selenium.common.exceptions.InvalidArgumentException: Message: invalid argument
(Session info: chrome=129.0.6668.100),上述代码在linux和windows上都能运行。
Python之selenium创建多个标签页 https://www.cnblogs.com/mafu/p/14158337.html
python实现自动登录淘宝 https://www.cnblogs.com/Vena/p/18435295
Selenium + Python 之 WebDriver 驱动下载与使用 https://www.cnblogs.com/sunisnyu/p/18442541
创建于2410042057,修改于2410140528
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具