Tahseen Adit
Tahseen Adit

Reputation: 184

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?

Upvotes: 0

Views: 2604

Answers (3)

dc7
dc7

Reputation: 418

This worked for me, however i used python,you can try using equivalent of find_element_by_id function in Java

driver.find_element_by_id("gt-res-dir-ctr").text

Upvotes: 0

Hezi Israeli
Hezi Israeli

Reputation: 439

you also can use css selector like this:

result = driver.findElement(By.cssSelector("#result_box>span"));

people say that is faster than xpath

Upvotes: 1

Guy
Guy

Reputation: 50809

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.

Upvotes: 1

Related Questions