Failed to extract text using gettext in selenium-webdriver and also failed to click it

I cannot find the gettext the code below in selenium webdriver .

 <a id="551" class="blueTextNormal1 spc" onclick="sPh(this,'079');return false;" title="079">Country</a> 

I want to get the value of the country. I tried using xpath

 driver.findElement(By.xpath("//*[@id='551']").getText()) 

but it does not return any value. When I tried using

 driver.findElement(By.xpath("//*[@id='551']")).getAttribute("title")) 

I get the value as "079"

Someone can suggest ideas on how to proceed.

+6
source share
7 answers

It also depends on the code. Try using the code below: Instead of .getText (), use .getAttribute ("innerHTML"), which will then return what you are looking for, including any invisible HTML

 <div class="no-docs-selected"> <p class="icon-alert">Please select an doc</p> </div> 

I searched β€œPlease select a document, but did not interrupt it .gettext (). But below worked.

.

driver.findElement (By.xpath ("// p [@ class = 'warning icon']”))) GetAttribute ("innerHTML");

+15
source

I ran into the same problem and then updated .getText () to .getAttribute ("textContent") and it worked.

+4
source

No wonder you can get the attribute name, but not the text,

Can you give one try with

 driver.findelement(By.xpath("//a[@id='551' and contains(text(),'Country')]")).isDisplayed(); 

or

 driver.findelement(By.xpath("//a[@id='551' and text()='Country']")).isDisplayed(); 
0
source

if it is not a type when copying to stackoverflow, you are missing parentheses before getText. + Change driver.findElement(By.xpath("//*[@id='551']").getText()) to driver.findElement(By.xpath("//*[@id='551']")).getText()

I tried the following code and it worked fine for me.

 WebDriver driver = new ChromeDriver(); driver.get("C:\\test.html"); System.out.println(driver.findElement(By.xpath("//*[@id='551']")).getText()); System.out.println(driver.findElement(By.xpath("//*@id='551']")).getAttribute("title")); 
0
source

use .getAttribute ("value") to extract text

0
source
  String text1=driver.findElementByXPath("//input[@value='EFASTGTC']/../ancestor::tr[1]/scipt[1]").getAttribute("innerText"); 

System.out.println ("Value =" + text1);

"This code will work if you want the text written between the script tag"

0
source

In a similar situation, the following worked for me: .getAttribute ("innerHTML"); .getAttribute ("InnerText"); .getAttribute ("outerText");

-1
source

Source: https://habr.com/ru/post/945648/


All Articles