0

I am using selenium to automate translation of some text from english to Japanese .link is present here translate at deepl.com I am having trouble in clicking on 'Translate Into japanese' button in drop down of Translate into. I am attaching photo for more clarity here click here for image. I used xpaths but sometimes they work and sometimes they just translate to russian instead of japanese. How should this button be located. I would greatly appreciate your help.

Mohammad Ahmed
  • 57
  • 1
  • 1
  • 6
  • Does this answer your question? [How to select a drop-down menu value with Selenium using Python?](https://stackoverflow.com/questions/7867537/how-to-select-a-drop-down-menu-value-with-selenium-using-python) – eNc Jun 13 '20 at 01:30
  • @eNc No, I checked , it doesnot help – Mohammad Ahmed Jun 13 '20 at 02:34

1 Answers1

0

You can add the following imports :

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

And pass some expected conditions before clicking the buttons :

driver.get('https://www.deepl.com/translator#en/de/...')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@class='lmt__language_select__active'])[2]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@dl-lang='JA'])[2]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//textarea)[2]"))).click()
trad = [el.text for el in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='lmt__textarea_base_style']/span[text()]")))]
print("".join(trad))

Alternatives : use the API (see @Bertrand Martel's answer), or replace directly "de" with "ja" in your request url.

https://www.deepl.com/translator#en/de... to https://www.deepl.com/translator#en/ja...

EDIT : If the second click fail you can use ActionChains :

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@dl-lang='JA'])[2]")))).click().perform()
E.Wiest
  • 5,425
  • 2
  • 7
  • 12
  • Inserted your snippet and it raises same exception as before : raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (700, 497). Other element would receive the click:
    ...
    – Mohammad Ahmed Jun 13 '20 at 02:50
  • Using https://www.deepl.com/translator#en/ja/ works when used manually but strangely with automated browser, language changes to german instead of japanese. – Mohammad Ahmed Jun 13 '20 at 03:58
  • Above code worked with using firefox browser instead of chrome browser. – Mohammad Ahmed Jun 14 '20 at 05:50
  • Great. I've added an alternative with "ActionChains" for the second click. – E.Wiest Jun 14 '20 at 10:00