OldMan OldMan - 3 months ago 30
Java Question

Selenium / getElements By XPATH and CONTAINS (JAVA)

I have a list of dynamic links with also dynamic

IDs
,
CSS
and all other options to get a list of elements are not useful expect one way. Using
contains()
. But in which way?

The list contains
XPATH
values like this:

//a[@id='contentDataTable:4711:startMenu']/span
//a[@id='contentDataTable:4712:startMenu']/span
//a[@id='contentDataTable:4713:startMenu']/span
..
//a[@id='contentDataTable:4714:startMenu']/span


Matches I can't use, because of the
XPATH 1.0
support only with Selenium. So I am searching for the correct
contains()
call...

A first idea was

xpath=//a[contains(@id, 'contentDataTable:')]


but it don't work.

How I've tried to get elements:

public List<WebElement> getWebElements(Boolean isDebug, Boolean isHPOMDebug, RemoteWebDriver remWebDriver, String htmlType, String htmlValue, String actField) {
List<WebElement> webElements = null;
String webRequest = "";

try {
switch (htmlType) {
case "XPATH":
webRequest = htmlValue;
webElements = remWebDriver.findElements(By.xpath(webRequest));
break;

case "LINKTEXT":
webRequest = htmlValue;
webElements = remWebDriver.findElements(By.linkText(webRequest));
break;

case "LINKTEXTPART":
webRequest = htmlValue;
webElements = remWebDriver.findElements(By.partialLinkText(webRequest));
break;

case "CSS":
webRequest = htmlValue;
webElements = remWebDriver.findElements(By.cssSelector(webRequest));
break;

case "TAG":
webRequest = htmlValue;
webElements = remWebDriver.findElements(By.tagName(webRequest));
break;

case "ID":
webRequest = actField;
webElements = remWebDriver.findElements(By.id(webRequest));
break;

case "NAME":
webRequest = actField;
webElements = remWebDriver.findElements(By.name(webRequest));
break;


default:
webElements = null;
} // END OF INNER SWTICH

if (isDebug && webElements != null && webElements.size() > 1) {
logger.log(LogStatus.INFO, "getWebElements: " + webRequest + " FOUND: " + webElements.size());
for (int i=0;i<webElements.size(); i++) {
logger.log(LogStatus.INFO, "getWebElements: (" + i + ") TEXT = [" + webElements.get(i).getText() + "]");
logger.log(LogStatus.INFO, "getWebElements: (" + i + ") TAG = [" + webElements.get(i).getTagName() + "]");
logger.log(LogStatus.INFO, "getWebElements: (" + i + ") LOC = [" + webElements.get(i).getLocation() + "]");
}
report.flush();
}

}
catch (Exception e) {
e.printStackTrace();
webElements = null;
}

return(webElements);
}

Answer

Actually your xpath is correct and your Java code for the method getWebElements(Boolean isDebug, Boolean isHPOMDebug, RemoteWebDriver remWebDriver, String htmlType, String htmlValue, String actField) to getting elements also looks ok.

Line which you commented out :-

I got an null pointer exception... I check this again, perhaps I am an idot

You're getting NullPointerException because you're initialising very first webElements variable as null which is wrong.

You should change this line :-

List<WebElement> webElements = null;

To

List<WebElement> webElements = new ArrayList<WebElement>();