SeleniumPython打开了一个与我用来清除数据的Chrome不同的版本

bvk5enib  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(103)
from selenium import webdriver
import undetected_chromedriver as UC
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

# Start Chrome browser
CHROMEDRIVER = UC.Chrome() 

def start_driver():
    driver = UC.Chrome()
    delete_cache(driver)
    return driver

def delete_cache(driver):
    driver.execute_script("window.open('')")  # Create a separate tab than the main one
    driver.switch_to.window(driver.window_handles[-1])  # Switch window to the second tab
    driver.get('chrome://settings/clearBrowserData')  # Open your chrome settings.
    perform_actions(driver, Keys.TAB + Keys.ENTER)  # Tab to the time select and key down to say "All Time" then go to the Confirm button and press Enter
    
    time.sleep(2)
    driver.close()  # Close that window
    driver.switch_to.window(driver.window_handles[0])  # Switch Selenium controls to the original tab to continue normal functionality.

def perform_actions(driver, keys):
    actions = ActionChains(driver)
    actions.send_keys(keys)
    time.sleep(2)
    print('Performing Actions!')
    actions.perform()

if __name__ == '__main__':
    driver = start_driver()

我尝试使用selenium自动化删除Chrome的历史记录,但selenium打开的示例与我的不同。每当我打开浏览器,并检查历史记录是剩余的,但 selenium 的Chrome版本是不同的,它总是没有历史记录。我想从我的Chrome浏览器中删除历史记录。我已经通过访问网站检查了我的本地示例,我看到网站正在缓存。但是selenium打开的示例不会缓存。

cbeh67ev

cbeh67ev1#

这是因为自动化是在由Selenium控制的单独Chrome示例中运行的,不会影响您的主Chrome浏览器。

相关问题