Python Forum
Feed List items with Integer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Feed List items with Integer
#1
I try to get a list item by feeding it with Int variable, but cannot find a way to do it.
# this one works
cars = ["Audi", "Opel", "BMW"]
for x,y in enumerate(cars):
    temp = int(x)
    print(cars[temp])
But when I try to feed an item directly with a variable, I get an error. I want to use direct method in WebAutomation (looping and picking elements in HTML).
# this one not
cars = ["Audi", "Opel", "BMW"]
for x,y in enumerate(cars):
    print(cars["{}".format(int(x))])
Error:
Exception has occurred: TypeError list indices must be integers or slices, not str File "C:\Users\stony\OneDrive\Stalinis kompiuteris\Python\smeliodeze.py", line 19, in <module> print(cars["{}".format(int(x))])
Reply
#2
This works
cars = ['Audi', 'Opal', 'BMW']

for x, y in enumerate(cars):
    print(f'{cars[x]}')
also this

cars = ['Audi', 'Opal', 'BMW']

for x, y in enumerate(cars):
    print(cars[x])
Output:
Audi Opal BMW
I welcome all feedback.
The only dumb question, is one that doesn't get asked.

My Scripts
CookBook - Shmup - PyQt5 Music Player


Reply
#3
but how to do that when interacting with Selenium in webautomation? let's say I want to loop throug a list of elements and click on each element in the list. In that case I need to feed a index in an element path, but I cannot find a way to do it.
contents = driver.find_element_by_id("searchResultTable").find_elements_by_tag_name("tr")
for inda, content in enumerate(contents):
    print(driver.find_element_by_id('searchResultTable').find_elements_by_tag_name('tr')[f'{inda}'].find_elements_by_tag_name('td')[7].text)
Reply
#4
(May-18-2021, 09:22 AM)euras Wrote: but how to do that when interacting with Selenium in webautomation? let's say I want to loop throug a list of elements and click on each element in the list.
Have you look at my answer it your other Thread?
Reply
#5
no, that method does not work, since the browser zoom can be set differently, I need just to feed somehow the index into element path, to make it work, and run
driver.execute_script
method.
Reply
#6
(May-18-2021, 10:18 AM)euras Wrote: I need just to feed somehow the index into element path, to make it work, and run
Then you can not do it like this.
.find_elements_by_tag_name('tr')[f'{inda}']
find_elements_by_tag_name('tr') in a loop return a webelement that can not be access bye referring to a index number[1]
.find_element_by_css_selector can be used to as showed in other Thread can be used to insert a index number.
If use find_elements_by_tag_name('tr') not in a loop so get all element,then can get use elements[1] on that list return.
Reply
#7
ok, I tried it with CSS selector (took full Xpath), but it still does not work :/
contents = driver.find_element_by_id("searchResultTable").find_elements_by_tag_name("tr")
for inda, content in enumerate(contents):
    temp_el = driver.find_element_by_css_selector(">html>body>div[1]>div>div>div>div[2]>div>div[1]>div[3]>div[2]>table>tbody>tr[{inda}]>td[8]").text
    print(temp_el)
Reply
#8
(May-18-2021, 02:17 PM)euras Wrote: ok, I tried it with CSS selector (took full Xpath), but it still does not work :/
There is no f then there is just a string and not using f-string.
The basic of this wrong and right.
>>> cars = ["Audi", "Opel", "BMW"]
# Just a string nothing happens 
>>> for inda, content in enumerate(cars):
...     print('tr[{inda}]>td[8]{content}')
...     
tr[{inda}]>td[8]{content}
tr[{inda}]>td[8]{content}
tr[{inda}]>td[8]{content}
>>> 
>>> # Right way
>>> for inda, content in enumerate(cars):
...     print(f'tr[{inda}]>td[8]{content}')     
...     
tr[0]>td[8]Audi
tr[1]>td[8]Opel
tr[2]>td[8]BMW
And you CSS selector can not start with > as is not a valid selector.
Look at CSS Selector Reference .
Reply
#9
thank you snippsat for your help! But I don't get it how to place that in an actual call for element. How to write that syntax on the following example?
contents = driver.find_element_by_id("searchResultTable").find_elements_by_tag_name("tr")
for inda, content in enumerate(contents):
   temp_el = driver.find_element_by_css_selector(f'html>body>div[1]>div>div>div>div[2]>div>div[1]>div[3]>div[2]>table>tbody>tr[{inda}]>td[8]')
   print(temp_el.text)
Reply
#10
(May-19-2021, 07:30 AM)euras Wrote: thank you snippsat for your help! But I don't get it how to place that in an actual call for element. How to write that syntax on the following example?
As don't now content of contents ,here is example from other Thread taken step further.
I use BS as it's easier to show than Selenium,CSS selector usage would be the same
So generate new selectors to take out first name,so a working example of similar task that you are trying to do.
from bs4 import BeautifulSoup

html = '''\
<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>'''

soup = BeautifulSoup(html, 'lxml')
contents= soup.select('tr')
for inda, content in enumerate(contents, 1):
    print(soup.select_one(f'tr:nth-child({inda}) > td:nth-child(1)').text)
Output:
Firstname Jill Eve
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  For Word, Count in List (Counts.Items()) new_coder_231013 6 736 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 446 May-26-2022, 01:37 PM
Last Post: Mark17
  how to assign items from a list to a dictionary CompleteNewb 3 718 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  How to parse a live feed in Python? Daring_T 2 994 Jan-20-2022, 04:17 AM
Last Post: Daring_T
  Reading list items without brackets and quotes jesse68 6 1,543 Jan-14-2022, 07:07 PM
Last Post: jesse68
Question How to gather specific second-level items from a list chatguy 2 809 Dec-17-2021, 05:05 PM
Last Post: chatguy
  deleting select items from a list Skaperen 13 2,458 Oct-11-2021, 01:02 AM
Last Post: Skaperen
  Getting All Items From A List knight2000 4 1,327 Sep-25-2021, 12:56 AM
Last Post: knight2000
  Change a list to integer so I can use IF statement buckssg 3 1,329 Sep-21-2021, 02:58 AM
Last Post: bowlofred
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 1,655 May-24-2021, 08:48 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020