Issue
I’m trying to build a data extract for my company and I need to get the data inside a html list but when I execute my code I only get the text of a single li
element below I shared my code.
screenshots of the site source and my result.
My code:
from selenium import webdriver
driver = webdriver.Safari()
driver.get("http://turkosb.com/kaynarca-mobilya-ihtisas-organize-sanayi-bolgesi.html")
results = driver.find_element_by_xpath("//ul[@class='firma-info']/li")
print(results.text)
But despite my efforts the result I get is the same I only get the text from first li element like this:
YETKİLİ KİŞİ: Fikret ARSLAN
Solution
Its because driver.find_element_by_xpath
is returning first element. It may highlight to all the li
in the DOM, but you are asking to find a single element and it returns the first one.
Use find_elements
to find all the li
tags and iterate to get the text from them.
results = driver.find_elements_by_xpath("//ul[@class='firma-info']/li")
for result in results:
print(result.text)
Answered By – pmadhu
This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0