0

I'm writing a code that uses https://www.deepl.com/translator to translate english movie titles to polish (since it's the only translator i know that doesn't translate them literally) but I'm unable to extract translated text from website.

driver.find_element(By.XPATH, '//*[@id="panelTranslateText"]/div[2]/section[2]/div[8]/div[1]/div/div[2]/ol/li[1]/button').click()
print("polish confirmed")

WebDriverWait(driver, 3)

translation = driver.find_element(By.ID, 'target-dummydiv').get_attribute("textContent")
WebDriverWait(driver, 3)
print("translation found")
print(translation)
print(len(translation))

The code runs smoothly until it's time to print out the translated text. It returns 2 character long blank string.

input text to translate
textarea found
sending text
text has been sent
language choice clicked
search bar found
polish inserted
polish confirmed
translation found


2

Here is the HTML code where the translated text is located.

<div class="lmt__inner_textarea_container" title="Kliknij na słowo, aby wyświetlić tłumaczenia alternatywne ">
   <textarea aria-labelledby="translation-results-heading" class="lmt__textarea lmt__target_textarea lmt__textarea_base_style" data-gramm_editor="false" dl-test="translator-target-input" autocomplete="off" lang="pl-PL"></textarea>
   <div id="target-dummydiv" aria-hidden="true" class="lmt__textarea lmt__textarea_dummydiv" lang="pl-PL">Stranger Things</div>
</div>

I have also tried using get_attribute("innerText") but it doesn't work either. Is there any other way to extract this text?

kubek
  • 3
  • 2

1 Answers1

0

Try driver.find_element(By.ID, 'target-dummydiv').get_attribute("innerHTML") or driver.find_element(By.ID, 'target-dummydiv').text

  • Unfortunately none of these return the text. ```driver.find_element(By.ID, 'target-dummydiv').get_attribute("innerHTML")``` returns the same blank string with 2 characters and ```driver.find_element(By.ID, 'target-dummydiv').text``` returns blank string with 0 characters. – kubek Jul 25 '22 at 12:51
  • Can you show full code, please? Because I have tried your peace of code with `https://www.deepl.com/translator#en/pl/Starager%20Things`with this url and get nice result. – Bro From Space Jul 25 '22 at 14:16
  • ```chrome_options = Options() chrome_options.add_experimental_option("detach", True) path = "C:\Program Files (x86)\chromedriver.exe" driver = webdriver.Chrome(options=chrome_options, service=Service(ChromeDriverManager().install())) driver.get("https://www.deepl.com/translator") driver.maximize_window() print("input text to translate") driver.implicitly_wait(5) wpisz_tekst = driver.find_element(By.XPATH, '//*[@id="panelTranslateText"]/div[2]/section[1]/div[3]/div[2]/textarea') print("textarea found")``` – kubek Jul 25 '22 at 16:11
  • ```print("sending text") wpisz_tekst.send_keys("Stranger Things") print("text has been sent") driver.find_element(By.XPATH, '//*[@id="panelTranslateText"]/div[2]/section[2]/div[1]/div[1]/div[1]/button').click() print("language choice clicked") find_pl = driver.find_element(By.XPATH,'//*[@id="panelTranslateText"]/div[2]/section[2]/div[8]/div[1]/div/div[1]/div/input') print("search bar found") find_pl.send_keys("polski") print("polish inserted") driver.find_element(By.XPATH, '//*[@id="panelTranslateText"]/div[2]/section[2]/div[8]/div[1]/div/div[2]/ol/li[1]/button').click()``` – kubek Jul 25 '22 at 16:12
  • ```print("polish confirmed") WebDriverWait(driver, 3) translation = driver.find_element(By.ID, 'target-dummydiv').text WebDriverWait(driver, 3) print("translation found") print(translation) print(len(translation))``` – kubek Jul 25 '22 at 16:13
  • Here is the full code – kubek Jul 25 '22 at 16:13