iman_sh77
iman_sh77

Reputation: 77

parse directly on pure html source with selenium in python

I'm trying to test the selenium program that I wrote by giving it an HTML source as a string for some reasons such as speed. I don't want it to get the URL and I don't want it to open a file I just want to pass it a string that contains whole DIV part of that site and do parsing stuff on it. this is part of a module that i wrote:

source = driver.page_source
return {'containers': source}

and in another module,

def get_rail_origin(self):
    return self.data['containers'].find_element_by_id('o_outDepName')...

I'm trying to do parsing stuff on it but I get

AttributeError: 'str' object has no attribute 'find_element_by_id'

So how can I parse on pure HTML source without opening any file or URL

Upvotes: 0

Views: 122

Answers (1)

Andersson
Andersson

Reputation: 52665

Selenium works with live HTML DOM. If you want to get source and then parse it, you can try, for instance, lxml.html:

def get_rail_origin(self):
    source = html.fromstring(self.data['containers'])
    return source.get_element_by_id('o_outDepName')

P.S. I assumed that self.data['containers'] is HTML source code

Upvotes: 1

Related Questions