Dec-16-2018, 11:25 PM
Background: I am a C++ programmer learning Python. I'm to the stage where I can write simple utilities with some tutorial pages open 
I thought I would try writing a script that would post Tweets to my Twitter account, with attached images. I initially thought I would use the Twitter API, which would be simpler, but I decided I wanted to learn more general browser automation, and I didn't want to go through the 5-day process of getting developer access to the Twitter API.
Well, looks like the general non-API way of doing things is harder... However, I want some friends to be able to use my completed script without having to go through the developer application process.
I figured out how to install selenium and the Chrome web driver.
Currently, I have figured out how to log in to my account, and make a tweet. But I am stuck on the problem of how to attach an image. It's easy enough to do interactively, but I want the script to run unattended. On the "Compose New Tweet" dialog, there is an "insert image" button. I can get to that with Python, but when I click on it, it creates a Windows file choice dialog, which I have no idea how to control.
Here's what I have worked out so far (BTW, any suggestions on how to do this better or simpler are welcome):

I thought I would try writing a script that would post Tweets to my Twitter account, with attached images. I initially thought I would use the Twitter API, which would be simpler, but I decided I wanted to learn more general browser automation, and I didn't want to go through the 5-day process of getting developer access to the Twitter API.
Well, looks like the general non-API way of doing things is harder... However, I want some friends to be able to use my completed script without having to go through the developer application process.
I figured out how to install selenium and the Chrome web driver.
Currently, I have figured out how to log in to my account, and make a tweet. But I am stuck on the problem of how to attach an image. It's easy enough to do interactively, but I want the script to run unattended. On the "Compose New Tweet" dialog, there is an "insert image" button. I can get to that with Python, but when I click on it, it creates a Windows file choice dialog, which I have no idea how to control.
Here's what I have worked out so far (BTW, any suggestions on how to do this better or simpler are welcome):
##################################################### # Local variables tweettext = "A tweet from my list of tweets" imagefile = "<Full pathname of an image file on my PC>" chromedriverpath = "<Full pathname of the downloaded Chrome driver>" accountname = '<MyTwitterAccountHandle>' pw = '<MyLongRandomPassword>' ##################################################### from selenium import webdriver browser = webdriver.Chrome(chromedriverpath) browser.get('https://twitter.com/login') # find username field for login elem = browser.find_element_by_class_name('js-username-field') elem.send_keys(accountname) # find password field elem = browser.find_element_by_class_name('js-password-field') elem.send_keys(pw) # find login button & complete the login elem = browser.find_element_by_xpath('//*[@id="page-container"]/div/div[1]/form/div[2]/button') elem.click() # click new tweet elem = browser.find_element_by_id('global-new-tweet-button') elem.click() # find tweet box & enter the next tweet tweetbox = browser.find_element_by_xpath('//*[@id="Tweetstorm-tweet-box-0"]/div[2]/div[1]/div[2]/div[2]/div[2]/div[1]') tweetbox.send_keys(tweettext) # Add image. First find the image insertion button & click it. imagebutton = browser.find_element_by_xpath('//*[@id="Tweetstorm-tweet-box-0"]/div[2]/div[2]/div[1]/span[1]/div/div/label/input') imagebutton.click() ##################################################### # TODO # At this point, a Windows file dialog comes up. Now what?