How to extract all the texts from tag using Selenium through Python

前端 未结 3 2007
旧时难觅i
旧时难觅i 2020-12-22 06:38

Here is the link of website from where I want to extract data, I\'m trying to get all text of href attribute under anchor tag. Here is the sample html:

3条回答
  •  执念已碎
    2020-12-22 07:17

    If you need all links values you should be using find_elements_.... functions, not find_element_... functions as the latter one will return you first single match.

    Recommended update for your code:

    driver.get("http://www.carboline.com/products/")
    for link in driver.find_elements_by_xpath("//ul[@id='productList']/descendant::*/a"):
        if link.is_displayed():
            print(link.text)
    

    More information:

    • find_elements_by_xpath(xpath)
    • Locating Elements
    • XPath Tutorial
    • XPath Operators & Functions
    • Python If ... Else
    • Python For Loops

提交回复
热议问题