Get links from a certain div using Selenium in Python

前端 未结 1 1127
渐次进展
渐次进展 2021-01-07 04:42

I have the following HTML page. I want to get all the links inside a specific div. Here is my HTML code:


                      
相关标签:
1条回答
  • 2021-01-07 05:03

    As per the HTML you have shared to get the list of all the links that are present on the rec_view div you can use the following code block :

    from selenium import webdriver
    
    driver = webdriver.Chrome(executable_path=r'C:\chromedriver_win32\chromedriver.exe')
    driver.get('https://www.testurl.com/page/123/')
    elements = driver.find_elements_by_css_selector("div.rec_view a")
    for element in elements:
        print(element.get_attribute("href"))
    

    Note : As you need to collect all the href attributes from the div tag so instead of find_element_* you need to use find_elements_*. Additionally, > refers to immediate <a> child node where as you need to traverse all the <a> child nodes so the desired css_selector will be div.rec_view a

    0 讨论(0)
提交回复
热议问题