简体   繁体   中英

python selenium xpath grabbing elements

I'm trying to grab some elements using selenium and python, but I'm having a hard time referencing the sub elements.

If you take a look at image 1 and at image 2 , I am trying to grab the dt and dd elements. My current code that I am playing around with is:

status = driver.find_elements_by_xpath("//div[@class='listview-info']")
for x in status:
    print(x.text)

I am using xpath and a for loop because there are 10 of these elements per page that I'm trying to grab. I'm unsure of how to grab the dt and dd tags. I've tried "//div[@dt]" with no luck.

You are trying to grab parent element ( //div[@class='listview-info'] ). Instead grab divs under it, and access dd and dt for each of them:

list = driver.find_elements_by_xpath("//div[@class='listview-info']/div")
for item in list: 
    print(item.find_element_by_xpath("./dd").text)
    print(item.find_element_by_xpath("./dt").text)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM