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?

Guy
  • 46,488
  • 10
  • 44
  • 88
Rana. Amir
  • 187
  • 3
  • 15

2 Answers2

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)
Bendik Knapstad
  • 1,409
  • 1
  • 9
  • 18
  • 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 Jan 30 '20 at 11:14
  • @Rana.Amir yes sorry i typed element when i ment driver. ive updated the code se if it works – Bendik Knapstad Jan 30 '20 at 11:16
  • 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()` – Rana. Amir Jan 30 '20 at 11:48
  • think you should do : `for element in new_elements: actionChains.context_click(element).perform()` – Bendik Knapstad Jan 30 '20 at 12:47
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)
Guy
  • 46,488
  • 10
  • 44
  • 88