Home > Enterprise >  My program don't find the cookie button (python, Chromedriver)
My program don't find the cookie button (python, Chromedriver)

Time:07-25

i use selenium chromedriver for make a program that automaticly go to nike.com and accept the cookie, but, for accept the cookie, the chromedriver must to find the cookie button with :

button = browser.find_element(By.CLASS_NAME,
"div[class='ncss-row']div[class='ncss-col-sm-12 ncss-col-md-6'] button[class='ncss-btn-primary-dark pt3-sm pb3-sm pt2-lg pb2-lg ta-sm-c u-full-width']"
)

but, when i start my program :

import webbrowser 
import undetected_chromedriver as uc
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
def main():
                                                                               
    browser = uc.Chrome()
                                                                            
    browser.get('https://www.nike.com/fr/snkrs-app?cp=24158993452_search_|snkrs|10689554523|109060467241|e|c|FR|app|508994633917&gclsrc=aw.ds&ds_rl=1252249&gclid=CjwKCAjwk_WVBhBZEiwAUHQCmV8yy66lssIN5EzoS0K8I4B1k9yk5kduxXy62w27SLfnChI_0U4UvBoC4P8QAvD_BwE')
                                                                                               
    sleep(8)
                                                                                              
    button = browser.find_element(By.CLASS_NAME, "div[class='ncss-row'] 
    div[class='ncss-col-sm-12 ncss-col-md-6'] button[class='ncss-btn-primary-dark pt3-sm pb3-sm pt2-lg pb2-lg ta-sm-c u-full-width']")
    button.click()
    sleep(100000)

if __name__ == '__main__':
    main()

So, when i start the program, he say that he not found the button, and i have tried a lot of solution, but i don't find the good program to do that.

CodePudding user response:

You would need a few extra imports: the following code will open your url, locate the Accept cookies button, and click it:

import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
import time as t
import pandas as pd

browser = uc.Chrome()

url = 'https://www.nike.com/fr/snkrs-app?cp=24158993452_search_|snkrs|10689554523|109060467241|e|c|FR|app|508994633917&gclsrc=aw.ds&ds_rl=1252249&gclid=CjwKCAjwk_WVBhBZEiwAUHQCmV8yy66lssIN5EzoS0K8I4B1k9yk5kduxXy62w27SLfnChI_0U4UvBoC4P8QAvD_BwE'

browser.get(url)

WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.ID, "hf_cookie_text_cookieAccept"))).click()

CodePudding user response:

The following locator:

div[class='ncss-row']div[class='ncss-col-sm-12 ncss-col-md-6'] button[class='ncss-btn-primary-dark pt3-sm pb3-sm pt2-lg pb2-lg ta-sm-c u-full-width']

isn't a CLASS_NAME. It's a CSS_SELECTOR.

You need to change the line of accordingly:

button = browser.find_element(By.CSS_SELECTOR, "div[class='ncss-row'] div[class='ncss-col-sm-12 ncss-col-md-6'] button[class='ncss-btn-primary-dark pt3-sm pb3-sm pt2-lg pb2-lg ta-sm-c u-full-width']")
  • Related