selenium 蟒:无法单击切换网站语言的按钮

nxowjjhe  于 2022-12-13  发布在  其他
关注(0)|答案(2)|浏览(133)

我尝试去这个网站的法语版本:https://ciqual.anses.fr/。我试着点击按钮“FR”,但什么也没发生,我仍然在英语页面上。
下面是我的代码:

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

救命啊!

zyfwsgd6

zyfwsgd61#

试试这个:

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

vhmi4jdf2#

请尝试以下操作

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()

您可以从here了解更多信息

相关问题