Home > Blockchain >  Selenium - Web page returns a "redirecting" link insted of original link with get_attribut
Selenium - Web page returns a "redirecting" link insted of original link with get_attribut

Time:03-25

I'm trying to get a link from a page using Selenium. When checking the page's source code I can clearly see the original link, but when I use Selenium to select the element, and then use element.get_attribute('href'), the link that it returns is a different one.

# Web page url request
driver.get('https://www.facebook.com/ads/library/?active_status=all&ad_type=all&country=BR&q=myshopify&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=keyword_unordered&media_type=all')
driver.maximize_window()
time.sleep(10)

v_link = driver.find_element(By.XPATH, '//*[@id="facebook"]/body/div[5]/div[2]/div/div/div/div/div[3]/span/div[1]/div/div[2]/div[1]/div[2]/div[3]/a')
  print(v_link.get_attribute('href'))

The actual link that I need: https://bhalliproducts.store/?_pos=1&_sid=8a26757f5&_ss=r

The link being returned: https://l.facebook.com/l.php?u=https://bhalliproducts.store/?_pos=1&_sid=8a26757f5&_ss=r&h=AT3KkXQbOn5s3oaaaCV2vjaAnyJqEqkIlqvP16g3eCsCnw-fx3VCNMR66_Zxs50v9JU5JK2DLABhoBHRNHQENH6oyp39Pho2Z6o25NZD5RIvl5kMow0lfd2rdaUWp11e6alEJFtoJp0X_uXgp5B2OYocRg5wGA

CodePudding user response:

You can use the following solution:

from urllib.parse import unquote

href = "https://l.facebook.com/l.php?u=https://bhalliproducts.store/?_pos=1&_sid=8a26757f5&_ss=r&h=AT3KkXQbOn5s3oaaaCV2vjaAnyJqEqkIlqvP16g3eCsCnw-fx3VCNMR66_Zxs50v9JU5JK2DLABhoBHRNHQENH6oyp39Pho2Z6o25NZD5RIvl5kMow0lfd2rdaUWp11e6alEJFtoJp0X_uXgp5B2OYocRg5wGA"

begin = href.find('=')   1 
end = href.find('&')

href = href[begin:end]
href = unquote(href)
print(href)
  • Related