简体   繁体   中英

The find element returns empty string..using XPath contains,Text()

The assertion fails as the Gender and date returns false.The IwebElement returns an element but when I print the text in it.The result is an empty string.This happens only for Gender and Date rest all give correct result.

CODE

 IWebElement actualname = BrowserHelper.WebDriver.FindElement(By.XPath("//a[contains(text(),'"+ name+"')]"));
            Console.Write(actualname.Text);
            IWebElement actualIdentifier = BrowserHelper.WebDriver.FindElement(By.XPath("//td[contains(text(),'"+ Identifier+"')]"));
            Console.Write(actualIdentifier.Text);
            IWebElement actualGender = BrowserHelper.WebDriver.FindElement(By.XPath("//td[contains(text(),'"+ Gender+"')]"));
             Console.Write(actualGender.Text);
            IWebElement actualDateofBirth = BrowserHelper.WebDriver.FindElement(By.XPath("//td[contains(text(),'"+ DateofBirth+"')]"));
            Console.Write(actualDateofBirth.Text);
            IWebElement actualAddress = BrowserHelper.WebDriver.FindElement(By.XPath("//td[contains(text(),'"+ Address+"')]"));
            Console.Write(actualAddress.Text);

            Assert.IsTrue((actualname.Displayed) && (actualIdentifier.Displayed) && (actualGender.Displayed) && (actualDateofBirth.Displayed) && (actualAddress.Displayed), "Incorrect out put displayed for patient query");

HTML :-

 <tbody _ngcontent-c32="" style="max-height: 20vh;"> <!----><tr _ngcontent-c32=""> <td _ngcontent-c32="" style="text-align:center;vertical-align:middle;width:5em"> <input _ngcontent-c32="" type="checkbox" id="0_checkbox"> </td> <td _ngcontent-c32="" style="vertical-align:middle"> <!----><span _ngcontent-c32="" class="fa fa-plus"></span> <a _ngcontent-c32="" href="javascript:void(0)">Braden Dunthorn</a> </td> <td _ngcontent-c32="" style="vertical-align:middle">Tribal ID: 49963093</td> <td _ngcontent-c32="" style="vertical-align:middle">M</td> <td _ngcontent-c32="" style="vertical-align:middle">12/21/1990</td> <td _ngcontent-c32="" style="vertical-align:middle">7 Carioca , Grenville, MD, 29605</td> <!----> </tr><tr _ngcontent-c32=""> <td _ngcontent-c32="" style="text-align:center;vertical-align:middle;width:5em"> 

A blind guess but judging from my own experience, maybe there is a reason these two fields load later than others (for example, lazy loading?). Consider setting up a wait of 1 second (pretty large wait time, I know, but you can always lower it down after you find if it works).

Expanding on the comment, I would try something like this (pseudocode-ish):

new Thread(()=>{
    Thread.Sleep(1000);
    IWebElement actualGender = BrowserHelper.WebDriver.FindElement(By.XPath("//td[contains(text(),'"+ Gender+"')]"));
    Console.Write(actualGender.Text);
    //put a breakpoint here to look at the value received
}).Start();

And last attempt: try getting all td elements BrowserHelper.WebDriver.FindElements(By.XPath("//td")) . Is Gender element in this list?

Is that your requirement to search by text?

Because here you are searching by element containing text 'M'

//td[contains(text(),'"+ Gender+"')]"

This will return two elements since both these contain text 'M'

<td _ngcontent-c32="" style="vertical-align:middle">M</td>
<td _ngcontent-c32="" style="vertical-align:middle">7 Carioca , Grenville, MD, 29605</td>

I would recommend to change your approach to use other locators (I guess xpath axes can help). If you know one element lets say id and location of gender, dob is static then xpath axes is best approach.

For the unmatched XPath for Gender and DateofBirth you can use the following solutions:

  • Gender :

     IWebElement actualGender = BrowserHelper.WebDriver.FindElement(By.XPath("//td[normalize-space()='" + Gender + "']")); 
  • DateofBirth :

     IWebElement actualDateofBirth = BrowserHelper.WebDriver.FindElement(By.XPath("//td[normalize-space()='" + DateofBirth + "']")); 

You can try fetching the innerText attribute to get the required text, as shown below:

IWebElement actualDateofBirth = BrowserHelper.WebDriver.FindElement(By.XPath("//td[contains(text(),'"+ DateofBirth+"')]"));
Console.Write(actualDateofBirth.GetAttribute("innerText"));

Let me know, whether it works for you.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM