Get href from anchor XPath(selenium python)


How can I get the XPath to get all the href of the products anchor on this page I want to get the href of the links that are the same as the below link. How can I retreive the href of the links that contains so the products links with Xpath and selenium. I will appreciate any help.

<a class="a-link-normal s-access-detail-page  s-color-twister-title-link a-text-normal" title="Under Armour Men's Tech Short Sleeve T-Shirt" href=";ie=UTF8&amp;qid=1516968485&amp;sr=1-4&amp;refinements=p_8%3A2229059011"><h2 data-attribute="Under Armour Men's Tech Short Sleeve T-Shirt" data-max-rows="0" class="a-size-base s-inline  s-access-title  a-text-normal">Under Armour Men's Tech Short Sleeve T-Shirt</h2></a>

Asked By: ryy77



find all a tag whose href starts with the url and get that href

//a[starts-with(@href, '')]/@href
Answered By: splash58

this should works

# selenium imports
from selenium import webdriver
from import By
from import WebDriverWait
from import expected_conditions as EC

LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Firefox()
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
    href = link.get_attribute('href')
    print href
Answered By: Sylvan LE DEUNFF