Page 1 of 1 1
Topic Options
#213805 - 2020-01-28 03:54 PM Python/Selenium/HTML
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4499
Loc: USA
Hey Guys, completely off topic, but I'm hoping someone could get me in the right direction. I've started using Python and Selenium to web scrape pages, to get bits of information for a larger script. I had been doing this with the GetPage() function but with IE becoming more and more old in the tooth, and with being able to use Chrome/FF in headless mode, I started trying to figure out the process of getting those data points a new way. I've gotten a bunch of them figured out, but I am struggling with one that is probably easy.

Here is the relevant html from the website:
 Quote:
<html class="windows" lang="en" current-version="x.x.x" stable-versions="x.x.x">


I need to get the current-version value. The old way I would have done this was to get the entire html source from GetPage, and then parse it. This works, but with Selenium you have access to the html structure.

A simple example is:

 Quote:

browser.get("https://www.libreoffice.org/download/download/")
x=browser.find_element_by_class_name("dl_version_number").text
print(x)


The other find methods are: (https://selenium-python.readthedocs.io/locating-elements.html)
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector

So my question is... What method do I use to read that attribute value?

Top
#213807 - 2020-01-28 05:12 PM Re: Python/Selenium/HTML [Re: Allen]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4499
Loc: USA
OMG... finally found it. \:\)

x=browser.find_element_by_tag_name("html").get_attribute("current-version")

Top
#213812 - 2020-01-30 01:49 PM Re: Python/Selenium/HTML [Re: Allen]
BradV Offline
Seasoned Scripter
****

Registered: 2006-08-16
Posts: 681
Loc: Maryland, USA
Just a thought, but have you tried using curl for windows? Curl is designed to be used from the command line/script. So, might be a lot easier to use?
Top
#213813 - 2020-01-30 06:16 PM Re: Python/Selenium/HTML [Re: BradV]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4499
Loc: USA
Never used curl, and will certainly look at it. However, now that I have invested so much time learning how to use Selenium, I'm going to commit to writing up some of the things I've learned. I want to add the biggest reason for me going down the Selenium road was trying to get the html of pages generated with js or ajax. Most of the methods I tried would only return the html pre js/ajax. I read about PhantomJS, which does return the complete html, but then I kept reading that it was not being maintained anymore. Finally, my research got me in the direction of Chrome/Firefox in headless mode. I never wanted anything more than the page html source, but with Selenium, not only can you get the page source, you can also find almost anything in the html (parse it) very easily. The hard part was learning HOW to do it. Now that I've done that, I'm incorporating as much of it in kix as I can. \:\)

When I have time, I'll leave more details and examples.

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 45 anonymous users online.
Newest Members
LayneSA, Mavack, xiripot, tspirtle, Safio
17719 Registered Users

Generated in 0.031 seconds in which 0.011 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org