0

I want to access the the last text of a message that contains three numbers that will provide me accesses to a url. I've looked into this solution, but it's not helping.

<div class="alert alert-danger" id="error_explanation">
  <h4>The form contains the following error:</h4>
  <ul>
    <li>
      "Validate branches Another open merge request already exist for this source branch: !323"
    </li>
  </ul>
</div>

I currently have

string sourcePath = ".//div[contains(text(),'Validate branches Another open')][last()]";
string urlPath    = driver.findElement(By.xpath(sourcePath)).getText();
driver.navigate.to(driver.getCurrentUrl()+urlPath);

But I am told that no element exist. I want to access the !323 and attach/navigate to the url

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

To extract the text and strip the part !323 to append it to the url you have to induce WebDriverWait for the visibilityOfElementLocated() and then you can use split() using positive look-ahead by prefixing ?= group on the pattern and you can use either of the following Locator Strategies:

  • cssSelector:

    String urlPath = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.alert.alert-danger#error_explanation>h4 +ul > li"))).getText();
    String[] parts = urlPath.split("(?=!)");
    driver.navigate.to(driver.getCurrentUrl()+parts[1]);
    
  • xpath:

    String urlPath = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='alert alert-danger' and @id='error_explanation']//li[contains(., 'Validate branches Another open')]"))).getText();
    String[] parts = urlPath.split("(?=!)");
    driver.navigate.to(driver.getCurrentUrl()+parts[1]);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Hope this helps!

 String myString= driver.findElement(By.xpath("//li[contains(text(), 'Validate branches Another open')]").getText();
 myString=myString.substring(myString.indexOf("!")+1);

this will return 323

Amruta
  • 1,128
  • 1
  • 9
  • 19