I have the following HTML
<div class="dx-filterbuilder-action-icon dx-icon-plus dx-filterbuilder-action" tabindex="0">
::before
</div>
I have tried locating this as follows in Selenium C#
driver.FindElement(By.XPath("//div[contains(#class, 'dx-filterbuilder-action-icon dx-icon-plus dx-filterbuilder-action']")
but the test is failing saying it could not Find Element. I'm trying to click the '+' element shown in the image below.
Not sure what I am doing wrong?
If the element is added dynamically, you need to wait until the element is in the dom.
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
wait.Until(x=> x.FindElement(By.XPath("//div[contains(#class, 'dx-filterbuilder-action-icon dx-icon-plus dx-filterbuilder-action']"));
Instead of putting x.FindElement in the lamba, you can directly do a click. It basically repeats your action until it gets non-null non-exception response, or it timeouts. Giving time for DOM to update.
Related
I am using selenium chromedriver in c# to try and click a print button however I am receiving an exception of "element not interactable", here is the website source of the print button:
<p _ngcontent-c27="" class="print"><span _ngcontent-c27="" class="floatRight">Print</span><img _ngcontent-c27="" class="printImg" src="../../../../../assets/images/Print.svg"></p>
What I've tried:
driver.FindElementById("clippedTab").Click(); // Successfully clicks on the 'Clipped' tab
//None of the below worked:
driver.FindElementByClassName("print").Click();
// and
driver.FindElementByClassName("printImg").Click();
// and
driver.FindElementByClassName("floatRight").Click();
However none of these worked for me.
The website used is bjs.com and the print button and can be found on the Clipped tab.
What am I doing wrong, why is the element not intractable and how could I solve this?
The Xpath you are making is actually causing issue.
All the above Xpaths are not unique and actually point to 2 Xpaths 1 is Print you want to click, and other is not interactable(or hidden) one. Since it is trying to click on hidden one, it is throwing Error.
See image
Simple solution :- use XPath as :: //*#id="clipped"]/div/div[1]/div[3]/div/p/span
The element has to be visible (that's usually what makes it "interactable") before you can Click() it. Are you opening the "Clipped" tab before you Click()?
The desired element is an Angular element so to Click() on the element you have to induce WebDriverWait for the ElementToBeClickable() and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("img.printImg"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//img[#class='printImg']/h2"))).Click();
I'm trying to select an element from a drop-down menu.
On my page, the drop-down items are all spans and looks something like this:
<span class="dropdownlist" id="MyID">
<span class>Option 1</span>
<span class>Option 2</span>
<span class>Option 3</span>
<span class>Option 4</span>
Because the only identifier is the innerText (I don't want to use nth:childs). I have to use XPaths to match against which looks something like this (I escape the " marks as I'm using C#):
//*[#id=\"MyID\"]/span[text()=\"Option 1\"]
When I run this using the Chrome Driver it works perfectly fine. However, when I use the IEDriver or EdgeDriver neither of these can find the element in the list.
All my CSS selectors (and some none text driven XPaths) up until this point in my test work fine, it is simply the drop-down field that is causing my problems.
Im currently starting my IE Driver with no capabilities set, could there be an option there I should be using perhaps?
UPDATE: I've tried using a CssSelector and this returns with an error saying that the element in the list I'm trying to click is not displayed. Running a little assertion to check if the element is displayed before clicking it returns false.
UPDATE 2: I've trying running some Java Script from inside my code to assign an ID to the element and then trying to click it using that, but I'm getting the same error (again, this method works in chrome and the ID is getting assigned properly)
Here's a picture of the control:
Code involves first clicking the field which makes the drop list appear, then selecting the item from the list.
dropDownField.Click();
IWebElement listItemToSelect=
driver.FindElement(By.XPath("//span[text()=\"Bangkok\"]"));
WebDriverWait wait = new WebDriverWait(driver,
TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[text()=\"Bangkok\"]")));
listItemToSelect.Click();
Firebug gives me this for an element inputfiled I want to fill in:
<label for="form:composite:tabView:ssn">Fødselsnummer</label>
Tried this in my selenium script:
WebElement velger = driver.findElement(By.xpath("//input[#id='form:composite:tabView:ssn']"));
Next I do this:
velger.sendKeys(new String[]{"27017833176"});
And then:
WebElement sokknapp = driver.findElement(By.xpath("//*[#id=\"form:composite:tabView:searchSSN\"]"));
sokknapp.click();
To click the serach button.
However when looking at the browser during replay I can se that the "sendkeys" does not work but the button click does work (the inputfield gets red because I press the button for searching without content in the inputfield).
Is there something wrong with this:
velger.sendKeys(new String[]{"27017833176"});
Unable to locate element might means that you need some time to wait before handle element:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var velger = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("form:composite:tabView:ssn")));
Also I'm not sure about correctness of velger.sendKeys(new String[]{"27017833176"});. Try to send simple string:
velger.sendKeys("27017833176");
The problem (or solution) was that I missed i clear() function before sendkeys.
velger.clear();
velger.sendKeys("27017833176");
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!
I am using Selenium in C# to Enter data into textboxes on a webpage:
But i am getting this error:
OpenQA.Selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
I'm using #name, but there are 2 controls on the page with name="MinPrice"
heres the HTML:
<div class="form-group for-sale">
<label>Min Price</label>
<input class="form-control" name="MinPrice" min="0" placeholder="Minimum Price" value="" type="number"></input>
and this is the xpath I'm using:
txtMinPrice = Driver.Instance.FindElement(By.Name("MinPrice"));
I also tried using XPath, but similar results:
txtMinPrice = Driver.Instance.FindElement(By.XPath("//input[contains(#name,'MinPrice') and type='number']"));
If anyone has any type of idea....this is driving me nuts.
ElementNotVisibleException exception occurs when selenium can find an element in the DOM but it is not rendered on the screen.
When I have encountered this error before it has been generally caused by one of three things:
Selenium is trying to interact with an object that is present in the DOM but has not yet rendered on the screen, in which case you might consider adding some type of delay. (Avoid sleep if you can but it is useful for debugging)
The element is below the visible screen, in which case you would need to scroll to interact with it.
There is an overlapping element that is blocking the display of the element.
Add a sleep(10) in to make sure everything on the page has loaded first before any user actions are preformed. If that doesn't work also add
driver.manage().window().maximize() at the start of your test to make sure all the page elements is in view.
If that doesn't work its your xpath. Try something like //*[#class="form-group for-sale"]/input
Or use the Firefinder add on in mozilla firefox to check your xpath is valid and exists on the page.
Selenium is good at scrolling down to view an item, but when it comes to Scrolling back up it's a PiA, and usually throws that exception. I usually just do something like
element.SendKeys(Keys.Home);
Thread.Sleep(100);