I am not able to find the gettext
of the below code in the 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 Country. I tried using the xpath
:
driver.findElement(By.xpath("//*[@id='551']").getText())
but it is not returning any value. When I tried with
driver.findElement(By.xpath("//*[@id='551']")).getAttribute("title"))
I am getting the value as "079".
How can I to proceed?
It depends on the code as well. Give a try with the below code:
Instead of getText()
, please use getAttribute("innerHTML")
which will then return what you are looking for, including any HTML that is invisible.
<div class="no-docs-selected">
<p class="icon-alert">Please select a doc</p>
</div>
I was looking for Please select a doc
, but I didn't succeed with getText()
. But the below one worked.
driver.findElement(By.xpath("//p[@class='icon-alert']")).getAttribute("innerHTML");
我遇到了同样的问题,然后我将 .getText() 更新为.getAttribute("textContent")并且它起作用了。
It is really surprising you are able to get attribute title, but not text.
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();
I also had a case where getAttribute("innerHTML")
worked, but not getText()
. It turned out that the element was present, but it was not visible or displayed.
When I scrolled to the element first before calling getText()
it worked.
if(!element.isDisplayed())
scrollIntoView(element);
element.getText();
The scrollIntoView
function is like this:
public void scrollIntoView(WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
I therefore don't need to use getAttribute("innerHTML")`.
Unless it is a typo while copying to Stack Overflow, you are missing a 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 perfectly 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"));
使用<WebElement>.getAttribute("value")
提取文本。
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 text written between script tag.
This worked for me:
System.out.println(driver.findElement(By.id("error")).getAttribute("textContent"));
Code:
driver.get("https://login.salesforce.com/");
driver.findElement(By.id("username")).sendKeys("Hello");
driver.findElement(By.id("password")).sendKeys("Hello");
driver.findElement(By.id("Login")).click();
System.out.println(driver.findElement(By.id("error")).getAttribute("textContent"));
driver.close();
This worked for me. I used getAttribute("innerText")
instead of getText()
.
System.out.println(driver.findElement(By.id("modalError")).getAttribute("innerText"));
In a similar situation, this worked for me:
.getAttribute("innerHTML");
.getAttribute("innerText");
.getAttribute("outerText");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.