使用Python+Firefox时Selenium无法向Google Auth输入密码
Hey there! Let's figure out why your Selenium script can type the email but won't fill in the password field—this is a common gotcha with Google's dynamic auth forms, especially since your tutorial is a bit outdated (2016 was a while ago, and Google has tweaked their login UI plenty since then). Here are the most likely fixes:
1. You're targeting the wrong <input> element
Using By.TAG_NAME to grab an <input> is risky because Google's login page has multiple input elements on it. Even if Selenium says it found an element, it might be grabbing a hidden input or a different field entirely, not the actual password box.
Instead, use a more precise locator that targets the password input specifically, using its parent div#passwordIdentifier and the input's type attribute:
Option 1: XPath Locator
# Replace your get_element_by_tag line with this password_input = self.browser.wait.until( EC.visibility_of_element_located( (By.XPATH, "//div[@id='passwordIdentifier']//input[@type='password']") ) ) password_input.send_keys(credentials["Passwd"])
Option 2: CSS Selector
password_input = self.browser.wait.until( EC.visibility_of_element_located( (By.CSS_SELECTOR, "#passwordIdentifier input[type='password']") ) ) password_input.send_keys(credentials["Passwd"])
Both of these ensure you're grabbing the exact password input, not a random input elsewhere on the page.
2. You're waiting for the element to exist, not to be interactable
Your get_element_by_id and get_element_by_tag methods use EC.presence_of_element_located, which only checks if the element exists in the DOM—not if it's visible, enabled, or ready to accept input. Google's password field loads dynamically and might be hidden or disabled for a split second after the email step.
Switch to EC.visibility_of_element_located (checks if the element is visible and present) or EC.element_to_be_clickable (checks if it's visible, enabled, and ready to interact) just like you do for the "Next" buttons. This ensures the input is actually ready before you try to type into it.
3. Try focusing the input first
Sometimes Google's auth inputs require a click to focus before they accept keyboard input. Add a click before sending keys:
password_input.click() password_input.send_keys(credentials["Passwd"])
4. Check for Firefox/Geckodriver compatibility
Since you're using Firefox, make sure your Geckodriver version matches your Firefox version. Mismatched versions can cause weird interaction issues. Grab the Geckodriver version that corresponds to your installed Firefox and update your script to use it.
5. Rule out iframes (unlikely but possible)
While Google's current login form doesn't use iframes for the password field, older versions or some regional variations might. If all else fails, check if the password input is inside an iframe:
# Look for iframes and switch to them temporarily iframes = self.browser.wait.until(EC.presence_of_all_elements_located((By.TAG_NAME, "iframe"))) for iframe in iframes: try: self.browser.switch_to.frame(iframe) # Try locating the password input here password_input = self.browser.find_element(By.CSS_SELECTOR, "input[type='password']") password_input.send_keys(credentials["Passwd"]) self.browser.switch_to.default_content() break except: self.browser.switch_to.default_content() continue
Start with the first two fixes—they're the most probable culprits. Google's UI changes often, so the broad By.TAG_NAME locator from your outdated tutorial just won't cut it anymore.
内容的提问来源于stack exchange,提问作者Haluka Maier-Borst

