简体   繁体   中英

Unable to locate element in Sitecore 8.1 (selenium and c#)

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

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...:)

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM