Algar Alg
Algar Alg

Reputation: 133

python specify css selector

The webpage have a code:

<table>
  <tbody>
   <tr>
    <td>
     <time data-timestamp="1458895194718" title="2016-03-25 11:39:54<small    class="milliseconds">.718</small>">11:39</time>
    </td>
    <td>
     <span class="invisep"><</span>
     <mark class="nickname" style="cursor:pointer;  color:#03DC03">usernickname</mark>
     <span class="invisep">></span>
    </td>

I need to get style= and I received advice to use:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.webdriver import FirefoxProfile


    colorelement = driver.find_element_by_css_selector('mark.nickname')
    color = colorelement.get_attribute('style')

and it works but my code returns me only the first value found. The web page have many blocks and everyone has block The code find_elements_by_css_selector returns "AttributeError: 'list' object has no attribute 'get_attribute'" Could you help me please, how can I get second(third etc.) value or maybe it can be found all the values at once

Upvotes: 1

Views: 875

Answers (1)

Florent B.
Florent B.

Reputation: 42518

You need to call get_attribute for each element:

elements = driver.find_elements_by_css_selector('mark.nickname')
for element in elements:
  print element.get_attribute('style')
  print color

Or with a list comprehension :

elements = driver.find_elements_by_css_selector('mark.nickname')
colors = [element.get_attribute('style') for element in elements]
for color in colors:
  print color

Upvotes: 1

Related Questions