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

1

6

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

Known answered 30/11, 2017 at 20:36 Comment(10)
Can you please post the error code you are running into?Tropous
@SuitBoyApps Edited original answer with corresponding errors! Appreciate the responseKnown
You may want to check out these articles Error 1 ,Error 2. I am not terribly familiar with the splinter library, but if I had to make a guess on what was going on I would say the second errors are caused by the first one.Tropous
I tried browser.find_by_css("#order_terms").click() and it worked for me?Eventual
@JoKo, any update?Eventual
@TarunLalwani Gave it an attempt but didn't work :(. Did you attempt it with Mac OS? Shouldn't be any different though right?Known
Yes I had tried on MacOS onlyEventual
@TarunLalwani That's odd. Tried it and got the following error: selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input class="checkbox" type="checkbox" value="1" name="order[terms]" id="order_terms" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%; height: 140%; margin: 0px; padding: 0px; background: rgb(255, 255, 255); border: 0px; opacity: 0;"> is not clickable at point (616, 478). Other element would receive the click: <ins class="iCheck-helper" style="position: absolute; top: -20%; left: -20%; display: block; width: 140%;...Known
@TarunLalwani Checking in to see if you had the chance to see my previous comment! Thank you. You can try with supremenewyork.com/shop/accessories/soi1ulsdv/yks6zay73 nowKnown
How do u guys verify if a checkbox is checked in splinter? tired of finding out answersTsosie
T
4

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()
Tiertza answered 5/12, 2017 at 13:58 Comment(4)
Appreciate the response! But have you tried the following? Add the following item to cart: supremenewyork.com/shop/accessories/wau85w4km/cxv3ybp1w and go to the check out page: supremenewyork.com/checkout, there is a terms and conditions checkbox that I’m attempting to check off with Browser’s splinter. Because I made the attempt but still doesn't work.Known
Checking in to see if you saw the previous comment. Thanks in advance!Known
@JoKo ah, sorry, missed the first comment. Please check out the updated answer, just added what works for me as is. Let me know if you have any issues with that. Thanks.Tiertza
No worries. Did you simply attempt to add the item to the cart and checked out manually and tried to execute the code to check off the checkbox? Unfortunately no luck so far :( supremenewyork.com/shop/accessories/soi1ulsdv/yks6zay73 Tried it with this. Thank you in advance!Known

© 2022 - 2024 — McMap. All rights reserved.