python - 分离以保持 web 浏览器在 selenium webdriver python 中打开不工作
问题描述
好的,所以我正在尝试在脚本完成执行后保持浏览器打开。我希望能够手动关闭它,这样浏览器就不会在用户完成之前关闭。我将在下面留下我的代码,希望有人可以帮助我看看我哪里出错了。我使用 Chrome 作为浏览器。版本 90.0.4430.85
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
# Launch the browser with 4 tabs.
def launchBrowser():
driver = webdriver.Chrome()
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
# Disables infobar: 'Chrome is being controlled by automation blah blah.' Check: https://github.com/GoogleChrome/chrome-launcher/blob/master/docs/chrome-flags-for-tools.md#--enable-automation if anything goes wrong.
options.add_experimental_option("excludeSwitches", ["enable-automation"])
# Ignores certificate and ssl errors.
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
# Doesn't work!
options.add_experimental_option("detach", True)
# Reject chrome notifications.
prefs = {'profile.default_content_setting_values.notifications': 2}
options.add_experimental_option('prefs', prefs)
# This line works for both the maximization of the window and the notifications preference.
driver = webdriver.Chrome(options=options, executable_path=r'C:\Users\Bernardo\Desktop\Python Projects\Automations\chromedriver.exe')
# Main browser, first tab - River.
driver.get('https://lapaginamillonaria.com/')
cookies_button = driver.find_element_by_class_name('fc-button.fc-cta-manage-options.fc-secondary-button')
ActionChains(driver).move_to_element(cookies_button).click(cookies_button).perform()
interes_legitimo = driver.find_element_by_class_name('fc-preference-legitimate-interest.purpose')
if interes_legitimo.get_attribute('checked') == 'true':
ActionChains(driver).move_to_element(interes_legitimo).click(interes_legitimo).perform()
enviar_button = driver.find_element_by_class_name('fc-button.fc-save-continue.fc-primary-button')
ActionChains(driver).move_to_element(enviar_button).click(enviar_button).perform()
else:
print("Couldn't proceed with our task, master Rari.")
# Second tab - La Nación.
driver.execute_script("window.open('about:blank', 'tab2');")
driver.switch_to.window("tab2")
driver.get('https://www.lanacion.com.ar/')
# Third tab - BBC.
driver.execute_script("window.open('about:blank', 'tab3')")
driver.switch_to.window("tab3")
driver.get('https://www.bbc.com/')
cookies_button = driver.find_element_by_class_name('fc-button.fc-cta-manage-options.fc-secondary-button')
ActionChains(driver).move_to_element(cookies_button).click(cookies_button).perform()
interes_legitimo = driver.find_element_by_class_name('fc-preference-legitimate-interest.purpose')
if interes_legitimo.get_attribute('checked') == 'true':
ActionChains(driver).move_to_element(interes_legitimo).click(interes_legitimo).perform()
enviar_button = driver.find_element_by_class_name('fc-button.fc-save-continue.fc-primary-button')
ActionChains(driver).move_to_element(enviar_button).click(enviar_button).perform()
accept_continue = driver.find_element_by_class_name('continue-button.banner-button')
ActionChains(driver).move_to_element(accept_continue).click(accept_continue).perform()
else:
print("Couldn't proceed with our task.")
take_me_to_news = driver.find_element_by_class_name('orb-nav-newsdotcom')
ActionChains(driver).move_to_element(take_me_to_news).click(take_me_to_news).perform()
launching = launchBrowser()
解决方案
推荐阅读
- bash - 如何仅获取 bash ssh 版本号?
- javascript - 如何将数据从 api 推送到数据集中
- react-native - 如何使 createMaterialTopTabNavigator tabBar 背景颜色为线性渐变
- flutter - 如何在颤振中使用 Navigator.pop() 方法传递多个值?
- javascript - 如何在 Visual Studio 中以所有形式更新 javascript 文件的版本
- c# - 从 Chocolatey 生成的 exe 有他们的 Logo 和描述。有没有办法改变这个?
- amazon-web-services - 每日数据加载的 AWS API Gateway POST 请求
- swift - 复制 iPhone 故事板并直接粘贴到 iPad 故事板上,但并非每个视图控制器都能在 iPad 上正确显示
- javascript - onOpen 状态“失败”是什么意思?
- javascript - 来自类的 JSPDF 自动表格自定义样式