Reputation: 184
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
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
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
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