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

如何将IE端Salesforce自动登录PS脚本改造为支持Chrome/Firefox版本

适配Chrome/Firefox的Salesforce登录PowerShell脚本

Chrome和Firefox不支持IE的COM对象交互方式,我们通过Selenium WebDriver实现同等的自动登录效果,操作步骤如下:

前置准备

  • 安装Selenium PowerShell模块,执行命令:Install-Module Selenium -Scope CurrentUser -Force
  • 下载和本地浏览器版本匹配的驱动:Chrome对应ChromeDriver,Firefox对应GeckoDriver,将驱动文件所在路径加入系统环境变量Path,或直接放在脚本同目录下。

Chrome版本脚本

# 导入Selenium模块
Import-Module Selenium

# 配置登录凭证
$username = "username@domain.com" 
$password = "Mypassword"

# 启动Chrome并打开Salesforce登录页
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver
$driver.Url = "https://login.salesforce.com"

# 等待页面加载完成,可根据网络情况调整等待时长
Start-Sleep -Seconds 2

# 填充账号密码
$driver.FindElementById("username").SendKeys($username)
$driver.FindElementById("password").SendKeys($password)

# 点击登录按钮
$driver.FindElementByCssSelector(".button.r4.wide.primary").Click()

Firefox版本脚本

仅需修改浏览器驱动实例化语句即可,其余逻辑完全一致:

# 导入Selenium模块
Import-Module Selenium

# 配置登录凭证
$username = "username@domain.com" 
$password = "Mypassword"

# 启动Firefox并打开Salesforce登录页
$driver = New-Object OpenQA.Selenium.Firefox.FirefoxDriver
$driver.Url = "https://login.salesforce.com"

# 等待页面加载完成,可根据网络情况调整等待时长
Start-Sleep -Seconds 2

# 填充账号密码
$driver.FindElementById("username").SendKeys($username)
$driver.FindElementById("password").SendKeys($password)

# 点击登录按钮
$driver.FindElementByCssSelector(".button.r4.wide.primary").Click()

注意事项

  • 不要明文存储敏感凭证,生产环境建议调用系统安全凭证管理接口读取用户名、密码,避免信息泄露
  • 若页面加载速度慢导致元素定位失败,可适当调大Start-Sleep的等待时长,或添加Selenium显式等待逻辑
  • 若Salesforce登录页的元素属性有更新,可手动打开页面按F12检查对应元素的ID、类名,修改脚本中的定位参数即可

内容的提问来源于stack exchange,提问作者Burmed IT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 06:09:01