Saagar
Saagar

Reputation: 834

org.openqa.selenium.InvalidSelectorException - [object Text]. It should be an element

I am trying to retrieve values email1, Email2, and HT from following using XPATH

<table class="table-striped table-bordered" cellspacing="0" style="table-layout: fixed; width: 100%;">
<colgroup>
<tbody>
<tr class="GPEHNV5BGF GPEHNV5BNF" __gwt_subrow="0" __gwt_row="0">
<td class="GPEHNV5BFF GPEHNV5BHF GPEHNV5BIF GPEHNV5BOF">
<div __gwt_cell="cell-gwt-uid-515" style="outline-style:none;">email1</div>
</td>
<td class="GPEHNV5BFF GPEHNV5BHF GPEHNV5BOF">
<td class="GPEHNV5BFF GPEHNV5BHF GPEHNV5BCG GPEHNV5BOF">
</tr>
<tr class="GPEHNV5BFG" __gwt_subrow="0" __gwt_row="1">
<td class="GPEHNV5BFF GPEHNV5BGG GPEHNV5BIF">
<div __gwt_cell="cell-gwt-uid-515" style="outline-style:none;">Email2</div>
</td>
<td class="GPEHNV5BFF GPEHNV5BGG">
<td class="GPEHNV5BFF GPEHNV5BGG GPEHNV5BCG">
</tr>
<tr class="GPEHNV5BGF" __gwt_subrow="0" __gwt_row="2">
<td class="GPEHNV5BFF GPEHNV5BHF GPEHNV5BIF">
<div __gwt_cell="cell-gwt-uid-515" style="outline-style:none;">HT</div>
</td>
<td class="GPEHNV5BFF GPEHNV5BHF">
<td class="GPEHNV5BFF GPEHNV5BHF GPEHNV5BCG">
</tr>

Following is my code with XPATH:

int rows=driver.findElements(By.xpath("//table[@class='table-striped table-bordered']/tbody/tr")).size();
        List<String> EmailConnectorRoutineList= new ArrayList<String>();
        for(int i=0;i<=rows;i++){
            String referenceName=null;
            referenceName=driver.findElement(By.xpath("//table[@class='table-striped table-bordered']/tbody/tr[@ __gwt_row='"+i+"']/td[1]/div/text()")).getText();

            //referenceName=refName.getText();
            EmailConnectorRoutineList.add(referenceName);
        }

I tried following XPATH as well

//table[@class='table-striped table-bordered']/tbody/tr[@ __gwt_row='"+i+"']/td[1]/div/text()")).getAttribute("value");

Showing following error:

org.openqa.selenium.InvalidSelectorException: The given selector //table[@class='table-striped table-bordered']/tbody/tr[@ __gwt_row='0']/td[1]/div/text() is either invalid or does not result in a WebElement. The following error occurred: InvalidSelectorError: The result of the xpath expression "//table[@class='table-striped table-bordered']/tbody/tr[@ __gwt_row='0']/td[1]/div/text()" is: [object Text]. It should be an element.

Upvotes: 5

Views: 24639

Answers (2)

V-cash
V-cash

Reputation: 368

I don't know what language are you using, but sure you need to omit /text() in xpath
AND if you want the text you've to ad something like .get or .get() at the end

//....tbody/tr[@ __gwt_row='"+i+"']/td[1]/div")).get();

Upvotes: 0

alecxe
alecxe

Reputation: 473833

You cannot point XPath expressions to text nodes in Selenium. Omit the /text() part at the end.

Upvotes: 14

Related Questions