0

I have a drop down menu which has the text "My Menu". Unfortunately the same drop down is used for another purpose with the text "My Second Menu". I need to click on this "My Menu" drop down and it looks like the following.

<?xml version="1.0" encoding="UTF-8"?>
<div class="c-composedDropdown u-display--flex u-align-items--center u-color--white u-cursor--pointer u-border-radius">
  <div class="u-display--flex u-align-items--center">
     <span class="flag-icon flag-icon-lg flag-icon-ie composedDropdown__subTitle__icon u-color--white" />
     <div>
        <div class="u-display--block u-display--flex u-align-items--center">
           <span class="u-font-weight--bold u-margin-right--medium">My Menu</span>
           <svg xmlns="http://www.w3.org/2000/svg" baseProfile="tiny" width="24" height="24" fill="currentColor" color="#37454D" viewBox="0 0 24 24" class="c-composedDropdown__arrow-icon u-color--blue-lighter">
              <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M9.5 7l5 5m-5 5l5-5" class="svg-color--primary" />
           </svg>
        </div>
        <p class="c-composedDropdown__subTitle u-display--block">Sample</p>
     </div>
  </div>

At the moment, I'm using XPath to capture this and it looks like the following.

//*[contains(@class, 'c-composedDropdown') and contains (.,'My Menu')]

The test scenario for this passes fine whenever I run it alone. But when the entire test suite runs, randomly this test fails. That too when running in headless mode. Is there something I can do about the way I pick this element to be clicked? Will that help the test not so flaky? Any suggestions would be much appreciated.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118

2 Answers2

0

Try following Xpath Hope this will work.

//div[contains(@class,"c-composedDropdown")]//following-sibling::span[text()="My Menu"]
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • Just out of curiosity as I'm still learning deep about XPaths, how would this help with the flakiness of the test at the moment? :) – AnOldSoul Apr 24 '19 at 15:15
  • @mayooran : Not sure though whether it will work in headless mode.Try with interactive mode first check whether it works.if works then try in headless mode.Let me know how it goes? – KunduK Apr 24 '19 at 15:47
0

To click() on the element with text as My Menu you need to induce to induce WebDriverWait for the element_to_be_clickable() and you can use either of the Locator Strategies:

  • cssSelector:

    "div.c-composedDropdown span.u-font-weight--bold.u-margin-right--medium"
    
  • xpath:

    "//div[contains(@class, 'c-composedDropdown')]//span[@class='u-font-weight--bold u-margin-right--medium' and text()='My Menu']"
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I cannot use the above CSS selector as there's also another drop down with the same properties but has a different text "My Second Menu" as I've mentioned in the question :) – AnOldSoul Apr 25 '19 at 09:50
  • @mayooran If you can update the question with both the similar _DropDown_ nodes possibly we can workout for an effective _CssSelector_ as well. Can you update about the result using the _xpath_ within my answer? – undetected Selenium Apr 25 '19 at 09:51
  • XPath seems to be incorrect, gives me an unable to locate the element all the time :( – AnOldSoul Apr 25 '19 at 09:57
  • Possibly you haven't added the much needed **WebDriverWait** which would be crucial for your usecase. – undetected Selenium Apr 25 '19 at 09:59