Selenium webdriver actions on element will be executed in wrong location - c#

I'm trying to build my first test with selenium and got a problem.
I'm searching for a element, no problem. I can click on it, get the
text in the element... every thing works fine.
But double click on the element just doesn't work. Selenium
clicks in the wrong location. I made a screenshot of this situation:
Screenshot
To find the row i use xpath and search for the text in the cell, but this text is unique(I checked it)
private readonly string _identityPath = ".//td[.= 'All Employees']";
...
mainPage.FindElement(By.XPath(_identityPath)).Click(); //Works(dotted box)
Actions builder = new Actions(mainPage);
IAction doubleClick = builder.DoubleClick(mainPage.FindElement(By.XPath(_identityPath))).Build();
doubleClick.Perform(); //wrong location/element
/*
Actions action = new Actions(mainPage);
action.DoubleClick(mainPage.FindElement(By.XPath(_identityPath)));
action.Perform(); *///wrong location/element
This page is in an iframe and the grid is a dojo component... maybe the problem
comes from there. Any ideas whats wrong? I have no idea where this is coming from. :/
Greets

It is common issue that Actions builder does not work.
Using JavaScript should help - find answer in this thread:
Selenium 2/Webdriver - how to double click a table row (which opens a new window)

If the element is in an iframe, you need to switch to that iframe in order to interact with the element.

Related

Click Dynamic button with Selenium (Pinterest/TailwindApp)

I am trying to automate the scheduling of pinning items to Pinterest using Tailwindapp.com. I am using a console app in .NET (C#) with Selenium Chromedriver. I start up the browser and enable the tailwind extension and login to tailwind. Then I go to the site I am trying to pin images from, get to the product page, search for the button and attempt to click it. That's where it falls apart. The 'Schedule' button in Tailwind appears over all images on the page as you hover. When I do an XPath search, it only returns a single button for the whole page (the console line below shows 1).
public static void ClickScheduleButton(IWebDriver driver) {
// get all the buttons and then use the first one
IList<IWebElement> buttons = driver.FindElements(By.XPath("//*[#id='tw_schedule_btn']"));
Console.WriteLine("Number of items found: " + buttons.Count());
IWebElement scheduleButton = buttons.ElementAt(0);
Actions actions = new Actions(driver);
actions.MoveToElement(scheduleButton).Click().Perform();
}
On the perform method, I get the following error: OpenQA.Selenium.WebDriverException: 'javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
From what I've read, this seems to mean that there are more than 1 element but I seem to have ruled this out with the number of buttons found. I have attempted to do a wait to make sure it's available but I do not believe that is the issue.
I have tried to find an example just trying to do this with a Pinterest button because in theory it would be the same logic but I cannot find anything for that either.
My assumption is that it's a problem just getting the button to appear on the correct image? But that's just a guess.
If I'm understanding this correctly you want to click the button who appears in when you hoover the mouse in the image.
If you can hoover the mouse and wait until the expected xpath appears should solve it.
protected virtual void HooverMouse(By element)
{
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(element)).Build().Perform();
}
The element will be the first image you have, so you must change your driver.findelements("the button") to the "images" and loop arround this.
In pseudo code will be:
Hoover(image) -> WaitButtonAppears -> ClickButton

Click event not working for Firefox but is for chrome

The click event is not firing in firefox but works ok in chrome.
The test fails with the error: "Element not found on page."
Below is the code and HTML for the button I want to click.
Browser.ElementClickById("ctl00_ContentPlaceHolderBody_lvProducts_ctrl0_ctrl1_btnAddProductToCart_input");
and inside the elementclickbyid i have:
driver.FindElement(By.Id(elementID)).Click();
HTML code is:
event
You could try working around with a Javascript click.
// declare JS executor
var executor = (IJavaScriptExecutor)Driver;
// locate the input
var input = Driver.FindElement(By.XPath("//input[#type='submit']"));
// execute JS to click
executor.ExecuteScript("arguments[0].click();", input);
I've seen cases where regular Click(); does not work across browsers -- these cases are rare, but using JS click usually works across multiple browsers when I run into this issue.
driver.findElement(By.xpath("//input[#type='submit']")).click();
i am sure you are trying to use browser class to keep your methods there, but try to use xpath not id. just use this code to click what you need. don't use page object model or anything else. don't save it in your browser class under click method. just in your main code use this code to click. and before to run it make sure that you have only one type submit. if its gonna show to you 2 types then use this code
driver.findElement(By.xpath("//input[#type='submit'][1]")).click();
number 1 says click to first submit if the button which you need second then follow the logic and change the number to 2
driver.findElement(By.xpath("//input[#type='submit'][2]")).click();
for better answer share your code class and also URL where you are trying to click button and also which element you are trying to click

