Today I am going to use Selenium to interact with a website by:
Opening a webpage
Logging in
Retrieving information from the page
As much as I’ve enjoyed using Wikipedia in my previous examples (and if you too are using Wikipedia please consider donating to them) today I am going to look at logging into WordPress.com.
geektechstuff.com is hosted on WordPress.com and every so often I like seeing how many daily visitors my site has. So I thought it would be cool if I created a Python program to do this for me.
stat = browser.find_element_by_css_selector(“#my-stats-content > div.card.stats- module.is-chart-tabs > ul > li:nth-child(2) > a > span.value”)
stat_value = stat.text
time.sleep(5)
print(“Total number of visitors so far today =”, stat_value)
browser.quit()
I’ve uploaded a video to show the function in action, please note the output on the terminal:
Selenium allows for elements to be selected by various functions including:
CSS Selector
XPath
ID
Link Text
This function uses a few different element method selections to get the task done, but you may be wondering how to find out where to get the details for these from. I’ve put a very brief video together to show how I accessed them via Safari’s Develop menu.
Related
Published by Geek_Dude
I'm a tech enthusiast that enjoys science, science fiction, comics and video games - pretty much anything geeky.
View all posts by Geek_Dude
You must be logged in to post a comment.