【问题标题】:how to extract a href tag elemnt from selenium python?如何从 selenium python 中提取 href 标签元素?
【发布时间】:2021-04-20 09:33:22
【问题描述】:

我真的是 Selenium 的新手,希望在以下情况下提供帮助。我正在尝试为 Facebook 制作生日机器人。我在禁用 js 的情况下运行 selenium,这使得代码更容易,因为我很难通过 Facebook 的最新 UI 进行操作。 现在,我尝试访问当前生日的文本框,但我不能,所以我试图提取所有今天过生日的朋友的个人资料链接。 我尝试过使用下面的 sn-ps,但实际上没有任何效果。 如果我可以将_keys 发送到文本框或从

中提取配置文件链接,两者都会有所帮助

请查看图片了解详情。

birthday = driver.find_element_by_css_selector('bj cf cq')
containers=birthday.find_element_by_css_selector("[class='bj cf cq']")
containers=birthday.find_element_by_xpath('//*[@id="events_dashboard_calendar_container"]/div/article[1]/div')
birthdayslist= birthdaystoday.find_elements_by_css_selector("[class='ce cf cg']")

list=driver.find_element_by_xpath('//*[@id="events_dashboard_calendar_container"]/div/article[1]/div/ul/div[2]')

//*[@id="events_dashboard_calendar_container"]/div/article[1]/div/ul/div[2]
driver.find_elements_by_class_name("ce cf cg")

find_element_by_tag_name("a").get_property("href")

page source code

【问题讨论】:

    标签: python html css selenium


    【解决方案1】:

    使用下面的代码(缩进固定):

    birthdays = driver.find_elements_by_xpath("//div[@id='events_dashboard_calendar_container']//a")
    for birthday in birthdays:
        print(birthday.get_attribute("href"))
    

    【讨论】:

    • 嗨,感谢您的回答,尝试了您的代码但得到一个空列表,打印(生日)>>>[]
    • @vitaliis:感谢您的指出。纠正了愚蠢的错误。
    • @KUNALDAS:立即尝试
    • 我对您的代码做了一些小改动。不客气。
    • @Sonali,感谢您的代码有效!为了获取今天的生日,我只是稍微修改了一下。生日 = driver.find_elements_by_xpath('//*[@id="events_dashboard_calendar_container"]/div/article[1]/div//a')
    猜你喜欢
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多