Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Selenium: How can I organize items with subitems within?

So I'm using Selenium (Firefox WebDriver) in Python to go to a link, login, then go to another page after login.

On this page is a grid of items. Each item has 3 subitems: link (the item is clickable), image, and title (like a description).

I'm able to pull all the items using a for-in loop into a variable. Then I just do all_items[:3] to get the first 3. However I can't figure out how to further narrow it by grabbing all_items[2] 's image only. Basically I want to organize it into a two-dimensional array if I'm understanding it correctly.

My end goal is to be able to do these outputs(not actual code):
Code
all_items[4][2] = https://example.com/image.png
all_items[4] = 'https://example.com/home.html', 'https://example.com/image.png', 'Welcome to Example!'

4 being item #4, 2 being the image subitem of the 3 (link, image, title)

I've been googling for a while. I've been working on this personal project for a bit now and I've made good progress for the little Python I know and I've managed to get a lot of progress thanks to researching. However, I'm stuck on this part and can't figure it out.

I have two versions I've been messing with but can't figure out how to go further.

V1 - I found similar code to this online and modified it to work for my use. This is the farthest I can get in terms of controlling the index. I'm able to call items[:3] for the first 3 items but I can't control the subitems.
Code
items = [{"Link": item.find_element_by_class_name('ember-view').get_attribute('href'), "Title": item.find_element_by_class_name('achievement-title').text, "Image": item.find_element_by_class_name('main-image').get_attribute('src')} for item in all_items]

V2 - This probably would work better than V1 but I can't figure out how to throw it into a two-dimensional array now. It does output it how I want but I can't control which item index it outputs.
Code
for item in all_items:
    item_link = [item.find_element_by_class_name('ember-view').get_attribute('href')]
    item_title = [item.find_element_by_class_name('achievement-title').text]
    item_image = [item.find_element_by_class_name('main-image').get_attribute('src')]
    print(item_link + item_title + item_image)

Any help would be greatly appreciated. Thanks a bunch!
[Image: support1.png]
Edit: noticed afterwards you wanted a 2 dimensional array(?), this is done with a multidimensional list

If I understood you correctly you might have:
PHP Code

<?php 
all_items 
= [["www.example.com/home.html", "www.example.com/image.png", "First example"], 
[
"www.example2.com/home.html", "www.example2.com/image2.png", "second example"], 
[
"www.example3.com/home.html", "www.example3.com/image3.png", "third example"]] 

To only get all the images (or sites/description just change the index):
PHP Code

<?php 
[i[1] for i in all_items] 

If you only want one specific image from the second example:
PHP Code

<?php 
[i[1] for i in all_items if i == all_items[1]] 
I'll test that out and see what I can get going, thanks for the help! Appreciate the examples.
[Image: support1.png]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Luffy-_-Aug 20, 2023 02:06 AM 12 446 Aug 20, 2023 02:06 AM
Last Post: Lucky4L
[Python] by Don CezarAug 20, 2023 01:37 AM 5 499 Aug 20, 2023 01:37 AM
Last Post: Lucky4L
by darkfantaOct 22, 2022 11:14 AM 6 366 Oct 22, 2022 11:14 AM
Last Post: darkfanta