Tahseen Adit Tahseen Adit - 3 months ago 19
Java Question

How to get the translated text from google translator using selenium?

I have tried the following code to get the google translator text using selenium:

result=driver.findElement(By.xpath("//body/div[@id='result_box']/span"));


I also tried these:

1.result=driver.findElement(By.xpath(".//body/div[@id='result_box']/span"));
2.result=driver.findElement(By.xpath("./*div[@id='result_box']/span"));
3.result=driver.findElement(By.xpath(".//div[@id='result_box']/span"));
4.result=driver.findElement(By.xpath("//div[@id='result_box']/span"));
5.result=driver.findElement(By.xpath(".//body/div[@id='result_box']/span"));
6.result=driver.findElement(By.xpath("./*[@id='result_box']/span"));


But none of the above works. I then tried to get the text by:

result=driver.findElement(By.id("result_box")).findElement(By.tagName("span"));
translatedtext=result.getText();


This returns a result but when I try to show the result in JTextarea it shows me '????' instead of the actual translated text.

I have also tried 'result.getAttribute("innerHTML")' but it also shows some question marks (?????) instead of the original translated text in JTextarea.

How can I solve this problem?

Guy Guy
Answer

The result box has tag <span>, not <div>

result = driver.findElement(By.xpath(".//span[@id='result_box']/span"));

Or

result = driver.findElement(By.xpath(".//*[@id='result_box']/span"));

With double slash.