0

I am trying to get my selenium script to click a <a> link that is in a modal. So this is the html;

<div class="modal-btns">
                    <a href="" id="confirm-btn" class="btn-primary-md">Get Now</a>
                    <a href="" id="decline-btn" class="btn-control-md">Cancel</a>
                </div>

Now I want to click the button Get Now. I tried this;

            button = browser.find_elements_by_css_selector('#confirm-btn')
            for e in elements:
                try:
                    e.click()
                except:
                    what = 0
            print "Clicked!"

I started this script without headless mode. In the console it printed out Clicked! while in the browser nothing has happend.

How can I make this work?

==== EDIT ====

There is a also a button that triggers the modal, that just works.. That button html is;

<div class="action-button">
                                                <button type="button" class="btn-fixed-width-lg btn-primary-lg PurchaseButton" data-button-type="main" data-button-action="get" data-expected-price="0" data-bc-requirement="0" data-product-id="222406" data-item-id="1744006" data-item-name="SBC Cannon 2.0" data-asset-type="TShirt" data-asset-type-display-name="T-Shirt" data-item-type="Asset" data-expected-currency="1" data-expected-seller-id="52246" data-seller-name="ViacomIsPoo" data-userasset-id="" style="">
                                                    Get
                                                </button>
                        </div>

It clicks that button by this code:

            elements = browser.find_elements_by_xpath("//button[contains(@class, 'PurchaseButton')]")
            for e in elements:
                try:
                    e.click()
                except:
                    what = 0

So the full code will be;

            elements = browser.find_elements_by_xpath("//button[contains(@class, 'PurchaseButton')]")
            for e in elements:
                try:
                    e.click()
                except:
                    what = 0
            time.sleep(4)
            button = browser.find_element_by_id('confirm-btn')
            for e in elements:
                try:
                    e.click()
                except:
                    what = 0
            print "Clicked!"

But it doesn't click the confirm button...

6
  • 1
    Maybe try a different search method? E.g. find_element_by_xpath? Commented Mar 13, 2019 at 17:27
  • What are you expecting to happen. There is nothing in the href ="". Put href="#" Commented Mar 13, 2019 at 17:32
  • Its not my website @Duck_dragon it has to trigger javascript I guess... Commented Mar 13, 2019 at 17:45
  • I already tried that @emporerblk Commented Mar 13, 2019 at 17:45
  • Is your confirm Button is a pop up ? Commented Mar 13, 2019 at 17:57

1 Answer 1

1

Try this:

driver.execute_script("document.getElementById('confirm-btn').click()")
Sign up to request clarification or add additional context in comments.

2 Comments

Just be aware that using JS to click an element is not a user scenario... no user can click a button that isn't visible, off screen, etc. but JS can. If you don't care about user scenarios then it doesn't matter.
It's not about visibility, it has to be a bug based on some browsing environment conditions. It has happened to me too, with a perfectly working and simple button and I ended up using JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.