Selenium ChromeDriver向Twitter发送Unicode国旗字符时丢失的问题求助
Selenium ChromeDriver向Twitter发送Unicode国旗字符时丢失的问题求助
大家好,我最近在维护一个Twitter机器人,之前用Tweepy库发推的时候,带国旗Unicode字符的内容都能正常发送,完全没出过问题。但因为Twitter API的访问权限变动,我换成了Selenium WebDriver来模拟浏览器操作发推,结果遇到了一个头疼的问题:
当我尝试发送包含Unicode国旗字符的文本(比如'check eng 飶磄beng'、'check sct 飶磄bsct'这类测试内容)时,那些Unicode表示的国旗字符会莫名其妙地“消失”,最终发出的推文只有前面的普通文本部分(比如只剩check eng),就像我录制的演示内容里显示的那样。
下面是我目前用来实现登录和发推的代码,平时会导入到主程序调用,直接运行的话是测试用例:
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 from selenium.webdriver.common.action_chains import ActionChains import config class TweeterChromeConnector: def __init__(self): # 打开Chrome浏览器 self.driver = webdriver.Chrome() self.driver.get("https://twitter.com/login") self.wait = WebDriverWait(self.driver, 20) def login(self, user_name, password): try: # 输入用户名/邮箱/手机号 span_element = self.wait.until( EC.presence_of_element_located((By.XPATH, "//span[text()='Phone, email address, or username']"))) input_element = span_element.find_element(By.XPATH, "./ancestor::div[contains(@class, 'r-')]/following::input") input_element.send_keys(user_name) # 点击下一步 span_element = self.driver.find_element(By.XPATH, "//span[text()='Next']") button_element = span_element.find_element(By.XPATH, "./ancestor::span[contains(@class, 'r-')]") button_element.click() # 输入密码 span_element = self.wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Password']"))) input_element = span_element.find_element(By.XPATH, "./ancestor::div[contains(@class, 'r-')]/following::input") input_element.send_keys(password) # 点击登录 span_element = self.wait.until(EC.presence_of_element_located((By.XPATH, "//span[text()='Log in']"))) button_element = span_element.find_element(By.XPATH, "./ancestor::span[contains(@class, 'r-')]") button_element.click() except Exception as e: print(f"登录时发生错误: {e}") raise def post_tweet(self, post_txt): try: # 定位并等待发推按钮可点击 post_button = self.wait.until( EC.element_to_be_clickable((By.XPATH, "//a[@data-testid='SideNav_NewTweet_Button']")) ) post_button.click() # 定位推文输入框 placeholder_element = self.wait.until( EC.presence_of_element_located((By.XPATH, "//div[@data-testid='tweetTextarea_0_label']"))) input_element = placeholder_element.find_element(By.XPATH, "//div[@class='public-DraftStyleDefault-block public-DraftStyleDefault-ltr']") # 聚焦输入框并发送文本 ActionChains(self.driver).move_to_element(input_element).send_keys(post_txt).perform() # 定位并等待发布按钮可点击且启用 post_button = self.wait.until( EC.element_to_be_clickable((By.XPATH, "//button[@data-testid='tweetButton']")) ) self.wait.until(lambda driver: post_button.is_enabled()) post_button.click() except Exception as e: print(f"发推时发生错误: {e}") raise if __name__ == '__main__': tc = TweeterChromeConnector() tc.login(user_name=config.TWITTER_USER_NAME, password=config.TWITTER_PASSWORD) tc.post_tweet('check eng 飶磄beng') tc.post_tweet('check sct 飶磄bsct') tc.post_tweet('check wls 飶磄bsct') print('done')
我已经确认过文本本身的Unicode编码是正确的,但通过Selenium的send_keys方法发送到Twitter输入框后,这些特殊字符就被过滤掉了。有没有朋友遇到过类似的问题?或者知道怎么让Selenium正确把这些Unicode国旗字符发送到Twitter的输入框里?
备注:内容来源于stack exchange,提问作者toma0711
相关产品推荐
相关产品推荐

