Search code examples
pythonselenium-webdrivercookies

how to accept cookies from a site with selenium


I create this code with selenium to click the cookie button, because I have to accept cookies to access web site data

from selenium import webdriver
from selenium.webdriver.common.by import By

chromeOptions = webdriver.ChromeOptions() 
chromeOptions.add_argument("ignore-certificate-errors")
chromeOptions.add_argument('--headless=chrome')
wbe = webdriver.Chrome(options=chromeOptions)
wbe.get('https://www.asianbetsoccer.com/it/nextgame.html')
wbe.implicitly_wait(10)

wbe.find_element(By.XPATH, "/html/body/button").click()

but it gives me an error because it doesn't find the button:

wbe.find_element(By.XPATH, "/html/body/button")
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 741, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_resp
onse
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/button"}
  (Session info: chrome-headless-shell=124.0.6367.91); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troub
leshooting/errors#no-such-element-exception
Stacktrace:
        GetHandleVerifier [0x00007FF754EB1502+60802]
        (No symbol) [0x00007FF754E2AC02]
        (No symbol) [0x00007FF754CE7CE4]
        (No symbol) [0x00007FF754D36D4D]
        (No symbol) [0x00007FF754D36E1C]
        (No symbol) [0x00007FF754D7CE37]
        (No symbol) [0x00007FF754D5ABBF]
        (No symbol) [0x00007FF754D7A224]
        (No symbol) [0x00007FF754D5A923]
        (No symbol) [0x00007FF754D28FEC]
        (No symbol) [0x00007FF754D29C21]
        GetHandleVerifier [0x00007FF7551B411D+3217821]
        GetHandleVerifier [0x00007FF7551F60B7+3488055]
        GetHandleVerifier [0x00007FF7551EF03F+3459263]
        GetHandleVerifier [0x00007FF754F6B846+823494]
        (No symbol) [0x00007FF754E35F9F]
        (No symbol) [0x00007FF754E30EC4]
        (No symbol) [0x00007FF754E31052]
        (No symbol) [0x00007FF754E218A4]
        BaseThreadInitThunk [0x00007FFCEC3E7AC4+20]
        RtlUserThreadStart [0x00007FFCEDC2A4E1+33]

can I use selenium or do I have to use another method?

I have to click to this button that, so it isn't a pop up

and then this window appears in which I have to click "Accetta tutto" and "Salva e continua"


Solution

  • You have wrong xpath.

    Path /html/body/button searchs button directly in body but button is in some div which is another div, etc. And this needs to use // to get nested element without using full path:

    /html/body//button or shorter //button

    But there are many buttons on page and using only //button will find first button on page - which is not button with Accetta tutto - so you have to use id or text() to find correct button.

    This works for me

    wbe.find_element(By.XPATH, "//button[text()='Accetta tutto']").click()
    

    or using contains() if you want to search only part of text

    wbe.find_element(By.XPATH, "//button[contains(text(), 'Accetta tutto')]").click()
    

    Full code which I used for tests:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    chromeOptions = webdriver.ChromeOptions() 
    chromeOptions.add_argument("ignore-certificate-errors")
    #chromeOptions.add_argument('--headless=chrome')
    
    wbe = webdriver.Chrome(options=chromeOptions)
    wbe.get('https://www.asianbetsoccer.com/it/nextgame.html')
    wbe.implicitly_wait(10)
    
    #wbe.find_element(By.XPATH, "/html/body/button").click()   # it can't find butotn
    #wbe.find_element(By.XPATH, "/html/body//button").click()  # it finds wrong button 
    wbe.find_element(By.XPATH, "//button[text()='Accetta tutto']").click()  # this works for me
    #wbe.find_element(By.XPATH, "//button[contains(text(), 'Accetta tutto')]").click()  # this works for me
    
    
    input('Press ENTER to close')