【问题标题】:Trying to locate the div after a particular h2 using Selenium locator尝试使用 Selenium 定位器在特定 h2 之后定位 div
【发布时间】:2019-03-18 23:36:06
【问题描述】:

任务:确定每个帖子在 Instagram (e.g.) 中引用特定主题标签的日期和时间。

方法:搜索“最近”标题后的第一个帖子并点击它,然后点击右箭头继续浏览帖子。

问题:我很难识别标题“最新”之后的第一个帖子。

代码

most_recent = driver.find_element(By.XPATH, "//h2[contains(text(), 'Most Recent')]")
ele_post = most_recent.find_element(By.CSS_SELECTOR, '.v1Nh3 a') 
ele_post.click() 

输出: 以上最终返回“热门帖子”不是“最新”中第一个帖子的日期和时间。

任何帮助将不胜感激。

【问题讨论】:

    标签: selenium selenium-webdriver xpath css-selectors instagram


    【解决方案1】:

    解决了。 (愚蠢地没有找到我正在搜索的 div 所在的元素。)

    此代码有效:

    most_recent = driver.find_element(By.XPATH, "(//span[normalize-space(text()) = \'Most Recent\']/following::h2)[1]")
    ele_post = most_recent.find_element(By.CSS_SELECTOR, '.v1Nh3 a') 
    ele_post.click() 
    

    【讨论】:

      猜你喜欢
      • 2011-08-09
      • 1970-01-01
      • 2010-10-30
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 2014-08-22
      相关资源
      最近更新 更多