0

I am building a program in Python that interacts with an online store. So far I am able to find the desired item and navigate to the page using BeautifulSoup, but I am having issues clicking the "Add to cart" button. Most of the solutions I've found online using robobrowser and similar would work except that they are dealing with the tag which has a method attribute. The for on the site I am dealing with looks like this:

<input class="button" name="commit" type="submit" value="add to cart">

How would I go about "clicking" this button? What libraries would I need. I'm using python 3 by the way so I can't use mechanize. Thanks in advance for the help.

1 Answers1

0

You can consider using Selenium in Python.

Please use the code snippet below as a reference:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("url")
button = driver.find_element_by_css_selector("input[class='button']")
button.click()

In case you get multiple matches, you can narrow it down by involving more attributes:

button = driver.find_element_by_css_selector("input[class='button'][name='commit']")

Please refer to this link for more examples on Python Selenium. http://selenium-python.readthedocs.io/locating-elements.html

Yu Zhang
  • 2,037
  • 6
  • 28
  • 42
  • 1
    No need for `;` in Python, missing `driver.get("url")` and the selectors can be simplified: `input.button` and `input.button[name=commit]` respectively. – alecxe Jul 11 '16 at 13:06
  • Does the user have to have firefox installed for this to work – Wayward Semicolon Jul 11 '16 at 14:30
  • Because I may distribute this to people who possibly don't have it. – Wayward Semicolon Jul 11 '16 at 14:31
  • @alecxe, yes, sorry about them, will remove them now. – Yu Zhang Jul 11 '16 at 20:10
  • @WaywardSemicolon, yes, you must have the following installed, Firefox (installed independently), Selenium (which will have Firefox webdriver by default), python. – Yu Zhang Jul 11 '16 at 20:11
  • is there any way to do it with chrome? – Wayward Semicolon Jul 11 '16 at 20:49
  • @WaywardSemicolon, sure, but you need to download Chrome driver here, https://sites.google.com/a/chromium.org/chromedriver/downloads, and when you call your driver = webdriver.Chrome(you need to put your chrome webdriver driver path here). That is it. – Yu Zhang Jul 11 '16 at 20:51
  • @WaywardSemicolon, has your question been answered yet? – Yu Zhang Jul 11 '16 at 22:55
  • I guess it has except for one more thing: I'm thinking about packaging this all up with py2exe. That will pack up all the libraries and things but it won't pack up the chrome web driver will it? How can I get my users to have that installed? – Wayward Semicolon Jul 12 '16 at 05:04
  • @WaywardSemicolon, will this help? http://stackoverflow.com/questions/30589329/creating-an-exe-with-selenium-module-py2exe-pyinstaller – Yu Zhang Jul 12 '16 at 05:15
  • That's for Firefox though. Is there any way to package the chrome driver – Wayward Semicolon Jul 12 '16 at 06:11