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?
答案 0 :(得分:1)
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.
答案 1 :(得分:1)
你也可以像这样使用css选择器:
result = driver.findElement(By.cssSelector(&#34;#result_box&gt; span&#34;));
人们说这比xpath快
答案 2 :(得分:0)
这对我有用,但是我使用python,您可以尝试在Java中使用等效的find_element_by_id
函数
driver.find_element_by_id("gt-res-dir-ctr").text