WebElement (Radio Button List) cannot be found in Internet Explorer

I have a WebElement in which cannot be found in IE 10 but works in FF 47.1 and Chrome 51.
[FindsBy(How = How.XPath, Using = "Foo's XPath")]
IWebElement Foo = null;
At runtime I have also called
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("Foo's XPath")));
But am receiving a NullReferenceException for IE10 Only. I have even tried a sleep as well. Can someone please advise me what else I can do b/c my program has to run IE.
As of the time this question was asked we have upgraded to IE 11 but am still seeing the exact same issue. I can't provide specific html code but I want to elaborate a little on the webelement foo.
The radio button which I have called "foo" is wrapped in the following
div
table
tbody
tr
td
input id = "foo"
When I call the xpath (I have tried id and css selector as well)
.//*[#id='foo_rblArgType_4']
the application itself defaults to the 1st value of the radio buttons and I can't seem to get it to select the 4th value which I need. Can anyone offer advice or suggestions of something else I can try?
Depending of what elements are siblings you can have selectors like:
//tr[4]//*[contains(#id, 'foo')]
//td[4]/*[contains(#id, 'foo')]
//*[contains(#id, 'foo')][4]
You can also use the entire id if is not changing by replacing [contains(#id, 'foo')] with [#id='foo']
If none of this are working then please provide the structure for all 4 inputs.
Try identifying element by id.
[FindsBy(How = How.Id, Using = "foo")]
IWebElement Foo = null;
Also make sure that the element is not hidden. Also try increasing the size of the element as sometimes web-driver is not able to locate element.
If you still have issues performing any operations on radio button, try using JavaScriptExecutor.
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html

WebDriver - element is not clickable Chrome

I have following problem. I run test on Firefox and Chrome. On Firefox test run correctly but on Chrome SauceLabs give a message:
unknown error: Element is not clickable at point (717, 657). Other
element would receive the click: <div class="col-md-9 col-sm-12"
style="margin-top:8px;">...</div> (Session info: chrome=36.0.1985.125)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.3 x86_64)
I choose element by unique css selector in both test in the same way:
driver.FindElement(By.CssSelector("button.btn-xs:nth-child(1)")).Click();
Any ideas what is wrong here?
I am assuming that you have the correct element you need, ie the XPath is correct.
Here are few ways out:
Try to Click on the parent element instead.
Try .Submit() instead of .Click()
Try to execute the JavaScript that will be executed on the OnClick event of the element you are trying to click.
I have used the 3rd way with success all the time.
Another one
Do a .SendKeys(Keys.Enter) on that element (or a Space key)
Since you've tagged the question as Google-Chrome too - I suppose that this is happening mostly with ChromeDriver. I had the same issues with one of my previous projects (Asp .Net MVC). I found that when some elements are not visible for this Driver if they are not in the screen_visible_area. Please note that they are loaded (HTML, CSS3, JS etc.) properly.
So after a lot of reading and testing, I found that my workaround is simply scroll to the WebElement - so it is in the visible part of the screen. Actually this issue was not for all elements and I didn't find better solution for it.
unknown error: Element is not clickable at point (..., ...)
Is not descriptive error for this case, because like you I also thought that is Selector-related.
Just to be full answer - I had the same problems with IEDriver too. My implementation was to use the Browser scroll down/up options and just "send the screen" where the problematic element is.
Simple JSExecutor code that you can use:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(110,350)", "");
or
jse.executeScript("scroll(0, 250);");
or
driver.executeScript("window.scrollBy(110,350)", "");
Other topic-related useful resources are here.
Update
When it comes to the .sendKeys() I also used the browser accessibility features. All you need to do is just count how many TAB clicks your test need in order to get to the targeted web_element. Then just call .click().
Try this simple code:
element.sendKeys(Keys.TAB);
or
element.sendKeys("\t")
or
Actions builder = new Actions(driver);
builder.keyDown(Keys.TAB).perform()
I realize this is a super old question, but it came up while searching a nearly identical problem in the present day. After attempting many of the fixes described here and getting new exceptions for my trouble (mostly stale element and http request timeouts) I stumbled across this issue on Selenium's GitHub.
As described in the post, Chrome had advanced beyond the abilities of my version of chromedriver.exe--my v2.30 driver had known issues with clicking elements due to changes in Chrome v61 scrolling mechanics. Updating to the latest chromedriver.exe solved all my problems.
tl/dr: ensure your version of chromedriver is compatible with the version of Chrome being tested.
I was getting issue that login button is not clickable in chrome even xpath was correct. after browsing many sites, i came to the solution - use .submit() instead of .click() and it worked perfectly.
driver.findElement(By.xpath("//button[#id='loginBtn']")).click();
driver.findElement(By.xpath("//button[#id='loginBtn']")).submit();
If you're doing anything complicated in your CSS, Chrome can get confused (this has happened to me) and think that the element you're trying to click is covered by another element even though this is not the case.
One way to resolve this is to help Chrome understand the situation correctly by adding z-indexes (you'll need to add relative or absolute positioning also) to unambiguously place the correct element on top of the other.
For me, it was creating this command instead.
driver.FindElementByXPath("id('gender1')").SendKeys(Keys.Space);
For my case, I was interacting with radio control.
I have had this problem on FF. This issue happens when your field is not in the view area. The slick way to resolve this issue is to zoom out your browser:
TheNotClickableField.SendKeys(Keys.Control + "-" + "-");
you might want to zoom out more or less according to your page size.
I.
If the button is on the bottom of the page, the following code could be used to get you to the bottom via JavaScript from where the click can be executed:
(driver as IJavaScriptExecutor).ExecuteJavaScript("window.scrollTo(0,document.body.scrollHeight - 150)");
The same action could be done via C# with the Actions class provided by Selenium.
This will get you to the bottom of the page----->
new Actions(Driver).SendKeys(Keys.End).Perform();
Keys.End could be switched with Keys.PageDown // Keys.Space
II.
If you want to get to the exact position of the element you can:
1.Get the element's Y location---> var elementToClick = driver.findElement(By.{Anything}(""));
2.Execute the following JS---> (driver as IJavaScriptExecutor).ExecuteScript(string.Format("window.scrollTo(0,{0})", elementToClickYLocation.Location.Y));
3.Click the element---> elementToClick.click();

Ranorex test automation issue: Unable to reliably click a button on silverlight web app

We have automated a few test cases using the Ranorex automation framework for a Silverlight web application. These test cases involve clicking buttons in order to invoke certain messages on the screen. In order to grab the button on the screen, we first create an Ranorex button object and then point it to the appropriate element using Ranorexpath. Then, we use the RanorexButton.Click() event to click the button. However, this event is unreliable. It works sometimes and at other times the button is not clicked. When the button is not clicked, we have to run the test case again from the start. What are we doing wrong? If this is a known problem of ranorex, please suggest workarounds.
I was facing the same problem but I am able to resolve the problem by introducing a Validate.Exists(infoObject) just before the click. Please make sure that you pass infoObject of your button or any element in Validate.Exists API.
Example:
Validate.Exists(repo.MyApp.LoginBtnInfo);
var button = repo.MyApp.LoginBtn;
button.Click();
With regards,
Avinash Nigam
I haven't heard about such a problem with Ranorex yet, maybe this is just a timing issue.
You could add a Validate.Exists(yourButton) right before the click, this ensures that the click is performed after the button was successfully loaded.
If it is a WebElement you could also use the PerformClick() method instead of the normal Click() method.
There are also different methods which will ensure that the button is in the visible area and has focus, like the EnsureVisible() or the Focus() method.
You will find the available methods of the used adapter in the online API of Ranorex.
If the Button is not within the area you can see without scrolling, you can use a
var button = repo.Buttons.button1;
button.EnsureVisible();
button.Click();
In this way the button is forced to be watched.
It might as well be an issue with the xpath and element Id-s.
If you have changing element Id-s even when the page is navigated away from and moved back (for example we have this issue with SAP related components) you might need to make a more robust xPath path variable using regular expressions.
Try to find object and parts of the path that do not change (eg. "iFrame id="MainContent"" or "btn id="ID_XXXX_Search_Button"") - ofcourse this will only help if the issue is within this.
Ranorex Regular Expression info can be found here: http://www.ranorex.com/support/user-guide-20/ranorexpath.html#c3294
A quick example of what I'm talking about:
Let's say we have an input field that has a changing ID in it's name:
US_EL.ID_F2B49DB60EE1B68131BD662A21B3432B:V_MAIN._046A-r
And I know that the part in the Id that doesn't change is:
:V_MAIN._046A-r
I can create a path for this searching the element by the ending of the elements' id that doesn't change using regular expression:
/dom[#domain='test.example.com']//iframe[#'identifier']//iframe[#'identifier2']//input[#id**~'^**:V_MAIN._046A-r']
The bold part will specify to search for an input element with an Id that ends with ":V_MAIN._046A-r".
An issue that might arrise from this is if you have elements using partially the same names you might get multiple elements returned for the same path. So it's wise to add a few more certain points to the path (eg. "iframe[#'identifier2']") when this issue arrises.

Categories

Resources