1
<li tabindex="0" role="tab" aria-selected="false">
   <a href="#gift-cards" class="leftnav-links kas-leftnav-links" data-section="gift-cards" data-ajaxurl="/wallet/my_wallet.jsp">                         
   <span class="width200 kas-gift-cards-tab">Gift Cards</span>  
   <span class="count kas-count">info</span>
   </a>
</li>

I have such a html code which is duplicated on a page about 5 times, and only 2 of these blocks have information I need. Their classes are the same and I don't know what to do. Plus, the execute_script in Firefox does not work for me.

html_list = driver.find_element_by_id("rewards-contents")
                items = html_list.find_element_by_tag_name("li")
                for item in items:
                    text = item.text
                    print(text)

I tried to crank it on python, but nothing sensible came out.

I expect the script to display info from all 5 blocks.

NarendraR
  • 7,577
  • 10
  • 44
  • 82
  • Possible duplicate of [Is there a way to find an element by attributes in Python Selenium?](https://stackoverflow.com/questions/28426645/is-there-a-way-to-find-an-element-by-attributes-in-python-selenium) – Aaron_ab Apr 10 '19 at 13:04
  • If you want all `li` elements use `find_elements`. Replace your code with `items = html_list.find_elements_by_tag_name("li")` – Sers Apr 10 '19 at 13:08

2 Answers2

2

To get all elements use find_elements instead of find_element. Your code should look like:

html_list = driver.find_element_by_id("rewards-contents")
items = html_list.find_elements_by_tag_name("li")
for item in items:
    print(item.text)

To get text by span elements:

html_list = driver.find_elements_by_css_selector("#rewards-contents li")
items = html_list.find_elements_by_tag_name("span")
for item in items:
    print(item.text)
Sers
  • 12,047
  • 2
  • 12
  • 31
  • Да, оказывается там у меня была ошибка но вместо info вывелось Gift Card что является не совсем тем что я ищу. Может быть можно к этой строке html_list.find_elements_by_tag_name("li") добавить ещё как то поиск по классу, если это возможно? И всё ещё открыт вопрос, почему в Firefox не работает стабильно execute_script? – Aleks Talibanskiy Apr 10 '19 at 13:15
  • Аттрибуты надо добавлять по необходимости, если не находит то что надо, можешь заменить на `find_elements_by_css_selector('li a[href="#gift-cards"]')`. Должен работать стабильно, что за скрипт? – Sers Apr 10 '19 at 13:16
  • driver.find_element_by_class_name("rewards-contents") items = html_list.find_elements_by_tag_name("span") for item in items: text = item.text print(text) HTML CODE:
  • Gift Cards info
  • Мне нужно вывести class="count kas-count" – Aleks Talibanskiy Apr 10 '19 at 13:20
  • Error: During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\xampps\htdocs\brute_kohls\checker.py", line 36, in html_list = driver.find_elements_by_css_selector('li a[href="#gift-cards"]').text AttributeError: 'list' object has no attribute 'text' Code: html_list = driver.find_element_by_class_name("rewards-contents") items = html_list.find_elements_by_css_selector('li a[href="#gift-cards"]') for item in items: text = item.text print(text) – Aleks Talibanskiy Apr 10 '19 at 13:32
  • Я плохо понимаю о чём вы, может покажете пример кода? Буду очень благодарен – Aleks Talibanskiy Apr 10 '19 at 13:42
  • Этот код должен печатать `span` элементы для каждого `li`: ```html_list = driver.find_elements_css_selector("#rewards-contents li") items = html_list.find_elements_by_tag_name("span") for item in items: print(item.text)``` – Sers Apr 10 '19 at 13:44
  • Error:During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\xampps\htdocs\brute_kohls\checker.py", line 36, in html_list = driver.find_elements_css_selector("#rewards-contents li") AttributeError: 'WebDriver' object has no attribute 'find_elements_css_selector' – Aleks Talibanskiy Apr 10 '19 at 14:20
  • Yes, should be find_elements_by_css_selector – Sers Apr 10 '19 at 16:13