ccasimiro9444
ccasimiro9444

Reputation: 415

How to select a different language on a website with selenium

This is an open site with tax information. I access the site, click on 'Abzüge', then the next page is in English. I try to change to German, however, I cannot locate the button. My current code is

from selenium import webdriver
driver = webdriver.Chrome('/path-to-my-chromedriver/chromedriver')
driver.get('https://www.estv.admin.ch/estv/de/home/allgemein/steuerstatistiken/fachinformationen/steuerbelastungen/steuerfuesse.html')
elem1 = driver.find_element_by_link_text('Abzüge')
elem1.click()
de = driver.find_element_by_xpath('/html/body/div/div[4]/header/div/div[2]/div/button[1]')
de.click()
driver.close()

The Error is NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/div[4]/header/div/div[2]/div/button[1]"} (Session info: chrome=85.0.4183.102)

I have also tried to find the button by class and by shorter xPaths, but I can never locate the button.

I also need to select some buttons in the main field. So I gave that a try as well, but it does not work as well. My code to select Whole of Switzerland, without changing to German, is

element = driver.find_element_by_xpath('//*[@id="LOCATION"]/div[2]/div[1]/div[1]/div/div/div[2]')
element.click()

I get the same error, NoSuchElementException.

How would I change the language on the webpage, and how would I select a button in the body?

Upvotes: 0

Views: 1374

Answers (1)

Dilip Meghwal
Dilip Meghwal

Reputation: 632

You can use below code with XPath locator discussed in comments.

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

driver = webdriver.Chrome()

driver.get('https://www.estv.admin.ch/estv/de/home/allgemein/steuerstatistiken/fachinformationen/steuerbelastungen/steuerfuesse.html')

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH, "//nav[@class='nav-lang']//li[contains(., 'IT')]")))

print(driver.find_element(By.XPATH, "//nav[@class='nav-lang']//li[contains(., 'IT')]").text)
driver.find_element(By.XPATH, "//nav[@class='nav-lang']//li[contains(., 'IT')]").click()

Upvotes: 1

Related Questions