Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 38b2a5d

Browse files
committed
Updated clickToFirstItem to clickElementByIndex method
1 parent fe5b8bb commit 38b2a5d

File tree

1 file changed

+15
-20
lines changed
  • src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords

1 file changed

+15
-20
lines changed

src/main/java/com/github/markusbernhardt/seleniumlibrary/keywords/Element.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -891,26 +891,21 @@ public void xpathShouldMatchXTimes(String xpath, int expectedXpathCount, String.
891891
logLevel);
892892
}
893893

894-
@RobotKeyword("Click to First Item from List Items by Locator.")
895-
@ArgumentNames({"locator", "childLocator=NONE", "message=NONE"})
896-
public void clickToFirstItem(String locator, String... params) {
897-
String childLocator = robot.getParamsValue(params, 0, "");
898-
String message = robot.getParamsValue(params, 1, "");
899-
List<WebElement> elements = browserManagement.getCurrentWebDriver().findElements(By.xpath(locator));
900-
if (elements.isEmpty()) {
901-
if (message.isEmpty()) {
902-
message = String.format("The Element was not found by locator '%s' and child locator '%s'.",
903-
locator, childLocator);
904-
}
905-
throw new SeleniumLibraryNonFatalException(message);
906-
}
907-
WebElement element = elements.get(0);
908-
if (!childLocator.isEmpty()) {
909-
element.findElements(By.xpath(childLocator)).get(0).click();
910-
} else {
911-
element.click();
912-
}
913-
}
894+
@RobotKeyword("Click to element from list elements by locator ``xpath``.")
895+
@ArgumentNames({"xpath", "index=0", "message=NONE"})
896+
public void clickElementByIndex(String xpath, String... params) {
897+
String message = robot.getParamsValue(params, 0, "");
898+
int index = robot.getParamsValue(params, 1, 0);
899+
List<WebElement> elements = elementFind(xpath, false, false);
900+
if (elements.isEmpty()) {
901+
if (message.isEmpty()) {
902+
message = String.format("The Element was not found by locator '%s' with index '%d'", xpath, index);
903+
}
904+
throw new SeleniumLibraryNonFatalException(message);
905+
}
906+
WebElement element = elements.get(index);
907+
element.click();
908+
}
914909

915910
// ##############################
916911
// Internal Methods

0 commit comments

Comments
 (0)