Home > Mobile >  Python Selenium Index nested Elements
Python Selenium Index nested Elements

Time:01-08

I have 5

<ul >

and multiple <li > within each <ul >

I would like to index the 3rd rental-facilities class and 1st feature.text class

Ideally 1 line code.

Tried this but doesn't work:

rental = browser.find_elements_by_xpath("//ul[@class='rental-facilities']")[3]
feature = rental.find_elements_by_xpath("//li[@class='feature']")[1].text

Thank you.

CodePudding user response:

browser.find_elements_by_xpath() will return a list. lists are 0-based index, so these should work:

rental = browser.find_elements_by_xpath("//ul[@class='rental-facilities']")[2]
feature = rental.find_elements_by_xpath("//li[@class='feature']")[0].text

CodePudding user response:

feature = rental.find_elements_by_xpath(".//li[@class='feature']")[1].text

When xpathing you need a . when referencing from another element instead of driver.

  •  Tags:  
  • Related