简体   繁体   中英

Click 'onclick' button with selenium and Python

I need to scrap some information from this page .

However, the page needs the age confirmation before entering it. It has a button which must be clicked before the page can be seen. My problem is that my methods to click this button simply does not work.

Im using selenium and Python 2.7.10.

Here's my code:

def download_specific_page(url):
    try:
        browser = get_browser()
        wait = WebDriverWait(browser, 30)
        browser.get(main_page_url)
        time.sleep(2)
        buttons = browser.find_elements_by_class('close')
        for button in buttons:
            onclick_text = button.get_attribute('onclick')
            if onclick_text and re.search('ConfirmAge();', onclick_text):
                print "found it!"
                button.click()
        browser.get(url)
        html = browser.page_source
        browser.close()
        return html
    except Exception:
        info = 'Generic exception\n'
        return ""

I also tried with xpath, but still, no success:

def download_specific_page(url):
    try:
        browser = get_browser()
        wait = WebDriverWait(browser, 30)
        browser.get(main_page_url)
        button = browser.find_element_by_xpath("/html/body/div#bg_image/div.container/div#content/div#confirmage/div.confirmage_navigation/button.close[1]")
        button.click()
        time.sleep(2)
        browser.get(url)
        html = browser.page_source
        browser.close()
        return html
    except Exception:
        info = 'Generic exception\n'
        return ""

Any ideas how to click this button so I can scrap the page?

Use regular click:

from selenium import webdriver

URL = 'https://www.24dolores.pl/pl/ogloszenia-towarzyskie'
CSS_SELECTOR = '.close'

browser = webdriver.Chrome()
browser.implicitly_wait(10)
browser.get(URL)
close = browser.find_element_by_css_selector(CSS_SELECTOR)
close.click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM