Python Selenium没有单击<a>

时间:2019-03-13 17:23:31

标签: python python-3.x selenium selenium-webdriver selenium-firefoxdriver

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...

1 个答案:

答案 0 :(得分:1)

尝试一下:

driver.execute_script("document.getElementById('confirm-btn').click()")