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
CodePudding user response:
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)
CodePudding user response:
find_element
is to fetch the only single web element. where as find_elements
is to get list of web elements.
so instead of
results = driver.find_element_by_xpath("//ul[@class='firma-info']/li")
try this :
results = driver.find_elements_by_xpath("//ul[@class='firma-info']/li")
Now results with find_elements
is a list. Iterate it like this :
for res in results:
print(res.text)