selenium 无痕模式打开 浏览器
时间: 2023-09-03 14:10:31 浏览: 83
对于使用Selenium打开无痕模式浏览器的问题,可以通过在ChromeOptions中设置--incognito参数来实现。代码示例:
```
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--incognito')
driver = webdriver.Chrome(chrome_options=options)
```
这将在无痕模式下启动Chrome浏览器。
相关问题
脚本使用google浏览器的无痕模式打开,在结束后,自动删除浏览器痕迹
要在脚本中使用Google Chrome浏览器的无痕模式打开,并在结束后自动删除浏览器痕迹,可以使用ChromeOptions来配置浏览器选项。下面是更新后的示例代码:
```python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# 打开URL文件
with open('url.txt', 'r') as file:
urls = file.read().splitlines()
# 定义特定的域名
specific_domains = ['dick9.cc', 'c6ih0t.com:8003/register?i_code=']
# 创建ChromeOptions对象
chrome_options = Options()
chrome_options.add_argument("--incognito") # 启用无痕模式
# 创建浏览器实例
driver = webdriver.Chrome(options=chrome_options) # 使用Chrome浏览器,需要下载对应的驱动并设置到环境变量中
# 遍历每个URL
for url in urls:
# 构建完整的URL
full_url = 'http://' + url + '/app/?dsadasd'
# 打开浏览器并访问URL
driver.get(full_url)
# 等待页面跳转完成
driver.implicitly_wait(10) # 设置隐式等待时间,单位为秒
# 获取当前页面的URL
current_url = driver.current_url
# 判断当前页面的URL是否包含特定域名
if any(domain in current_url for domain in specific_domains):
# 写入1.txt文本文件
with open('1.txt', 'a') as file:
file.write(url + '\n')
else:
# 写入2.txt文本文件
with open('2.txt', 'a') as file:
file.write(url + '\n')
# 关闭浏览器
driver.quit()
```
通过添加`--incognito`参数到ChromeOptions中的`add_argument`方法,可以启用无痕模式。这样,在每次运行脚本时,将会以无痕模式打开浏览器,并在关闭浏览器后自动删除浏览器痕迹。
记得保存脚本并将url.txt文件放在相同的目录下。运行脚本后,将会根据url.txt中的每一行进行操作,并根据特定域名的存在与否将URL分别写入1.txt和2.txt文本文件中。
如果有任何其他问题,请随时向我提问。
from concurrent.futures import ThreadPoolExecutor from selenium import webdriver from selenium.webdriver.chrome.options import Options # 打开URL文件 with open('url.txt', 'r') as file: urls = file.read().splitlines() # 定义特定的域名 specific_domains = ['4qo4li.com:9516/register?i_code='] # 创建ChromeOptions对象 chrome_options = Options() chrome_options.add_argument("--incognito") # 启用无痕模式 def process_url(url): # 创建浏览器实例 driver = webdriver.Chrome(options=chrome_options) # 使用Chrome浏览器,需要下载对应的驱动并设置到环境变量中 # 构建完整的URL full_url = 'http://' + url + '/a/index.php/54545' # 打开浏览器并访问URL driver.get(full_url) # 等待页面跳转完成 driver.implicitly_wait(10) # 设置隐式等待时间,单位为秒 # 获取当前页面的URL current_url = driver.current_url # 判断当前页面的URL是否包含特定域名 if any(domain in current_url for domain in specific_domains): # 写入1.txt文本文件 with open('1.txt', 'a') as file: file.write(url + '\n') else: # 写入2.txt文本文件 with open('2.txt', 'a') as file: file.write(url + '\n') # 关闭浏览器 driver.quit() # 创建线程池 with ThreadPoolExecutor(max_workers=10) as executor: # 提交任务给线程池 futures = [executor.submit(process_url, url) for url in urls] # 等待所有任务完成 for future in futures: future.result()
这段代码使用了Python的concurrent.futures库来创建了一个线程池,并使用Selenium库来进行网页操作。代码首先从url.txt文件中读取URL列表,然后定义了一个特定的域名列表。接下来,创建了一个ChromeOptions对象,添加了启用无痕模式的参数。然后,定义了一个process_url函数,该函数用于处理每个URL。在函数内部,首先创建了一个Chrome浏览器实例,然后构建完整的URL并打开浏览器访问该URL。然后等待页面跳转完成,获取当前页面的URL,并判断该URL是否包含特定域名。根据判断结果,将URL写入不同的文件中。最后,关闭浏览器。接下来,使用ThreadPoolExecutor创建了一个最大线程数为10的线程池,并将任务提交给线程池进行处理。最后,等待所有任务完成。
请问有什么问题我可以帮助您解答吗?