Dani M
Dani M

Reputation: 1201

Get link when clicked in an image

This is the image where, when clicked, user is redirected to another page.

<div class="lis_el          " id="cel_lisimg_18755" onclick="lis_mostrarficha(0);">

    <div class="lis_elc ">
        <div class="lis_eloverflow">
            <div class="lis_elc_img">
                <div class="lis_elc_imgc"><img class="lis_elc_img_img" id="lisimg_18755" src="https://sgfm.elcorteingles.es/SGFM/dctm/MEDIA03/201705/29/0280282401564764342_1_.jpg">
                </div>
            </div>
        </div>
        <div class="lis_info  ">

                            <div class="clear"></div>

            <div class="lis_info_precio">


                            5<span class="lis_info_preciop">,99€</span>

            </div>
            <h2>Camiseta flame</h2>
            <div class="lis_mascol displaynone" id="lis_mascol18755" style="display: block;">+ Colores</div>
        </div>
    </div>
</div>

I'm tryin to obtain that link using Selenium in Python, but I don't know where I can obtain it from. I noticed this however, which I suppose this function does the redirection:

onclick="lis_mostrarficha(0);

I don't have much experience in web developing so I'm not sure how I can obtain that link without clicking, as this would take too long.

Thanks,

Upvotes: 0

Views: 423

Answers (1)

Ron Norris
Ron Norris

Reputation: 2690

You will have to perform the click event in this case because the HTML does not contain the URL linked to the image -- it calls a script. What can be done is to use Selenium to click the element that contains the onclick event.

from selenium.webdriver.chrome.options import Options
from selenium import webdriver

options = Options()
options.add_argument('--disable-infobars')
driver = webdriver.Chrome(chrome_options=options)

div = find_element_by_id('cel_lisimg_18755')
div.click()

# Then wait for the page to load

# Get the URL
url = driver.current_url
print(url)  # Assumes v3 python

Upvotes: 1

Related Questions