B
B
BelBES2016-03-05 01:08:47
Python
BelBES, 2016-03-05 01:08:47

How to get the text of an element using selenium in python?

Hello everyone,
I am writing a parser for issuing google in python using selenium.
I need to parse JSON like this:

{"cl":12,"cr":3,"id":"JAoZEfikv3d7yM:","isu":"moistimidtran.blogspot.com","ity":"jpg","oh":334,"ou ":" farm4.static.flickr.com/3239/2594001090_b5b9fb0ef6.jpg ","ow":500,"pt":"moistimidtran: people walking down the street","rid":"Hu_70STTJA3k4M","ru": " moistimidtran.blogspot.com/2010/11/people-walking-... ","s":"people walking down the street","th":183,"tu":" https://encrypted-tbn1 .gstatic.com/images?q\u003dt... ","tw":275}

I select all such elements with:
e = browser.find_elements_by_class_name('rg_meta')
Then I run through the list of all found and display the text:
for img in imgs:
    print img.text

but only empty lines are output.
Tried adding .encode('utf-8') after text but that didn't help.
Has anyone encountered this problem and knows how to overcome it? Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeniy _, 2016-03-12
@BelBES

import ast
from selenium import webdriver

wd = webdriver.Firefox()
wd.get('https://goo.gl/knrjOl')
all_pics = wd.find_elements_by_xpath("//div[@class='rg_meta']")
try:
    for pic in all_pics:
        a = ast.literal_eval(wd.execute_script("return arguments[0].innerHTML", pic))
        if "ou" in a.keys():
            print a["ou"]
finally:
    wd.close()
    wd.quit()

M
mipan, 2016-03-05
@mipan

I don't know which one is in python api. In java it looks like this:
upd:
look here - yizeng.me/2014/04/08/get-text-from-hidden-elements...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question