简体   繁体   中英

Understanding what find_elements_by_xpath returns in Selenium

This is my code:

mydata = driver.find_elements_by_xpath('/html/body/section/div/div/div[1]/div[4]/div/table/tbody[1]/tr/td[3]')
print(mydata)

It returns:

[<selenium.webdriver.remote.webelement.WebElement (session="70471ea3d46485ab51b62586ffa7ff21", element="4877016a-a335-47f7-b46d-52dbc3604dce")>].

How do I understand this result?

Try this:

elements = driver.find_elements_by_xpath('/html/body/section/div/div/div[1]/div[4]/div/table/tbody[1]/tr/td[3]')
for element in elements:
    print(element) # prints element
    print(element.id) # prints the id of element
    print(element.text) # prints the text of element
    print(element.size) # prints the size of element
    print(element.location) # prints the location of element
    print(element.parent) # prints the text of element

When you use driver.find_elements_by_xpath() , as the name implies, you get multiple elements (stored in a list). To investigate all elements, you need to loop through all of them using for element in elements .

To find out what kind of information you can get (by accessing its attributes or calling its methods), you can check the docs , eg, id , location , tag_name , size , text .

mydata = driver.find_elements_by_xpath('/html/body/section/div/div/div[1]/div[4]/div/table/tbody[1]/tr/td[3]')
# this will print the text written within tags
print(mydata.text)

mydata is an object and you can perform opeation on this, like get text or get parent, send keys etc.

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