Skip to content Skip to sidebar Skip to footer

Want To Get The Text From The Li Tag Using Selenium

I want the text from the li tag that is the specification of the product but when i am searching using driver.find_element_by_css_selector it gives the error as path cannot find

Solution 1:

There are anti-scraping measures. If those do not affect you then you can use css classes to target the li elements to loop over, and the title/values for each specification:

specs = [(i.find_element_by_css_selector('.key-title').text, i.find_element_by_css_selector('.key-value').text) for i in driver.find_elements_by_css_selector('.key-li')]

You can also regex that required info from a script tag and just use requests and json to parse out the spec (there is other info including reviews included in data)

import re, json

r = requests.get("https://www.lazada.sg/products/samsung-galaxy-watch3-bt-45mm-titanium-i1156462257-s4537770883.html?search=1&freeshipping=1",
                 headers = {'User-Agent':'Mozilla/5.0'})
html = r.text
#html = driver.page_source

data = json.loads(re.search(r'var __moduleData__ = (.*);', html).group(1))
print(data['data']['root']['fields']['specifications'])

Post a Comment for "Want To Get The Text From The Li Tag Using Selenium"