selenium通过标签页访问网站
我的电脑上的chrome自动更新到最新版本,再从https://googlechromelabs.github.io/chrome-for-testing/#stable \处下载稳定版chromedriver程序,稳定版和最新版本的版本号接近。chromedriver.exe放在chrome程序的工作目录下,再在脚本里面指明chromedriver.exe的路径。
chromedriver 镜像下载:https://mirrors.huaweicloud.com/chromedriver/137.0.7151.68/
没有input()阻塞,浏览器访问网站十秒后就关闭窗口了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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') #chromedriver路径 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