Target text is in div instead of textarea - and it has also id so xpath is short
'//div[@id="target-dummydiv"]'
But I couldn't get text using
content.text # empty list
but I could get it with
content.get_attribute('innerHTML')
Because I used page with Polish language so I had lists with Polish names - ie. "Angielski" instead of "English" so I couldn't use names on list to select language and I used
for source language
@dl-test="translator-lang-option-en"
@dl-test="translator-lang-option-fr"
@dl-test="translator-lang-option-pl"
for target language
@dl-test="translator-lang-option-en-EN"
@dl-test="translator-lang-option-fr-FR"
@dl-test="translator-lang-option-pl-PL"
Some languages show alternative translations which I could get with
'//button[@class="lmt__translations_as_text__text_btn"]'
It needed only to skip empty strings.
Result:
__________________________________________________
Original : hello this is me not him
Translation : Bonjour, c'est moi, pas lui.
Alternative : Bonjour, c'est moi et non lui.
Alternative : Allo, c'est moi, pas lui.
__________________________________________________
Full working code with other changes
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
import time
# Define text to translate
source_text = 'hello this is me not him'
LANGUAGES = ({ # [source, target]
"french": ['fr', 'fr-FR'],
"english": ['en', 'en-GB'],
"german": ['de', 'de-DE'],
"spanish": ['es', 'es-ES'],
"portuguese": ['pt', 'pt-PT'],
"italian": ['it', 'it-IT'],
"dutch": ['da', 'da-DA'],
"polish": ['pl', 'pl-PL'],
"russian": ['ru', 'ru-RU']
})
#source_xpath = '//div[@dl-test="translator-source"]'
#target_xpath = '//div[@dl-test="translator-target"]'
source_input_text_xpath = '//textarea[@dl-test="translator-source-input"]'
#target_input_text_xpath = '//textarea[@dl-test="translator-target-input"]'
#target_output_text_xpath = '//div[@id="source-dummydiv"]
target_output_text_xpath = '//div[@id="target-dummydiv"]'
source_button_xpath = '//button[@dl-test="translator-source-lang-btn"]'
target_button_xpath = '//button[@dl-test="translator-target-lang-btn"]'
lang = LANGUAGES["english"][0] # 0 - source
source_language_xpath = f'//div[@dl-test="translator-source-lang-list"]//button[@dl-test="translator-lang-option-{lang}"]'
#lang = LANGUAGES["polish"][1] # 1 - target
lang = LANGUAGES["french"][1] # 1 - target
target_language_xpath = f'//div[@dl-test="translator-target-lang-list"]//button[@dl-test="translator-lang-option-{lang}"]'
# Start a Selenium driver
options = Options()
#options.add_argument("--headless")
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.set_window_size(1400, 1200)
# Reach the deepL website
url = 'https://www.deepl.com/en/translator'
#url = 'https://www.deepl.com/fr/translator'
#url = 'https://www.deepl.com/pl/translator'
driver.get(url)
# Select languages
driver.find_element_by_xpath(source_button_xpath).click() # open list
time.sleep(0.1) # wait for list
driver.find_element_by_xpath(source_language_xpath).click() # click option on list (maybe it will need to scroll)
driver.find_element_by_xpath(target_button_xpath).click() # open list
time.sleep(0.1) # wait for list
driver.find_element_by_xpath(target_language_xpath).click() # click option on list (maybe it will need to scroll)
# Select Automatic
driver.find_element_by_xpath('//button[@class="lmt__formalitySwitch__toggler"]').click()
time.sleep(0.1)
driver.find_element_by_xpath('//div[@class="lmt__formalitySwitch__menu_items"]//button[@_dl-connected="1"][3]').click()
# Get thie source inupt_area
input_area = driver.find_element_by_xpath(source_input_text_xpath)
# Send the text
input_area.clear()
input_area.send_keys(source_text)
# Wait for translation to appear on the web page
time.sleep(3)
# Get target text and alternatives
target_text = driver.find_element_by_xpath(target_output_text_xpath).get_attribute('innerHTML')
target_alternative_text = driver.find_elements_by_xpath('//button[@class="lmt__translations_as_text__text_btn"]')
# Display results
print('_'*50)
print('Original :', source_text)
print('Translation :', target_text.strip())
for item in target_alternative_text:
text = item.text.strip()
if text:
print('Alternative :', text)
print('_'*50)
Result:
having some issuesis not enough. Don't expect that we will run code to see it. Maybe elements are in<frame>and then browser treats it as separated element and you have to useswitch_to