Forum Archive

Selenium for Pythonista

jl

I'm sorry if this is obvious or been covered before, but I couldn't find it in the forum. Is it possible to use selenium with Pythonista?

I'm very new to Python, and using Pythonista to learn in the few free minutes here and there during the day. Most of the applications I want to do with Python involve automation of tasks on various websites. If there is some other module or way to do this other than selenium, I'd be interested in that too - for various reasons, it would be a big help if I could do these automated tasks directly from iOS.

Thanks, and Pythonista is absolutely great! I have some previous coding experience, but there is no way I would have time to learn Python without being able to work on the iPhone. Keep up the great work!

Webmaster4o

As I understand it, selenium directly controls an actual browser like Firefox running in the same computer. I'd reccommend mechanize for browser automation with pythonista.

dgelessus

Isn't Selenium basically a way to remote-control browsers from Python? I don't know how exactly Selenium does that, but most likely iOS Safari is not one of the supported browsers. If the communication with the browser is (or at least can be) done via network, you could perhaps control a browser on your PC from Pythonista on your iDevice, but that's probably not what you're looking for. You'd need to be in the same local network as your PC, and then you might as well run your script there directly.

There are ways to automate web requests using Pythonista though. The requests module can be used to communicate with the server (requests is much more straightforward to use than urllib2 from the standard library), and HTML can be parsed using Beautiful Soup (bs4). Those are third-party modules, but Pythonista comes with both of them included. Keep in mind that this is nothing like using Selenium - these modules work at a lower level than a full web browser, so you'll want to use them differently. Instead of trying to simulate the entire "journey" through the website, you'll need to find out which parts are important, what URLs you need to request and what response data you need.

Webmaster4o

From the selenium docs:

Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer, PhantomJS), as well as the Remote protocol.

I don't know what the "remote protocol" is or how it works, maybe it could control a browser on a desktop computer from your iOS device? I'm doubtful that it will work. I've had success with mechanize on pythonista. Sourceforge downloads are awkward on iOS to say the least... You can find an easier-downloadable version here

JonB

For solutions requiring javascript, WebView's are also automatable, as you can inject arbitraty js into a page. I have found that often, though, js is not really required, as long as you look at what the js is doing and replicating it in requests/bs4. A great many pages use js for validation, or constructing urls from other element attributes, which you easily do yourself.