3

I'm trying to run some automated tests in Sitecore 8.1 using Chrome and Selenium and c#. My code doesn't want to find any elements within the Sitecore pages, specifically the experience editor. I am encountering the "unable to locate element" warning.

For eg: an item I want to .Click() is the toolbar ribbon button to expose the toolbar menu. Here's the element:

<a data-sc-id="QuickRibbon" data-sc-click="trigger:button:toggleshow" data-sc-command="" data-sc-сontrolstaterequest="" data-sc-controlstateresult="" data-sc-postponedcall="" data-sc-ispressed="false" class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered" title="Toggle the ribbon." data-sc-pagecodescriptfilename="" data-bind="ispressed: isPressed, visible: isVisible, click: click, command: command, enabled: isEnabled" data-sc-require="/-/speak/v1/ribbon/QuickbarButton.js" href="#" style="float:right"><img src="/sitecore/shell/client/Speak/Assets/img/Speak/Common/16x16/white/navigate_down.png" alt="Toggle the ribbon."></a>

Here's its XPath:

/html/body/div/div/div[1]/nav[1]/a[3]

I have extended the wait time to allow it to become visible as it can take a few seconds to load these pages. But this didn't work.

I have tried:

driver.FindElement(By.XPath("/html/body/div/div/div[1]/nav[1]/a[3]/img")).Click();

which gave me the "unable to locate" error

driver.findElement(By.className("class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered"")).Click();

which gave me an error about unable to use compounded classnames.

I've tried a whole host of other options/combinations trying to pick up the alt text etc but I just can't get it to pick up the element.

Any ideas? Let me know if you need any more info.

Thanks

2 Answers2

3

Change this line

driver.findElement(By.className("class="sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered"")).Click();

to below line :-

driver.findElement(By.className("sc-quickbar-item sc-quickbar-button sc_QuickbarButton_53 data-sc-registered")).Click();

Edited 1..

If compound class does not work here you can perform action by using xpath as below :-

var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.xpath("//a[@data-sc-id='QuickRibbon']")));
clickableElement.Click();

Edited 2..

You need to switch frame before perform action if your element is present inside a frame as below :-

driver.SwitchTo().Frame("your frame name or id");

Hope it will work...:)

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • Hi, Thanks, tried that and it still gives me the "compound class names not permitted". When I last looked at this "compounding" error, it suggested to take out the spaces and replace them with full stops. But that didn't work. Another mentioned to use commas then full stops but both of these options returned the "unable to locate element". – geneticmaterial Jun 07 '16 at 10:41
  • @geneticmaterial that's because compound class names are not supported by the `.className` method. See http://stackoverflow.com/questions/20361643/ – Dmytro Shevchenko Jun 07 '16 at 10:42
  • @Software_Engineer - still unable to locate. – geneticmaterial Jun 07 '16 at 10:56
  • 1
    @geneticmaterial have you implemented WebDriverWait??... and be sure the element which you want to locate and click is not inside any frame?? – Saurabh Gaur Jun 07 '16 at 10:58
  • Hi, yes, I tried both option you supplied. The WebDriverWait timed out. – geneticmaterial Jun 07 '16 at 11:04
  • 1
    @geneticmaterial and what about frame?? here is any frame or not?? – Saurabh Gaur Jun 07 '16 at 11:06
  • @Software_Engineer it appears to be in an iframe with id="scWebEditRibbon" – geneticmaterial Jun 07 '16 at 11:06
  • 1
    @geneticmaterial Yeah.. that is the problem...You need to switch that frame first then going to find element... – Saurabh Gaur Jun 07 '16 at 11:22
1

following suggestions from @Software_engineer I have managed to write this which works:

Thread.Sleep(6000);
driver.SwitchTo().Frame(driver.FindElement(By.Id("scWebEditRibbon")));
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
var clickableElement = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@data-sc-    id='QuickRibbon']")));
clickableElement.Click(); //click to drop down the toolbar
driver.SwitchTo().DefaultContent();

I needed to switch to the iframe!