2

I am new to Selenium WebDriver tests and I try to use it at work. I tried many combinations of selectors, xpaths and so on, but I can't move past it. I searched also many similiar topics on stackoverflow, sadly without expected results. What I need is to be able to click on "NO SERVICE" button (a href). When I try, I keep getting error, that this element is not visible. When I try to handle this error using "wait"s, I keep getting another error "Expected condition failed: waiting for visibility of element...". What am I doing wrong?

My code:

    WebDriverWait waitWait = new WebDriverWait(driver, 40);     
    waitWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("withoutService")));      
    WebElement x = driver.findElement(By.className("withoutService"));
    x.click();

and also a html code snippet from webpage:

<div id="fancybox-outer">
    <div id="fancybox-content">
        <div style="width:auto;position:relative;">
            <div id="serviceReminder" style="width: 765px">
                <form id="serviceReminderFrom" method="post">
                    <div class="homeMessage">
                        <div class="innerMessage">
                            <input type="hidden" id="serviceToAddReminderFromAction" name="F_ACTION" value="">
                            <input type="hidden" id="itemsWithServices" name="itemsWithServices" value="">
                            <input type="hidden" name="eventTypeName" value="Something">
                                <div class="ServicesDelivery"><span class="disable-button"></span>
                                    <a href="javaScript:void(0);" rel="3" class="withoutService btn btn-fourth" onclick="registerButtonClickOnPopup('NO SERVICE'); setTimeout(function(){registerButtonClickOnPopup('NO SERVICE');},400);">NO SERVICE</a>
                                    <a href="javascript:void(0)" rel="1" class="next js-tooltip btn btn-second" onclick="registerButtonClickOnPopup('ADD SERVICE'); setTimeout(function(){registerButtonClickOnPopup('ADD SERVICE');},400);">ADD SERVICE</a>
                            <div class="none">
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                </div>
            </form>
        </div></div></div><a id="fancybox-close" style="display: inline;"></a><div id="fancybox-title" class="" style="display: none;">
        </div><a href="javascript:;" id="fancybox-left"><span class="fancy-ico" id="fancybox-left-ico"></span></a><a href="javascript:;" id="fancybox-right"><span class="fancy-ico" id="fancybox-right-ico"></span></a></div>
Joel Conan
  • 23
  • 1
  • 4
  • The element you're looking for is contained within an input that is `hidden`, are you doing something within your test to make the input not hidden? – Josh Feb 14 '17 at 12:41
  • @Josh, no, it's not a part of hidden `input`. AFAIK, `input` element can contain attributes only, but not other nodes. – Andersson Feb 14 '17 at 13:03

2 Answers2

0

Your locator By.className("withoutService") can match several elements. You need more specific selector. Try below code:

WebDriverWait waitWait = new WebDriverWait(driver, 40);     
WebElement x = waitWait.until(ExpectedConditions.elementToBeClickable(By.linkText("NO SERVICE")));      
x.click();
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • I don't think the errors he's receiving indicate that it's an issue with multiple elements meeting the criteria – Josh Feb 14 '17 at 12:44
  • sorry, I edited your answer by mistake. you can reject it if you want – Naveen Kumar R B Feb 14 '17 at 13:03
  • 1
    @Naveen, That's ok :) But you should note that if element located inside an `iframe`, one might get `NoSuchElementException`, but not `ElementNotVisibleException` – Andersson Feb 14 '17 at 13:06
  • 1
    You are correct :). But I am not so sure in case of `ExpectedConditions`, as the method used is `visibilityOfElementLocated`. – Naveen Kumar R B Feb 14 '17 at 13:07
  • Just confirmed. I gave wrong locator, but still getting `Expected condition failed: waiting for visibility of element located by By.xpath` – Naveen Kumar R B Feb 14 '17 at 13:13
  • I think this is because we're trying to wait for a visibility of link. In this case better to use `elementToBeClickable`. Hm... however, it works for me actually. Both wait for clickability and visibility... – Andersson Feb 14 '17 at 13:19
  • So far only this solution works, so I will mark it as a good one. Thanks to everyone for your time guys! :) – Joel Conan Feb 14 '17 at 13:59
0

Try the following XPath:

//a[contains(@class, 'withoutService')]

Complete Code:

WebDriverWait waitWait = new WebDriverWait(driver, 40);     
WebElement x = waitWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@class, 'withoutService')]")));     
x.click();

If above code is NOT working, then the element might be inside an iframe. please look my detailed answer here.

Community
  • 1
  • 1
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • 1
    Actually `withoutService` is complete class name that can be used with search `By.className()`. I think, you confuse *complete* and *compound* class names – Andersson Feb 14 '17 at 12:56