0

I am trying to create a Selenium function that will select a specific article in a Tree component. This article was already searched for in the corresponding searchbar. So it is the only element that is present.

I am trying to create the xPath for this element dynamically based on the ArticleId.

I know there are more simpler methods to select an element e.g. with By.className, By.Id, etc. ... but unfortunately this always selects the same element and does not work in my UseCase. So I try to select the element with the corresponding ArticleId.

My xPath looks like this: //span[contains(text(), 'articleId')]

The problem is that my test case does not find this element. But when I manually search for it in the FireFox console it works.

Heres my function:

public void selectArticleWithSpecificArticleNr(String article) {
    WebElement correspondingArticle = waitTime.until(ExpectedConditions.elementToBeClickable(this.driver.findElement(By.xpath("//span[contains(text(), '" + article + "')]"))));
    correspondingArticle.click();
}

That would be the span element as it is in the DOM:

<span _ngcontent-ewt-c164="" style="font-size: 13px; display: block; word-wrap: break-word; white-space: initial;" class="ng-star-inserted"> 10225929 </span>

I already tried to wait for a while before clicking on this element with Thread.sleep(ms), because maybe the element has not been present in the DOM yet. But that didn't work either.

The funny thing is that the first time I tried to run this test, the function found the element. But after I tried it again and again it didn't.

stax
  • 132
  • 8

1 Answers1

0

Given the HTML:

<span _ngcontent-ewt-c164="" style="font-size: 13px; display: block; word-wrap: break-word; white-space: initial;" class="ng-star-inserted"> 10225929 </span>

You can modify the locator strategy as follows:

public void selectArticleWithSpecificArticleNr(String article) {
    WebElement correspondingArticle = waitTime.until(ExpectedConditions.elementToBeClickable(this.driver.findElement(By.xpath("//span[@class='ng-star-inserted' and contains(., '" + article + "')]"))));
    correspondingArticle.click();
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Sadly that doesn't work ... i get this error: `org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: [[FirefoxDriver: firefox on windows ('ID'] -> xpath: //span[@class='ng-star-inserted' and contains(., '10217672')]] (tried for 7 second(s) with 200 milliseconds interval)` – stax Jun 13 '23 at 12:27
  • @stax Let's discuss the issue in [Selenium](https://chat.stackoverflow.com/rooms/223360/selenium) room. – undetected Selenium Jun 14 '23 at 22:01