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.
Gaming Collection
Homepage
my-python
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
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 271 May-24-2021, 08:48 AM
Last Post: bowlofred
  Parse String between 2 Delimiters and add as single list items lastyle 5 399 Apr-11-2021, 11:03 PM
Last Post: lastyle
  Sum of list items tristanfermat 2 436 Feb-06-2021, 12:04 PM
Last Post: tristanfermat
  splitting UAV/sat images to smaller pieces in order to feed a CNN hobbyist 0 344 Dec-08-2020, 11:48 AM
Last Post: hobbyist
  Count number of occurrences of list items in list of tuples t4keheart 1 459 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  concatenating 2 items at a time in a python list K11 3 488 Oct-21-2020, 09:34 AM
Last Post: buran
  Rotation Effect on live Webcam Feed Leziiy 0 379 Sep-12-2020, 04:25 PM
Last Post: Leziiy
  Select the other of 2 items in a list Clunk_Head 7 900 Sep-01-2020, 05:27 PM
Last Post: Clunk_Head
  Removing items from list if containing a substring pythonnewbie138 2 642 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Print the number of items in a list on ubuntu terminal buttercup 2 685 Jul-24-2020, 01:46 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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