I’m pretty new with the selenium module in python and I’m not able to press a button. Here the HTML-Code:
<!-- more tags --> <a href="#"> <img src="flag-en.png" title="English" alt="English"> English </a> <!-- more tags -->
Here’s a minimal example:
import selenium.webdriver import selenium.webdriver.chrome import selenium.webdriver.chrome.options import time from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC def get_driver(): chrome_options = selenium.webdriver.chrome.options.Options() return selenium.webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options) driver = get_driver() driver.get(url) # you can comment this out, if the first language which is selected isn't english # otherwise this time is used to manually change the language to a non-english # language to test, if it really selects the correct button time.sleep(2) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='btn-group lang-sel']"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[@title='English']"))).click()
what am I doing wrong?
Context
I want to automatically select the language of the website.
Advertisement
Answer
It seems to be your locator strategy is correct. You can shorten your xpath expression as follows:
pos= WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,'//ul[@class="dropdown-menu"]/li[1]/a))).click()