0

I am using this website with selenium : link

And am having some issues trying to : select language for input and output, output text and language type(formel or informel or automatique )

enter image description here

my code so far:

# Make imports

# Define text to translate
text_to_translate = 'hello this is me not him'

chrome_options = Options()
chrome_options.add_argument("--headless")

TARGET_LANGUAGES = ({"french": 1,
                     "english": 2,
                     "german": 3,
                     "spanish": 4,
                     "portuguese": 5,
                     "italian": 6,
                     "dutch": 7,
                     "polish": 8,
                     "russian": 9})
#input is working good 
input_text_xpath = """//*[@id="dl_translator"]/div[5]/div[2]/div[1]/div[2]/div[1]/textarea""" 
output_text_xpath = """//*[@id="dl_translator"]/div[5]/div[2]/div[3]/div[3]/div[1]/textarea"""
language_select_xpath = """//*[@id="dl_translator"]/div[1]/div[3]/div[1]/div/button/div"""
target_language_xpath = f"""//*[@id="dl_translator"]/div[1]/div[3]/div[1]/div/div/button[{target_language=TARGET_LANGUAGES["english"]}]"""
# Start a Selenium driver
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=chrome_options)


# Reach the deepL website
deepl_url = 'https://www.deepl.com/fr/translator'
driver.get(deepl_url)

# Get thie inupt_area
input_area = driver.find_element_by_xpath(input_text_xpath)

# Send the text
input_area.clear()
input_area.send_keys(text_to_translate)

# Wait for translation to appear on the web page
time.sleep(2)

# Get copybutton and click on it
content= driver.find_element_by_xpath(output_text_xpath).text
# Display results
print('_'*50)
print('Original    :', text_to_translate)
print('Translation :', content)
print('_'*50)
4
  • What happens when you run your code? Commented Apr 30, 2021 at 19:33
  • do you get error message ? You have to describe it. having some issues is 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 use switch_to Commented Apr 30, 2021 at 20:53
  • @furas i get no error message, i just get nothing back from it. Commented Apr 30, 2021 at 21:14
  • @furas can u just help me locate these elements from the website i added the code just to not get vote for closing or something. Commented Apr 30, 2021 at 21:16

2 Answers 2

2

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:

Sign up to request clarification or add additional context in comments.

3 Comments

thank you soo much, it's working like a charm, i did manage to have a working version in normal mode but when i switch to headless it doesn't work, but ur code is working perfectly in headless, thanks again.
I added selection Auotmatic which I forgot. Now I also added driver.set_window_size(1400, 1200) because it headless mode had problem on my computer - window was too small and it clicked in cookie message
i am just getting confused why i was getting blank text in the output and now with inner html it's working?
1

This is the xpath to open the language list:

//button[@dl-test='translator-source-lang-btn']

This is the xpath for selecting the language:

f"//div[@dl-test='translator-source-lang-list']/button[text()='{language}']"

This is the xpath to open the translate into list

//button[@dl-test='translator-target-lang-btn']

This is the xpath to select the translate language

 f"//button[contains(@dl-test,'translator-lang-option') and text() = '{translate}']"

This is the xpath to open the formal/informal list

//button[@class = 'lmt__formalitySwitch__toggler']

This is the xpath to select the formal/informal options. Replace 'Automatic' with the option you want.

(//div[@class='lmt__formalitySwitch__menu_items']//button[text()='Automatic'])[0]

So first click the list, then pass in the list item you want to select

language = 'Danish'
driver.find_element_by_xpath("//button[@dl-test='translator-source-lang-btn']").click()
driver.find_element_by_xpath(f"//div[@dl-test='translator-source-lang-list']/button[text()='{language}']").click()

Output text

driver.find_element_by_xpath("//div[@class='lmt__textarea_container lmt__textarea_container_no_shadow']).text

5 Comments

what about the output text please!
I missed that. That would be: //div[@class='lmt__textarea_container lmt__textarea_container_no_shadow']
Oh I see, you need the other elements from output. I'll update my code.
actually for the output text i get only the suggested translations not the actual one.
Try this xpath://textarea[@dl-test='translator-target-input']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.