Search code examples
pythonseleniumclick

Selenium with python : failed to click on the button to switch the language of a website


I am trying to go to the french version of this website : https://ciqual.anses.fr/. I tried to click on the button 'FR' but nothing happens, I am still on the english page.

Here is my code :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--headless")
driver = webdriver.Chrome("chromedriver", options=chrome_options)

driver.get('https://ciqual.anses.fr/')

switch_to_french = driver.find_element("xpath", "//a[@id='fr-switch']")
ActionChains(driver).move_to_element(switch_to_french).click()


#to see what happened : 

from IPython.display import Image
png = driver.get_screenshot_as_png()
Image(png, width='500')
#I am still on the english website 

Please help !


Solution

  • Try this:

    switch_to_french = driver.find_element(By.XPATH, "//a[@id='fr-switch']")
    driver.execute_script("arguments[0].click();", switch_to_french)