通过 selenium 进行日志记录是否被 twitter 屏蔽了?

问题描述 投票:0回答:1

即使使用 selenium 具有有效凭据,也无法登录 Twitter。元素接受凭据,验证它们,但登录后不重定向到主页。

登录详细信息和主要代码:

email = 'twitter_email'
username = 'twitter_username'
password = 'twitter_password'
try:
    login_button = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'a[data-testid = loginButton]')))
    login_button.click()

    if driver.current_url == login_url:
        print('Login Page')
    else:
        print(f'Still at {base_url}')

    username_field = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[autocomplete = username]')))
    username_field.send_keys(username, Keys.RETURN)
    print('Username is valid')
    password_field = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[type = password]')))
    password_field.send_keys(password, Keys.RETURN)
    print('Password is valid')

    if wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[data-testid = ocfEnterTextTextInput]'))):
        email_field = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'input[data-testid = ocfEnterTextTextInput]')))
        email_field.send_keys(email, Keys.RETURN)
        print('Email verified')
    else:
        print('Email verification not required')

    # wait.until(lambda driver: driver.current_url != login_url)
    if driver.current_url == home_url:
        print('Logged in successfully!')
    else:
        print(f"Login failed! Current page: {driver.current_url}")

except TimeoutException:
    print('Login Failed due to timeout!')

输出:

Login Page
Username is valid
Password is valid
Email verified
Login failed! Current page: https://twitter.com/i/flow/login

登录成功后也无法进入主页。

我已声明我的网址并将其与基本 twitter 网址连接起来,因为 twitter 网址遵循基于用户查询创建网址的模式。

base_url = 'https://twitter.com/'
login_element = 'i/flow/login'
home_element = 'home'
search_query = 'Random Query'
login_url = base_url + login_element
home_url = base_url + home_element
driver = gs.Chrome()
driver.get(base_url)
wait = WebDriverWait(driver, 30)

我无法在 Twitter 主页上找到任何可用元素,因此通过检查 current_url 和 login_url 彼此不匹配来检查重定向,但它引发了错误,因此对其进行了评论并使用 if else 块来检查是否令我惊讶的是,驱动程序位于主页,即使使用正确的凭据登录后,它仍将当前页面显示为登录页面。我认为“current_url”显示提供给它的输入,而不是实际的当前页面。

# wait.until(lambda driver: driver.current_url != login_url)
if driver.current_url == home_url:
    print('Logged in successfully!')
else:
    print(f"Login failed! Current page: {driver.current_url}")

所以我尝试从base_url开始最终到达login_url,我得出的结论是current_url是驱动程序当前所在的url,而不是我提供的输入。

login_button = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, 'a[data-testid = loginButton]')))
login_button.click()

if driver.current_url == login_url:
    print('Login Page')
else:
    print('Still at base_url')

因此,问题仅在通过提供凭据进行登录后出现,因为即使在提供并输入凭据并点击返回登录后,驱动程序仍停留在登录页面。我尝试登录 Twitter 时在代码中做错了什么?

python selenium-webdriver web-scraping twitter
1个回答
0
投票
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
 
username = 'username'
password = 'password'

 # Function to login to Twitter
    def login_to_twitter(username, password):
        driver = webdriver.Chrome()
        wait = WebDriverWait(driver, 10)
    
        try:
            # Load the login page
            driver.get("https://twitter.com/login")
    
            # Find the username input field and enter the username
            username_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[name="text"]')))
            username_input.send_keys(username)
    
            login_button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '[role=button].r-13qz1uu')))
            login_button.click()
    
            password_input = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input[name="password"]')))
            password_input.send_keys(password)
    
            # Find and click the login button
            login_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div[data-testid="LoginForm_Login_Button"]')))
            login_button.click()
    
            # Wait for the home page to load after successful login
            home_url = 'https://twitter.com/home'
            wait.until(EC.url_to_be(home_url))
            print('Logged in successfully!')
    
        except Exception as e:
            print('Login Failed:', e)
    
        finally:
            driver.quit()
    
    # Call the login function
    login_to_twitter(username, password)
© www.soinside.com 2019 - 2024. All rights reserved.