Home > Net >  selenium python a click in a cookie doesn't work
selenium python a click in a cookie doesn't work

Time:06-26

i try to click a button cookie, but doesn't locate element. with linux works but not with windows.

my code :

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


options = Options()
options.add_argument("start-maximized")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'chromedriver.exe')

url = "https://www.lachainemeteo.com/meteo-france/previsions-meteo-france-aujourdhui"
driver.get(url)
sleep(2)

btn = driver.find_element(By.CLASS_NAME,"sc-1epc5np-0.dnGUzk.sc-f7uhhq-2.coEmEP.button.button--filled.button__acceptAll").click()

where is the probleme, thank you

CodePudding user response:

Here's a command that worked for me:

driver.find_element(By.CSS_SELECTOR, "button.button__acceptAll").click()

I used By.CSS_SELECTOR with "button.button__acceptAll".

Easy to verify from the console.

enter image description here

For reliability on slower page loads, this is better:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# ...
WebDriverWait(self.driver, 10).until(
    EC.element_to_be_clickable(
        (By.CSS_SELECTOR, "button.button__acceptAll")
    )
).click()

Because this alert only appears for me when I set the browser's locale code to fr, I would consider adding a try/except block around the code. Wait for the alert to appear for up to 5 seconds, and if it does, close it, otherwise continue:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# ...
try:
    WebDriverWait(self.driver, 5).until(
        EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "button.button__acceptAll")
        )
    ).click()
except Exception:
    pass

CodePudding user response:

with just this line and it's ok, thx for your help Michael, it's work now. with my system linux chrome is in english, and windows my chrome in french, you are god

options.add_argument("--lang=en")
  • Related