Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 312
Saagar Avatar asked Jul 24 '16 15:07

Saagar


1 Answers

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

like image 111
alecxe Avatar answered Jan 03 '23 12:01

alecxe