1

I'm unable to click a button and an exception is thrown as unable to locate element and exception throws on the marked lines of the screenshot.

The button takes some time to load and I have increased the time to 100 seconds, yet that didn't fix the error.

Code:

public static WebElement viewShipment(WebDriver driver, String mskuType) {
    WebElement noOfShipment = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']"));
    WebDriverWait wait = new WebDriverWait(driver, 15);
    List<WebElement> shipmentList = noOfShipment.findElements(By.tagName("tr"));
    int shipmentCount = shipmentList.size();

    for (int row=1;row<shipmentCount;row=+1)
    {
        WebElement onOfSkuWE= driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[3]"));
        String noOfSku = onOfSkuWE.getText();
        int noOfSkuValue = Integer.parseInt(noOfSku);
        for(int i=0;i<2;i++)
        {
            try{
                if(mskuType.equalsIgnoreCase("single"))
                {
                    if(noOfSku.equalsIgnoreCase("1"))
                    {
                        Thread.sleep(10000);
                        WebElement workOnShipmentWE = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[6]/button"));
                        wait.until(ExpectedConditions.visibilityOf(workOnShipmentWE));
                        workOnShipmentWE.click();
                        break;
                    }
                }
                else if(mskuType.equalsIgnoreCase("multiple"))
                {
                    if(noOfSkuValue>1)
                    {
                        WebElement moreThanOneUnit = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[6]/button"));
                        wait.until(ExpectedConditions.elementToBeClickable(moreThanOneUnit));
                        moreThanOneUnit.click();
                        break;
                    }
                }
            }
            catch(Exception e)
            {
                driver.navigate().refresh();

                e.getMessage();
            }
        }
    }
    return element;
}   

HTML:

<tbody id="fba-core-workflow-shipment-summary-shipment">
    <tr>
        <td>FBA (11/3/16 9:32 PM) - 1</td>
        <td>FBA43K62MB</td>
        <td class="number total-line-items">1</td>
        <td class="number total-quantity">3</td>
        <td>
        <td>
            <button class="amznBtn btn-lg-pri-arrowr" onclick="window.location='#FBA43K62MB/prepare'" style="width: 28ex;" type="button" name="Work on shipment">
                <span>Work on shipment</span>
            </button>
            <p class="action-links content-bottom">
            <p class="action-links">
            <p/>
        </td>
    </tr>
    <tr style="display: none;">
    <tr>
    <tr style="display: none;">
    <tr>
    <tr style="display: none;">
</tbody>
Antti29
  • 2,953
  • 12
  • 34
  • 36
user3782636
  • 83
  • 1
  • 2
  • 13

1 Answers1

1

1.If the page is loading too slow, then try out with the following ExpectedCondition:

 WebElement myDynamicElement = (new WebDriverWait(driver, **10**)).until(**ExpectedConditions.presenceOfElementLocated**(By.id("fba-core-workflow-shipment-summary-shipment")));

Keep the code as the first statement of the viewShipment method. This will make sure that web driver waits for the element (specified by Id, in this case) to load for the configured time (10 seconds in the code). Increase this value if page loading is too slow, as per your requirements.

The advantage of using ExpectedCondition over Thread.sleep is that, whenever an element is found, it will be returned immediately (no need to wait to complete 10 seconds, such as the case in case of Thread.sleep).

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.

  1. If the element to found is, part of an iframe or new window: If the element is inside a frame (check whether the elements you are trying to find, are child elements of an iframe tag), then first find the frame and then switch to it. then use the XPath to find elements.

If the element is in a new window (like opened in new tab when you click a link), then first find the window and then switch to it, and then try to find the element.

References:

  1. http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
  2. https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm
  3. https://stackoverflow.com/a/10887059/2575259
  4. https://stackoverflow.com/a/9597714/2575259
Community
  • 1
  • 1
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • i tried the same and changed the given line to first line of the method. yet the issue happens same. Please suggest me – user3782636 Nov 07 '16 at 11:17
  • to debug further, pause the execution during the run-time (either using 100 seconds sleep or using reading some input by Scanner), and try your xpath on the loaded page (use FireBug plugin) and see whether there are any matching elements. – Naveen Kumar R B Nov 07 '16 at 11:31
  • Yes element are matching on the page but the button takes some extra time to load than button. any idea ? – user3782636 Nov 08 '16 at 04:23
  • as suggested increase from 10 seconds (new WebDriverWait(driver, **10**)) to a higher value such as 120 seconds. as soon as the element is identified, the method will be returned (it won't wait till 120 seconds) – Naveen Kumar R B Nov 08 '16 at 05:33
  • I increased the time but the result is same and there is an iframe tag but that is in different div tag.. Would it be an issue here? – user3782636 Nov 08 '16 at 05:51
  • no. the element should be a child element of the iframe tag, then only you need to switch. what value you specified for timeout? by that time did the page load? – Naveen Kumar R B Nov 08 '16 at 05:53
  • i gave 180 as timeout value and when the execution comes to the line "WebElement workOnShipmentWE = driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr["+row+"]/td[6]/button/span"));" it stays for 180.27 milli sec and throws the exception. I'm totally confused – user3782636 Nov 08 '16 at 06:04
  • It does mean that it is able to find the element "driver.findElement(By.xpath(".//*[@id='fba-core-workflow-shipment-summary-shipment']")" (first line), otherwise, it would have thrown TimeOut exception. – Naveen Kumar R B Nov 08 '16 at 06:17
  • try with single quote : ".//*[@id='fba-core-workflow-shipment-summary-shipment']/tr['+row+']/td[6]/button/span" (at +row+ value) Or store the xpath in a new String variable, then pass it to the method. (I doubt it is the double quotes which are causing the problem) – Naveen Kumar R B Nov 08 '16 at 06:30
  • Tried both the way but result remains same. can't even guess what gone wrong with this. – user3782636 Nov 08 '16 at 06:55
  • i have checked there element should be in DOM and so. would it cause this issue ? any idea ? – user3782636 Nov 08 '16 at 07:07
  • Is the code mentioned in the answer, finding the element (myDynamicElement)? and can you tell me which XPATH is causing the problem? noOfShipment or shipmentList or onOfSkuWE? – Naveen Kumar R B Nov 08 '16 at 07:08
  • onOfSkuWE and noOfShipment works fine problem occurring on workOnShipmentWE xpath – user3782636 Nov 08 '16 at 07:26
  • Are you sure there are 6 td tags? td[6]? in the html above I can see only 5 td tags, where 6th one is incomplete (only is present but not . I would suggest to check the count and ORDER as well – Naveen Kumar R B Nov 08 '16 at 07:41
  • FBA (11/7/16 7:25 PM) - 1 FBA43WZTCC 1 1

    above is the code 6th td has button inside span tag
    – user3782636 Nov 08 '16 at 08:02
  • is the id () value really empty? or you ignored it while commenting?. please share the (updated) HTML code for the table in the question itself with formatting. In the HTML code also, I can see 5th td tag is not ending properly with . better share the screen shot of HTML code from "View Page Source" – Naveen Kumar R B Nov 08 '16 at 08:16
  • i'm unable to attach the screen shot, i have removed the tbody id since it over the character range. – user3782636 Nov 08 '16 at 09:12
  • FB FB 1 1 T
    La, FL

    – user3782636 Nov 08 '16 at 09:13
  • one option try till only td[6] in the XPATH and see whether it is matching any element. another option, try your xpath during run-time (introduce sleep time in minutes, so that execution is paused for some time) on the browser using Firebug plugin , check whether it matching any element. – Naveen Kumar R B Nov 08 '16 at 10:12