Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get links from a certain div using Selenium in Python

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

<div class="rec_view">
    <a href='www.xyz.com/firstlink.html'>
        <img src='imga.png'>
    </a>
    <a href='www.xyz.com/seclink.html'>
        <img src='imgb.png'>
    </a>
    <a href='www.xyz.com/thrdlink.html'>
        <img src='imgc.png'>
    </a>
</div>

I want to get all the links that are present on the rec_view div. So those links that I want are,

www.xyz.com/firstlink.html
www.xyz.com/seclink.html
www.xyz.com/thrdlink.html

Here is the Python code which I tried with

from selenium import webdriver;
webpage = r"https://www.testurl.com/page/123/"
driver = webdriver.Chrome("C:\chromedriver_win32\chromedriver.exe")
driver.get(webpage)
element = driver.find_element_by_css_selector("div[class='rec_view']>a")
link = element.get_attribute("href")
print(link)

How can I get those links using selenium on Python?

like image 886
Ronald Avatar asked Dec 08 '25 06:12

Ronald


1 Answers

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

like image 59
undetected Selenium Avatar answered Dec 09 '25 21:12

undetected Selenium