Python Selenium自动化登录Blogger时Gmail出现“此浏览器或应用可能不安全”错误的解决求助
Hey there! That frustrating error pops up because Google's security systems can easily spot automated browsers like Selenium. They look for telltale signs like missing browser features or automation-specific flags. Let's fix this with some tweaks to your code to make your bot act more like a real human using a normal browser.
Key Solutions to Implement
1. Use Selenium Stealth Properly
You already imported stealth but aren't using it! This tool masks all the automated browser traits that Google checks for. Add this right after initializing your Chrome driver to mimic a real browser's fingerprint.
2. Add ChromeOptions to Block Automation Detection
Chrome has built-in flags that give away automated tools. We'll disable those and add settings to mimic a real user's browser environment.
3. Replace Deprecated Element Locators
Your code uses old methods like find_element_by_id which are no longer supported in newer Selenium versions. Switch to the modern find_element(By.ID, ...) syntax for better reliability.
4. Ditch PyAutoGUI's Hardcoded Clicks
Clicking fixed screen coordinates is super unreliable—it breaks if your screen resolution changes. Use Selenium to locate and interact with elements directly instead.
Modified Full Code
from statistics import mean import pandas as pd from selenium_stealth import stealth from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By # Import modern locator module import time print("ready?") print("loading.......................................") df = pd.read_excel("Moaaz.xlsx","Moaaz") email = "sisoasn81@gmail.com" password = "1*1#36@$%anz" name = df["Name"].values.tolist() website = df["Website"].values.tolist() phone = df["Phone"].values.tolist() logo = df["Logo"].values.tolist() firm_map = df["Firm map"].values.tolist() # Configure Chrome options to avoid detection chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") chrome_options.add_argument("--disable-blink-features=AutomationControlled") chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option('useAutomationExtension', False) # Optional: Use a user data directory to mimic a logged-in profile (create the folder first) # chrome_options.add_argument(r"user-data-dir=C:\Users\YourUser\AppData\Local\Google\Chrome\User Data") driver = webdriver.Chrome("D:\\driver\\chromedriver.exe", options=chrome_options) # Apply stealth settings to mask automated browser traits stealth(driver, languages=["en-US", "en"], vendor="Google Inc.", platform="Win32", webgl_vendor="Intel Inc.", renderer="Intel Iris OpenGL Engine", fix_hairline=True, ) driver.delete_all_cookies() driver.get('https://www.blogger.com/blog/posts/3793019451239403430') time.sleep(2) driver.maximize_window() time.sleep(2) # Use modern element locator syntax driver.find_element(By.ID, 'identifierId').send_keys(email) time.sleep(3) driver.find_element(By.ID, 'identifierNext').click() time.sleep(3) # Note: Password field sometimes uses name="password" instead of id - adjust if needed driver.find_element(By.NAME, 'password').send_keys(password) time.sleep(2) driver.find_element(By.ID, 'passwordNext').click() time.sleep(10) for s in range(len(name)): # Click new post with a reliable XPath locator (adjust if your Blogger UI differs) driver.find_element(By.XPATH, '//div[text()="New post"]').click() time.sleep(2) # Enter name using modern locator driver.find_element(By.CLASS_NAME, 'whsOnd.zHQkBf').send_keys(name[s]) time.sleep(1) # Switch to HTML view using Selenium (replace with actual element locators for your editor) html_button = driver.find_element(By.XPATH, '//div[text()="HTML"]') html_button.click() time.sleep(1) driver.quit() # Properly close the driver instead of stop_client()
Why This Works
- Selenium Stealth: Masks your browser's fingerprint to match a real Chrome installation, hiding traits like
navigator.webdriverbeing true. - ChromeOptions: Disables automation-specific flags and switches that Google uses to detect bots.
- Modern Locators: Avoids deprecated methods that might trigger security flags, and makes your code more maintainable.
- Element-Based Clicks: Eliminates unreliable screen coordinates, so your code works across different devices and resolutions.
If you still run into issues, try adding small random delays instead of fixed time.sleep() calls (like time.sleep(random.uniform(1,3))), which makes your bot's behavior more human-like. Also, avoid running the script too frequently—Google might temporarily block your IP if it detects too many automated attempts.
内容的提问来源于stack exchange,提问作者يوسف محمد

