user2492364
user2492364

Reputation: 6703

selenium catch all the link with css selector result

I practice how to use selenium and it can catch the first link.
But I hope it can catch all the link

Because the structure is the same,it should catch all links

<div class='dah'>
   <section>
       <div class='media'><a href='http://www.test.tw/event/22'></a>
   <section>
   <section>
       <div class='media'><a href='http://www.test.tw/event/23'></a>
   <section>
   <section>
       <div class='media'><a href='http://www.test.tw/event/24'></a>
   <section>
   <section>
       <div class='media'><a href='http://www.test.tw/event/25'></a>
   <section>
 <div>

How can I do?

Here is my code in terminal:

>>> from selenium import webdriver
>>> d = webdriver.Firefox()
>>> d.get('http://www.test.com/')
>>> next = d.find_element_by_css_selector("div.hah section div.media a")
>>> next.get_attribute("href")    
u'http://www.test.tw/event/22'

Upvotes: 1

Views: 752

Answers (1)

falsetru
falsetru

Reputation: 369164

Use find_elements_by_css_selector (element s): (Selenium provides find_element_* and find_elements_*)

elements = d.find_elements_by_css_selector("div.hah section div.media a")
[a.get_attribute("href") for a in elements]   

Upvotes: 1

Related Questions