Home > Blockchain >  Iterating through different fieldsets in a form using Selenium Python
Iterating through different fieldsets in a form using Selenium Python

Time:05-27

I am trying to extract the title of videos saved on my webserver. They're saved under a form and the form has different number of field sets that are subject to change depending on how many videos there are. I am looking to iterate through all of the available field sets and extract the title of the video which I've highlighted in the attached picture.I don't know how many sets there will be so I was thinking of a loop to go through the length(fieldsets) however my unfamilarity with web scraping/dev has left me a little confused.

I've tried a few different things such as:

results = driver.find_elements(BY.XPATH, "/html/body/div[2]/form/fieldset[1]")

However I am unsure how to extract the attributes of the subelements via Selenium.

Thank you

MY webserver HTML page

CodePudding user response:

If you have so many videos as per shown in the images and you need all videos then you have to iterate through all videos by the given below code.

all_videos_title = []
all_videos = driver.find_elements_by_xpath('//fieldset[@]')
for sl_video in all_vides:
    sl_title = sl_video.find_element_by_xpath("//a//@title")
    all_videos_title.append(sl_title)
print(all_videos_title)

If any other anchor tag "a" in the "fieldset" tag then in your loop XPath changed. So If It's not working then you have to share the whole page source so I'll provide exact details of the data that you need.

CodePudding user response:

Please try below XPath

//form//ancestor::fieldset//@title
  • Related