[Python] Issues with Automated Twitter Account Creation Bot in Python (Playwright) - Unable to...

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm developing a bot in Python to automate the account creation process on Twitter (X) using Playwright, but I am consistently facing issues in certain steps, especially when trying to find and click the "Authenticate" button after filling out the initial account details.

    Project Context:


    • Python Version: 3.11


    • Library: Playwright


    • Operating System: Windows 10

    The bot successfully:


    • Accesses the account creation page


    • Clicks "Sign Up"


    • Fills in name, email, and birthdate


    • Proceeds through the initial screens

    But the bot consistently fails to find and click the "Authenticate" button that appears after these initial steps.

    Here’s the part of the code that attempts to click the "Authenticate" button:

    print("\nSearching for 'Authenticate' button...")
    auth_button_selectors = [
    'button.eZxMRy', # Specific class selector for the button
    'button[data-theme="home.verifyButton"]', # data-theme selector
    'button.sc-nkuzb1-0.sc-d5trka-0', # Full class names
    '.eZxMRy.button', # Class combination
    'button:has-text("Authenticate")', # Text-based selector in English
    'button:has-text("Autenticar")', # Text-based selector in Portuguese
    '[role="button"]:has-text("Authenticate")', # Role button in English
    '[role="button"]:has-text("Autenticar")', # Role button in Portuguese
    'div[role="button"]:has-text("Authenticate")', # div with role button in English
    'div[role="button"]:has-text("Autenticar")', # div with role button in Portuguese
    'span:has-text("Authenticate")', # Span with text in English
    'span:has-text("Autenticar")', # Span with text in Portuguese
    'button[type="submit"]', # Generic submit button
    '[data-testid="confirmationSheetConfirm"]', # Specific testid
    '[data-testid="ocfSignupNextLink"]', # testid for next button
    '[data-testid="SignupButton"]', # testid for signup button
    'button.css-18t94o4', # Generic Twitter primary button class
    'button.css-901oao' # Another common button class
    ]

    # Method 1: Try clicking the button with selectors
    auth_clicked = False
    for selector in auth_button_selectors:
    try:
    if page.locator(selector).count() > 0:
    print(f"'Authenticate' button found with selector: {selector}")
    page.locator(selector).first.click(force=True, timeout=5000)
    auth_clicked = True
    break
    except Exception as e:
    print(f"Error while trying to click the 'Authenticate' button: {e}")
    continue

    # Method 2: Attempt to find any black button on the page
    if not auth_clicked:
    print("Trying to find a black button on the page...")
    try:
    # Search for buttons with dark background color via JavaScript
    found = page.evaluate("""() => {
    const buttons = Array.from(document.querySelectorAll('button'));
    for (const button of buttons) {
    const style = window.getComputedStyle(button);
    const bgColor = style.backgroundColor;
    // Check if the color is dark (low RGB values)
    if (bgColor && (
    bgColor.includes('rgb(0, 0, 0)') ||
    bgColor.includes('rgb(15, 20, 25)')
    )) {
    button.click();
    return true;
    }
    }
    return false;
    }""")
    except Exception as e:
    print(f"Error while trying to find black button: {e}")


    Problems Faced: The "Authenticate" button is not found with conventional selectors.

    I’ve tried over 20 different selectors.

    The selectors change frequently in the Twitter interface, making it difficult to maintain the script.

    JavaScript Errors: When using :has-text() with JavaScript, I get the error: DOMException: Failed to execute 'querySelectorAll' on 'Document': 'button:has-text("Authenticate")' is not a valid selector.

    Partial temporary solution: The only method that works intermittently is searching for "black buttons" via JavaScript and clicking on them. However, this doesn't always work as the layout changes frequently.

    What happens after failure: Sometimes, Twitter ignores the "Authenticate" button and moves on to the next step (CAPTCHAs).

    Other times, it redirects to the Twitter homepage.

    What I've Already Tried:


    • Using force: true to click elements.


    • Increasing timeouts.


    • Trying clicks via JavaScript.


    • Searching for visual characteristics (black button).


    • Implementing a solution to restart the account creation flow if redirected to the homepage.

    Here’s the code I use in case the bot is redirected to the homepage:

    Handle redirect to homepage


    if page.url == "https://twitter.com/":
    print("Redirected to homepage. Restarting the flow...")
    \# Code to restart the account creation process here


    Questions:


    • Is there a more reliable technique to identify and click this specific "Authenticate" button?


    • Could the problem be related to anti-bot measures Twitter is using?


    • Is it possible that Twitter is dynamically changing classes to prevent automation?


    • Are there alternatives to the method I’m using to click the button?

    Logs and Errors: Searching for 'Authenticate' button... Trying to click via JavaScript... Error while clicking via JavaScript with selector button:has-text("Authenticate"): DOMException: Failed to execute 'querySelectorAll' on 'Document': 'button:has-text("Authenticate")' is not a valid selector. Trying to find black button on the page... Black button found and clicked via JavaScript.

    Continue reading...

Compartilhe esta Página