从锚点 XPath(selenium python) 获取 href
Get href from anchor XPath(selenium python)
如何获取 XPath 以获取此页面上锚定产品的所有 href https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011. 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 https://www.amazon.com/,以便产品链接到 Xpath 和 selenium。我将不胜感激任何帮助。
<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="https://www.amazon.com/Shortsleeve-T-Shirt-Under-Armour-Midnight/dp/B00783KT9Y/ref=sr_1_4?s=sports-and-fitness-clothing&ie=UTF8&qid=1516968485&sr=1-4&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>
找到 href 以 url 开头的所有标签并获取该 href
//a[starts-with(@href, 'https://www.amazon.com/')]/@href
这应该有效
# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Firefox()
browser.get('https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
href = link.get_attribute('href')
print href
如何获取 XPath 以获取此页面上锚定产品的所有 href https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011. 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 https://www.amazon.com/,以便产品链接到 Xpath 和 selenium。我将不胜感激任何帮助。
<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="https://www.amazon.com/Shortsleeve-T-Shirt-Under-Armour-Midnight/dp/B00783KT9Y/ref=sr_1_4?s=sports-and-fitness-clothing&ie=UTF8&qid=1516968485&sr=1-4&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>
找到 href 以 url 开头的所有标签并获取该 href
//a[starts-with(@href, 'https://www.amazon.com/')]/@href
这应该有效
# selenium imports
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
LINKS_XPATH = '//*[contains(@id,"result")]/div/div[3]/div[1]/a'
browser = webdriver.Firefox()
browser.get('https://www.amazon.com/s/ref=lp_11444071011_nr_p_8_1/132-3636705-4291947?rh=n%3A3375251%2Cn%3A%213375301%2Cn%3A10971181011%2Cn%3A11444071011%2Cp_8%3A2229059011')
links = browser.find_elements_by_xpath(LINKS_XPATH)
for link in links:
href = link.get_attribute('href')
print href