【问题标题】:Selenium iterating through unordered list but not getting the right textSelenium 遍历无序列表但没有得到正确的文本
【发布时间】:2021-04-20 19:09:41
【问题描述】:

我正在使用 selenium 编写一个 craigslist 搜索机器人,并且我正在尝试遍历无序列表中的搜索结果。它正在迭代但没有得到我想要的链接文本。有人有什么想法吗?

browser.get('https://losangeles.craigslist.org/search/sfv/sss?')

searchForm = browser.find_element_by_id("query")
searchForm.click()

searchForm.send_keys(whatWant)
sendClick = browser.find_element_by_class_name("searchbtn")
sendClick.click()

ulSelector = browser.find_element_by_class_name("rows")
liClass = ulSelector.find_elements_by_tag_name("li")
for links in liClass:
    print(links.get_attribute('href'))

我得到了输出

None
None
None
None
None
None
None
None
[many more Nones - truncated]

我知道它正在迭代,但没有得到正确的文本。

【问题讨论】:

    标签: python html css selenium


    【解决方案1】:
    for links in liClass:
        print(links.find_element_by_xpath('./a').get_attribute('href'))
    

    获取 a 标签而不是 li 标签以从中获取 ahref 值。

    【讨论】:

    • 非常感谢阿伦迪普!
    猜你喜欢
    • 2015-04-15
    • 2015-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 2018-06-02
    • 2020-03-25
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多