0

I have The following element in my web page:

<button id="btnPackageDownloadtopPackageTable1dde0fcb2-948b-4bbc-b347-40deb2f6034e"
 title="Download package" ng-style="{'display': ''}" 
 ng-show="!PackageManagementCtrl.downloadStartedForPackage['topPackageTable1dde0fcb2-948b-4bbc-b347-40deb2f6034e']" 
 phd="{&quot;action&quot;: &quot;enable&quot;, &quot;code&quot;: 3002}"
 phd-unit="Bank(Y)" class="btn action-btn" style="margin-left: 5px;" type="button" 
 ng-click="PackageManagementCtrl.downloadPackage('dde0fcb2-948b-4bbc-b347-40deb2f6034e',
 'Pack_110','topPackageTable1dde0fcb2-948b-4bbc-b347-40deb2f6034e')">
<i class="fa fa-download"></i>  Download</button>

This is the CSS expression I'm passing to my Selenium code:

[phd-unit='Bank(Y)'][ng-click*='Pack_110'][ng-click*='downloadPackage']

My code:

 @Test
public void findElementByCss() {

    softAsserter = new SoftAsserter(testParameters);
    String CssExpresion = testParameters.get("actionData");

    WebElement optionToFind = null;
    try {
        WebDriverWait wait = new WebDriverWait(browser, 5);
        optionToFind = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(CssExpresion)));
    }
    catch (Exception ex){
        softAsserter.fail(ex.getMessage());
    }
    if (optionToFind==null) {
        softAsserter.fail("failed to failed element with expression: " + CssExpresion);
    }
    if (optionToFind!=null) {
        softAsserter.assertTrue(true, "Element found by attributes: </br>" + CssExpresion);
    }
    if (softAsserter.isOneFailed()) {
        asserter.fail("findElementByCss failed");
    }
}

Why my code fails in finding this specific element?

1
  • Why do you think it didn't find the element? remove the try catch to get the error message. You should also check if it's inside iframe. Commented Jan 15, 2020 at 15:05

2 Answers 2

2

I would suggest use the following css selector.

button.btn.action-btn[title='Download package']

OR

button.btn.action-btn[title='Download package'][phd-unit='Bank(Y)']
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but there might have very similar elements. I must know that the element has 'Pack_100' and 'Bank(Y)'. Other Elements will have Bank(Z) etc'
@TalAngel : How about the second one?does that helps.
0

The problem was it:

ExpectedConditions.visibilityOfElementLocated

changed it to:

ExpectedConditions.presenceOfElementLocated

And it works like a charm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.