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

移动端模拟ChromeDriver下Selenium click()方法失效求助

Troubleshooting Selenium Click Issue on Local Mobile Emulation (Works on BrowserStack)

Let's break down why your local Selenium script fails to click the "Sign In" button even though it works on BrowserStack, and walk through actionable fixes:

1. First, confirm the button is enabled before clicking

Looking at your page source, the btnSignIn input has disabled="disabled" by default. The checkSignInButton() JavaScript function (triggered by onkeyup on the email/password fields) is supposed to enable it once valid credentials are entered.

On local mobile emulation, the keyup event might not fire properly when using send_keys(), leaving the button disabled. Fix this by adding an explicit wait to ensure the button becomes clickable:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

# After entering your password
wait = WebDriverWait(driver, 10)
sign_in_btn = wait.until(EC.element_to_be_clickable((By.ID, "btnSignIn")))
sign_in_btn.click()

2. Manually trigger the validation JavaScript

If the onkeyup event isn't firing automatically in your local setup, you can directly call the validation function to enable the button:

# After entering password
driver.execute_script("checkSignInButton();")
# Wait for the button to become clickable then click
wait = WebDriverWait(driver, 10)
sign_in_btn = wait.until(EC.element_to_be_clickable((By.ID, "btnSignIn")))
sign_in_btn.click()

3. Use JavaScript to trigger the click

Sometimes native Selenium clicks don't behave as expected in mobile emulation. Try bypassing the native click and using JavaScript to trigger it directly:

sign_in_btn = driver.find_element_by_id("btnSignIn")
driver.execute_script("arguments[0].click();", sign_in_btn)

4. Check ChromeDriver-Chrome version compatibility

BrowserStack maintains perfectly matched Chrome and ChromeDriver versions, but your local setup might have a mismatch. Make sure your installed ChromeDriver version is compatible with your local Chrome browser—even a minor version gap can cause unexpected behavior.

5. Adjust mobile emulation settings

Your current custom device metrics and older user agent might not replicate real mobile browser behavior accurately. Try using a predefined device name instead for more reliable emulation:

mobile_emulation = { "deviceName": "Nexus 5" }
# Rest of your chrome_options setup stays the same

A small time.sleep(1) after entering credentials can also help in some cases, though explicit waits (like the ones above) are always better practice than hardcoded delays.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:56:31