selenium通过标签页访问网站

园糯 / 2024-10-05 / 原文

我的电脑上的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')
		#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()

创建于2410042057,修改于2410042057