Python Forum
Feed List items with Integer - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Feed List items with Integer (/thread-33694.html)



Feed List items with Integer - euras - May-17-2021

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))])



RE: Feed List items with Integer - menator01 - May-17-2021

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



RE: Feed List items with Integer - euras - May-18-2021

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)



RE: Feed List items with Integer - snippsat - May-18-2021

(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?


RE: Feed List items with Integer - euras - May-18-2021

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.


RE: Feed List items with Integer - snippsat - May-18-2021

(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.


RE: Feed List items with Integer - euras - May-18-2021

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)



RE: Feed List items with Integer - snippsat - May-18-2021

(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 .


RE: Feed List items with Integer - euras - May-19-2021

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)



RE: Feed List items with Integer - snippsat - May-19-2021

(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