I want to click at a point inside an iframe using Actions class of Selenium. But the code I'm trying is not clicking. Below is the code:
Actions action = new Actions(driver);
action.MoveByOffset(x-coordinate, y-coordinate).Click().Perform();
I also tried passing the driver like:
Actions action=new Actions(driver.SwitchTo().Frame(0));
But it was giving error and I couldn't find any other way to pass the driver with a frame selected.
The error I'm getting is
Unable to locate frame 0
You couldnt switch to iframe, better way is to assign an iframe object the swithc to it. You need to find the iframe by xpath:
IWebElement iframe = driver.FindElement(By.XPath("//html//body//iframe"));
driver.SwitchTo().Frame(iframe);
Actions action = new Actions(driver);
action.MoveByOffset(x-coordinate, y-coordinate);
action.Click();
action.Perform();
Related
I am getting the following error while trying to click on a button which is present at the bottom of the page. Looks like this error is coming because that button is not visible at the first point, if we scroll the page down then only selenium is able to identify that button.
OpenQA.Selenium.ElementClickInterceptedException : element click intercepted: Element <span>...</span> is not clickable at point (1113, 659)..
I have tried with following code to scroll down the web page but it does not help me.
Actions actions = new Actions(driver);
actions.SendKeys(Keys.ArrowDown);
If I try with element.Sendkeys(Keys.ArrowDown) then also its not helping.
what would be the correct approach here?
You should use Actions class to perform scrolling to the element in this way
Actions actions = new Actions(driver);
var element = driver.FindElement(...);
actions.MoveToElement(element);
actions.Perform();
substitute ... with the selector for the element.
There is also another method relying on javascript
IJavaScriptExecutor js = (IJavaScriptExecutor) driver;
js.ExecuteScript("arguments[0].scrollIntoView();", element);
I wanted to locate the value of BNK1 using Selenium web driver by C#. my local website i found have 2 iframe. i have try to change to get my iframe to change to detail frame for me to get my value of BNK1 in the table.
I had no idea what going on with the problem that i not able locate the iframe by using XPath.
but i having error "no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[#class='tabcontentiframe']"}
IWebElement detailFrame = driver.FindElement(By.XPath("//iframe[#class='tabcontentiframe']"));
driver.SwitchTo().Frame(detailFrame);
IWebElement element = driver.FindElement(By.XPath("//table/tbody/tr/td[contains(text(),'BNK1')]"));
String text = element.Text;
First, let's get sure that your page is fully loaded before trying anything, using a heavy-hand approach,
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Second, assuming the element you want to access is inside an iframe, you first need to switch to it. Actually, you mention in comment that you have two nested iframes,
// Let's switch to the parent frame
IWebElement tabcontentiframe = driver.FindElement(By.XPath("//iframe[#class='tabcontentiframe']"));
driver.SwitchTo().Frame(tabcontentiframe);
// Let's switch to the child frame
IWebElement iframeHome = driver.FindElement(By.XPath("//iframe[#id='iframeHome']"));
driver.SwitchTo().Frame(iframeHome);
Now, you can do
IWebElement element = driver.FindElement(By.XPath("//table/tbody/tr/td[contains(text(),'BNK1')]"));
// or By.XPath("//td[contains(text(),'BNK1')]")
// or By.XPath("//td[text()='BNK1']")
When you are done, you may want to switch back to the main frame,
driver.SwitchTo().DefaultContent();
You can find the element by using the relative xpath:
IWebElement element = driver.FindElement(By.XPath("//td[text()='BNK1']"));
I use Selenium C# binding and I want to click over the addToCart button.
First, I'm waiting the button appears on the page with ExpectedConditions.ToBeClickable.
Then, I need to scroll down the page to be able to click over the button. I used the Selenium.Interactions class but it work as well with js executor.
private By addToCartBy = By.XPath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement addToCart = wait.Until(ExpectedConditions.ElementToBeClickable(addToCartBy));
Actions action = new Actions(driver);
action.MoveToElement(addToCart);
action.Perform();
addToCart.Click();
When I perform this test with Chrome driver, the page loads and the navigator scroll down to the element addToCart and click it but I'm unable to get the button working properly.
When I use Firefox driver, it raise this exception:
OpenQA.Selenium.WebDriverException : '(1435, 998) is out of bounds of viewport width (1920) and height (966)'and I'm unable to click over the button
Most of the time, the click will be performed but no action will follow from this. If I want the button to work properly (go the the cart), I need to put a System.Threading.Thread.Sleep(2000) to be effective or a Console.ReadLine(). Even if I disable the automation click and I do it manually, the button don't always proceed.
How can I make sure my element addToCart is displayed after I moved to Element? in a way to click it when he's ready.
Thank you, Eelke Johnson
For some of the scenarios, you can use not
IWebElement.Click();
but
IWebElement.SendKeys(Keys.Enter);
I had some scenarios, where just Click() didn't work for me.Maybe this will help in your situation.
You need to consider a couple of things.
To invoke MoveToElement(), ExpectedConditions as ElementToBeClickable() is overkill. Instead you can do away with ElementIsVisible().
While invoking Click() as per best practices you need to invoke WebDriverWait inconjunction with ExpectedConditions as ElementToBeClickable().
So effectively, your code block will be:
Actions action = new Actions(driver);
action.MoveToElement(new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button")))).Perform();
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("/html/body/div[2]/div/div/div[1]/div/div[3]/div[2]/div/section[1]/div[2]/aside/div/div[2]/div/div[2]/div/button"))).Click();
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!
Obviously selenium doesn't interact with an webelement until it's in the view, and selenium automatically tries to scroll to that webelement. But in my case when I try to click a particular button it doesn't scroll it in to view, it simply scrolls to a random place in the page.
Objective:
scrolls the webelement into view, and then click that element.
Methods I've already used:
element.Click(); //method 1
Actions actions = new Actions(driver); // method 2
actions.MoveToElement(element);
actions.Perform();
IJavaScriptExecutor js = driver as IJavaScriptExecutor; //method 3
js.ExecuteScript("$('#Id_Body' + element_id)[0].scrollIntoView( true );"); //because the driver scrolls to a random place I use this to get back to the top of the page.
int Y = element.Location.Y, X = element.Location.X;
js.ExecuteScrip($"window.scrollBy( {X}, {Y};");
I'm using selenium 2.48.0, firefoxDriver 43.0.1
Is there a fix for this issue? if someone knows of an older version of selenium/firefox that works fine with one of these methods please tell me, thanks.
Try double MoveToElement
Actions action = new Actions(driver);
action.MoveToElement(elementParent).MoveToElement(elementToClick).Build().Perform();
First move to the element area and than to the element you want to click.
Why don't use scrollIntoView() directly:
js.ExecuteScript("arguments[0].scrollIntoView();", element);