0

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?

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

3 Comments

No, I use Firefox, but it works, thanks :) (I made a silly mistake, I don't use Chromium ;)
When I was running the example, I used Linux. Now trying it on Windows, I'm getting the message Unable to find element with css selector 'close'
@darias I updated answer, it might be a waiting issue - I added an implicit wait and make sure that the window is appearing...

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.