如何从selenium webdriver元素中提取链接?

时间:2017-04-28 19:57:25

标签: python selenium parsing selenium-webdriver

我之前做了一个网站的解析器,现在我想为另一个https://999.md/ru/list/real-estate/apartments-and-rooms?view_type=photo做同样的事情,但我有一个问题。我想从以下代码中提取链接:

<ul class="ads-list-photo">   
    <li class="ads-list-photo-item"> 
       <div class="ads-list-photo-item-thumb">
         <a href="/ru/39854705">

我正在尝试使用selenium webdriver和以下代码:

driver.get(url)
driver.find_element_by_xpath('//*[@id="container"]/div/section/nav/ul/li[2]/a').click() # acces Imobiliare
driver.find_element_by_link_text("Apartamente şi camere").click()

parentElement = WebDriverWait(driver, 10)
parentElement = parentElement.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="js-pjax-container"]')))
elementList = parentElement.find_elements_by_tag_name("li") #Acces lista cu obiecte

links = []

for element in driver.find_elements_by_xpath('//*[@id="js-ads-container"]/ul/li[1]/div[1]'):
    links.append(element.get_attribute('href'))
links

但不是获得链接的alit,而是获得None值列表。怎么解决?

1 个答案:

答案 0 :(得分:3)

我想你可以使用:

driver = webdriver.Firefox()
driver.get("https://999.md/ru/list/real-estate/apartments-and-rooms?view_type=photo")
links = driver.find_elements_by_xpath("//div[@class='ads-list-photo-item-thumb']//a")

for x in links:
    print x.get_attribute('href')

<强>输出:

https://999.md/ru/40057219
https://999.md/ru/22587357
https://999.md/ru/38650049
https://999.md/ru/17528096
https://999.md/ru/31246607
https://999.md/ru/12459330
https://999.md/ru/8449720
https://999.md/ru/34011321
...