-1

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)

Heres the site and the list data I want to extract

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 

2 Answers 2

1

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)
0

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)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.