You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python版Selenium通过标签特定字符串选择未查看的Instagram故事按钮

解决Instagram未查看故事的点击问题

当然可以通过目标字符串定位并点击未查看的故事按钮,以下是几种可行的实现方式:

1. 使用XPath匹配包含指定文本的元素

如果", not seen."是按钮的直接文本内容,或者在某个属性(比如aria-label,Instagram故事按钮常用此属性)中,可以用contains()函数定位:

# 情况1:文本在按钮的直接文本中
unseen_story = driver.find_element(By.XPATH, "//button[contains(text(), ', not seen.')]")
unseen_story.click()

# 情况2:文本在aria-label属性中
unseen_story = driver.find_element(By.XPATH, "//button[contains(@aria-label, ', not seen.')]")
unseen_story.click()

2. 使用CSS选择器匹配属性包含指定文本

如果目标字符串在元素属性中,CSS选择器的*=语法可以匹配包含指定子串的属性值:

unseen_story = driver.find_element(By.CSS_SELECTOR, "button[aria-label*=', not seen.']")
unseen_story.click()

关键注意事项

  • 必须等待页面完全加载后再执行定位操作,避免出现元素未找到的错误,推荐使用显式等待:
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    # 最长等待10秒,直到元素可点击
    unseen_story = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, "//button[contains(@aria-label, ', not seen.')]"))
    )
    unseen_story.click()
    
  • Instagram的页面结构可能会随时更新,若后续定位失效,需要重新检查元素的属性或文本内容,调整定位表达式。

内容的提问来源于stack exchange,提问作者koppany_b

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 04:27:50