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

Time:12-05

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 !

CodePudding user response:

Try this:

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

CodePudding user response:

Try the below

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


language_Fr= WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a[@id='fr-switch']")))
language_Fr.click()

You can read more about this from here

  • Related