Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to check off a checkbox with Browser splinter?

Once I add the following item to cart: http://www.supremenewyork.com/shop/accessories/wau85w4km/cxv3ybp1w and go to the check out page: https://www.supremenewyork.com/checkout, there is a terms and conditions checkbox that I’m attempting to check off with Browser’s splinter but I’m not able to do so:

e.g. Tried the following but all encountered an error:

from splinter import Browser

browser = Browser("chrome")
browser.find_by_id('order_terms').click()         
#Error: selenium.common.exceptions.WebDriverException: Message: unknown error

browser.check('order[terms]').click()
#Error: selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

browser.find_by_name('order[terms]').click()
#Error: selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

What could I be doing wrong? And how can I go about checking the checkbox with the Browser splinter?

Thank you in advance and will be sure to upvote/accept answer

like image 999
Jo Ko Avatar asked Nov 30 '17 20:11

Jo Ko


1 Answers

Two major things to point out:

  • to avoid timing issues, you need to have "explicit waits" in your script - waiting for elements to be visible or present before proceeding to the next actions
  • this checkbox can and should be clicked by clicking the whole label element containing the input and other auxiliary elements

Here is the complete code:

from splinter import Browser

browser = Browser("chrome")
browser.visit("http://www.supremenewyork.com/shop/accessories/wau85w4km/cxv3ybp1w")
browser.wait_time = 10

try:
    browser.is_element_visible_by_css("input[name=commit]", 10)
    browser.find_by_css("input[name=commit]").first.click()

    browser.is_element_visible_by_css("a.checkout", 10)
    browser.find_by_css("a.checkout").first.click()

    browser.is_element_present_by_css("label.terms", 10)
    browser.find_by_css('label.terms').click()
finally:
    browser.quit()

Here is a working code that goes to the main page, navigates to the third product in the scroller, adds it to cart, checks out and accepts the terms of use, time.sleep() at the end is just for you to see the result:

from splinter import Browser

browser = Browser("chrome")
browser.visit("http://www.supremenewyork.com/shop")
browser.wait_time = 10

try:
    # open a product
    browser.is_element_visible_by_css("#shop-scroller > li > a", 10)
    browser.find_by_css("#shop-scroller > li > a")[2].click()

    # add to cart
    browser.is_element_visible_by_css("input[name=commit]", 10)
    browser.find_by_css("input[name=commit]").first.click()

    # checkout
    browser.is_element_visible_by_css("a.checkout", 10)
    browser.find_by_css("a.checkout").first.click()

    # accept terms and conditions
    browser.is_element_present_by_css("label.terms", 10)
    browser.find_by_css('label.terms').click()

    import time
    time.sleep(10)
finally:
    browser.quit()
like image 133
alecxe Avatar answered Nov 12 '22 07:11

alecxe