PayPal沙箱在Chrome无头模式下无法完成支付流程求助
PayPal沙箱无头测试点击授权按钮冻结的解决建议
问题概述
- 测试场景:执行用户注册并选择PayPal支付的自动化测试,使用Selenium WebDriver 3.14.1
- 异常现象:Chrome非无头模式下测试可正常通过;无头模式下,登录PayPal后被重定向至
https://www.sandbox.paypal.com/webapps/hermes?../checkout/selectFi,点击Continue按钮进入授权页面后,点击Agree and Continue按钮(HTML代码如下),按钮会被禁用且页面冻结,无法跳转回商户页面 - 有头模式对比:同一测试在有头模式下跳转的PayPal URL为
https://www.sandbox.paypal.com/webapps/hermes?../billingweb/review,授权按钮可正常工作
授权按钮HTML代码:
<div id="button" class="buttons reviewButton"> <input track-submit="" type="submit" value="Agree and Continue" id="confirmButtonTop" class="btn full confirmButton continueButton" ng-disabled="onPayButtonDisabled" data-test-id="continueButton" validate-submit="onPay()"> </div>
解决建议
模拟真实浏览器环境配置:无头Chrome默认的窗口尺寸、用户代理等与有头模式差异较大,PayPal可能据此切换页面逻辑。添加以下配置缩小环境差异:
ChromeOptions options = new ChromeOptions(); // 启用新版无头模式,更贴近真实浏览器行为 options.addArguments("--headless=new"); // 设置和有头模式一致的窗口尺寸 options.addArguments("window-size=1920,1080"); // 设置真实用户代理,避免被识别为无头浏览器 options.addArguments("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"); // 禁用自动化检测特征 options.addArguments("--disable-blink-features=AutomationControlled"); // 解决无头模式下的沙箱限制问题 options.addArguments("--no-sandbox"); options.addArguments("--disable-dev-shm-usage");优化页面等待逻辑:无头模式下页面加载和渲染速度与有头模式存在差异,使用显式等待确保元素完全可交互后再操作,避免过早触发点击导致逻辑异常:
WebDriverWait wait = new WebDriverWait(driver, 15); // 等待按钮可点击 WebElement confirmButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("confirmButtonTop"))); confirmButton.click(); // 点击后等待页面跳转或按钮失效,确保后续逻辑触发 wait.until(ExpectedConditions.stalenessOf(confirmButton));升级Selenium版本:Selenium 3.14.1版本较陈旧,可能与新版Chrome存在兼容性问题。升级至Selenium 4.x版本,并配合对应版本的ChromeDriver,能更好支持无头模式下的复杂交互逻辑。
排查页面跳转差异原因:无头模式下跳转到
selectFi页面,大概率是PayPal的风控机制识别到自动化环境。除上述配置外,可尝试在测试流程中添加随机延时(模拟用户操作节奏),或确保测试使用的PayPal沙箱账号无异常标记。
内容的提问来源于stack exchange,提问作者reactdesign
相关产品推荐
相关产品推荐

