Nicole Phillips
Nicole Phillips

Reputation: 763

Clicking outside an element does not work

I am trying to select an element from a list that will update fields upon a clicking outside of the list. The below code works if I only have one event (see picture)

Actions action = new Actions(driver);
EventGrid.FindElement(By.XPath("//div[contains(.,'" + nameToBeClicked + "')]")).Click();
// Forces a click outside element to fire the triggers
EventGrid.FindElement(By.XPath("//html")).Click();

Notice only one event under the name catgory

But will not work when I have two events (notice there are two events in the picture now, one new event and the other called buffet breakfast). I pass in the event name to the method which does click the buffet breakfast from the drop down however the click to html only works with the above picture but not the below. Any ideas how to get around this?

enter image description here

Html of the list:

<div id="boundlist-1193" class="x-boundlist x-boundlist-floating x-layer x-boundlist-default x-border-box" tabindex="-1" style="right: auto; left: 100px; top: 97px; height: auto; width: 150px; z-index: 29001;" data-selenium-id="EventGrid-EventClassificationName-list">
  <div id="boundlist-1193-listEl" class="x-boundlist-list-ct x-unselectable" style="overflow: auto; height: auto;" role="presentation">
    <ul class="x-list-plain">
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>Buffet Breakfast</div>
      </li>
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>NotMarkedAsPosted</div>
      </li>
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>Package Afternoon Break</div>
      </li>
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>Package Dinner</div>
      </li>
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>Package Lunch</div>
      </li>
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>Package Morning Break</div>
      </li>
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>Party Hard</div>
      </li>
      <li class="x-boundlist-item" unselectable="on" role="option">
        <div>Unassigned</div>
      </li>
    </ul>
  </div>
</div>

Upvotes: 1

Views: 5237

Answers (4)

Scott
Scott

Reputation: 63

I found the easiest way is just to click something that has no interaction i.e. body attribute...this will always be there.

If you were wanting to mimic user behaviour for your test then you would click or tab to the next fields or user actionable element - but for the sake of just validating a field the below method would suffice.

    Class PageElements {
        public static By body = By.XPath("//body");
}

driver method:

driver.FindElement(PageElements.body).Click();

Upvotes: 0

pr4bh4sh
pr4bh4sh

Reputation: 714

From what I understand, Your objective is to get onfocusout event triggered. I had this issue and this solves my problem

driver.find_element_by_tag_name('body').send_keys(Keys.TAB)

what it does is sends tab key event to body,(you can use html as tag if you wish). I use this soution as it's the most prefered behaviour of the actual user.

Upvotes: 0

Nicole Phillips
Nicole Phillips

Reputation: 763

I have achieved the click by using the following code. It seems when there are multiple events the html tag falls off and becomes remote. Please review the code below. I also removed the string parameter as when I type the name it only one member of the list returns.

EventGrid.FindElement(By.XPath("//li/div")).Click();
RemoteWebElement element = (RemoteWebElement)driver.FindElement(By.XPath("//html"));
var scrollIt = element.LocationOnScreenOnceScrolledIntoView;
element.Click();

Upvotes: 1

ievche
ievche

Reputation: 1805

You can try to perform click by coordinates after you have selected your option.

action.MoveByOffset(x, y).Click().Perform();

Upvotes: 1

Related Questions