移动端模拟ChromeDriver下Selenium click()方法失效求助
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

