Automating Poll Tweets in the New Layout Hellscape
Yes, the new Twitter layout is so o o bad, but it also broke my bot that uses Selenium to post polls (a weird missing part of the Twitter API).
Here are some new selectors that should be useful.
While I’m at it, I’ll explain a few pieces of rest of this slightly-trickier-than-usual bot. (I’m sure I borrowed most of this code from somewhere but I can’t remember where :(, sorry knowledge hole.)
This uses selenium and a few other standards, here’s all them parts
Parameter classes
A few classes to hold our parameters. The primary class here is TwitterLocator. To manipulate the web zone you need to know what to click on, what to scroll to, heck even what to type in. We use selenium’s By module and make tuples for each as class attributes. Some of these were pretty heinous to find after the homepage update, so they’ll probably change next week and ymmv.
PollBot itself
It starts innocently enough, loading a basic Chrome webdriver and loading the homepage. You’ll have to add your chromedriver to your path, eg. export PATH=$PATH:/path/to/chromedriver/folder. Uncomment the ‘–headless’ line if you don’t want it popping up on you.
The guts of the class uses the TwitterLocator class to navigate the site by overloading the __getattr__. We use a few WebDriverWaits to make sure the thing we’re looking for is on the page, and then find_element
We’ll chain together two methods and I guess quit too.
Login
So when we do things like .login() we just chain together a bunch of attribute calls - calling self.login_btn calls self.__getattr__(self, 'login_btn') - and selenium commands. We get pretty sleepy through all these methods because this bot doesn’t care about FAST POSTS and has bad internet.
Tweet poll
Once we’re logged in, go ahead and tweet the poll already. More of the same song and dance.