1

I'm facing a issue when I try to get element inside of an element. I tried 2 methods to achieve this but unfortunately didn't get success.

element_list = driver.find_elements_by_xpath("//div[@class='WGGO WKFO']//ul//li[@class='WO0F WHHO WI5 WF2F']//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']")

above code get all the list of DOM elements list.

Method 1:

for element in element_list:
    element_text = element.find_element_by_xpath("//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']").text
    print(element_text)

when I tried this it always return me first element's text instead of respective element text.

while searching I found a link where someone asked same as I want.

Selenium Webdriver finding an element in a sub-element

when I tried p0deje's Answer it gives me blank array.

element_text = element.find_elements_by_xpath(".//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']").text

Result: []

please suggest me what can I do to achieve this, How can I get an element detail inside an element?

2 Answers 2

1

the problem with your code on this line: element_text = element.find_elements_by_xpath(".//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']").text ist that find elements retunes a list, and a list has no .text attribute. try iterating over the returned list:

elements = driver.find_elements_by_xpath("//div[@class='WGGO WKFO']//ul//li[@class='WO0F WHHO WI5 WF2F']//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']")
#as a list comprehention
element_text= [element.text for element in elements]
#or as a for loop
element_text=[]
for element in elements:
    element_text.append(element.text)
Sign up to request clarification or add additional context in comments.

4 Comments

even this line of code returns also a empty array elements = element.find_elements_by_xpath(".//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']")
@Rana.Amir yes sorry i typed element when i ment driver. ive updated the code se if it works
it's working fine but when I want to right click on element instead of text it always clicked on first element. :( elements = driver.find_elements_by_xpath("//div[@class='WGGO WKFO']//ul//li[@class='WO0F WHHO WI5 WF2F']//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']") new_elements= [element for element in elements] for element in new_elements: action = ActionChains(driver) action.move_to_element(element).perform(); action.context_click().perform()
think you should do : for element in new_elements: actionChains.context_click(element).perform()
1

element_list already holds all the elements you want to print, i.e. the element_text. You can't use WebElement to locate itself, the reason it didn't fail with NoSuchElementException is that when using xpath to locate element from another element you need to specify current context .

find_element_by_xpath('.//div')

Iterate over element_list

for element in element_list:
    print(element.text)

Or if you really want to split it because you have more actions on the parent list you can use (note she shortened element_list xpath)

element_list = driver.find_elements_by_xpath("//div[@class='WGGO WKFO']//ul//li[@class='WO0F WHHO WI5 WF2F']")
for element in element_list:
    element_text = element.find_element_by_xpath(".//div[@class='WP0F WN0F']//div[@class='WDQY']//ul[@class='WHDR WCQY']").text
    print(element_text)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.