You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Python Selenium打开NOT SECURE URL并实现自动化操作?

解决Selenium无法打开非安全(NOT SECURE)URL及后续自动化操作问题

一、绕过SSL证书验证(核心解决无法打开的问题)

不同浏览器需配置对应的参数绕过证书错误,以下是主流浏览器的实现代码:

Chrome

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# 忽略证书错误与不安全提示
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--ignore-ssl-errors')
# 禁用网页安全限制
chrome_options.add_argument('--disable-web-security')

driver = webdriver.Chrome(options=chrome_options)
driver.get("你的非安全URL")

Firefox

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

firefox_options = Options()
firefox_options.set_preference("security.insecure_field_warning.contextual.enabled", False)
firefox_options.set_preference("security.ssl.errorReporting.enabled", False)
firefox_options.set_preference("browser.ssl_override_behavior", 1)  # 直接绕过证书错误

driver = webdriver.Firefox(options=firefox_options)
driver.get("你的非安全URL")

Edge

from selenium import webdriver
from selenium.webdriver.edge.options import Options

edge_options = Options()
edge_options.add_argument('--ignore-certificate-errors')
edge_options.add_argument('--ignore-ssl-errors')

driver = webdriver.Edge(options=edge_options)
driver.get("你的非安全URL")

二、自动登录页面

页面打开后,通过元素定位完成登录操作(以Chrome为例):

from selenium.webdriver.common.by import By

# 定位并输入用户名
username_input = driver.find_element(By.ID, "username")  # 替换为页面实际元素定位标识
username_input.send_keys("你的用户名")

# 定位并输入密码
password_input = driver.find_element(By.NAME, "password")  # 替换为页面实际元素定位标识
password_input.send_keys("你的密码")

# 点击登录按钮
login_button = driver.find_element(By.XPATH, "//button[@type='submit']")  # 替换为页面实际元素定位标识
login_button.click()

三、执行按钮点击等后续操作

登录完成后,通过显式等待确保元素加载完成,再执行点击:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 最多等待10秒,直到目标按钮加载完成
target_button = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "target-btn"))  # 替换为页面实际元素定位标识
)
target_button.click()

注意事项

  • 所有元素定位标识(ID/NAME/XPATH等)需根据目标页面的DOM结构替换,可通过浏览器F12开发者工具查看元素属性。
  • 优先使用WebDriverWait显式等待,避免用time.sleep()硬等待,提升自动化脚本稳定性。
  • 若仍有安全提示,可尝试添加--allow-running-insecure-content参数(Chrome/Edge适用)。

内容的提问来源于stack exchange,提问作者ABDUR R RAUF TAHIR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 06:07:40