selenium c# mouse over actions function - c#

I have to mouse over a row to display a link that I need to click. I am able to mouse over the row and the link I need to click on is displayed but I can't click on it. Below is my code:
IWebElement element = driver.FindElement(By.XPath("//span[contains(text(), 'abc#gmail.com')]"));
System.Threading.Thread.Sleep(5000);
Actions action = new Actions(driver);
action.MoveToElement(element).Perform();
Thread.Sleep(5000);
IWebElement delete = driver.FindElement(By.XPath("//span[contains(text(), 'Delete')]"));
delete.Click();

I gather that there is a menu that pops up when you hover over that first "element" IWebElement. When using Java, I had a similar problem where a simple click() action on the menu item in that popup would not work. I had to resort to using Javascript. You could insert this line right after your code to retrieve the delete web element:
((JavascriptExecutor)driver).executeScript("arguments[0].click();", delete);
I'm guessing that you could do something similar with C#

Related

Not able scroll down the page using selenium - nUnit, C#

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);

Selenium Webdriver: Selecting menu item in multi-level menu

Selenium WebDriver in Visual Studio With C#.
I've create a driver Object, which I'm using an instance of.
I'm trying to Access the top menu on this site: http://store.demoqa.com/
The second menu element, "Product Category", has a submenu. I'm trying to emulate a mouse-over of the "Product Category" element, then select and click the first element in the list (Accessories).
This is what I've got so far, pieced together by several searches here and elsewhere. It works up the point of clicking on the menu item ("Accessories"). I see that the top element is selected, and that the menu item is "selected" because it slightly indents when hovering the mouse over it. However, from there I cannot seem to Click() it.
"menu-item-33" is the top menu item, Product Category.
"menu-item-34" is the sub menu item Accessories.
Actions action = new Actions(FFDriver.Instance);
IWebElement we = FFDriver.Instance.FindElement(By.Id("menu-item-33"));
action.MoveToElement(we).MoveToElement(FFDriver.Instance.FindElement(By.Id("menu-item-34"))).Click().Build().Perform();
It moves to the correct item, but the Click() function doesn't seem to work, since the page isn't changed.
Pardon me if this is too little information, but I've tried to keep it narrowed down to the code that seems to be the struggle.
I do not see why the code you have wouldn't work. However, you can try implementing some explicit wait if necessary. I have tried the following and it works. Note: I always suggest you to use id for locating element. But, I thought I show you another option and directly finding the anchor will be wiser for submenu item
By byId = By.Id("menu-item-33");
By css = By.CssSelector("a[href*='product-category/accessories']");
Actions action = new Actions(_driver);
IWebElement we = _driver.FindElement(byId);
action.MoveToElement(we).Build().Perform();
new WebDriverWait(_driver,TimeSpan.FromSeconds(2)).Until(ExpectedConditions.ElementIsVisible(css)).Click();

Selenium - Force click on non-visible element

I'm using C# Selenium.WebDriver.2.44.0
On some 3rd party site I'm trying to press an element and get this:
var myWebElement = Driver.FindElement(By.XPath("//a[.=' some value']
myWebElement.Click();
I get the element and on click I get this:
{"element not visible\n (Session info: chrome=39.0.2171.95)\n (Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86_64)"}
The item is within some sub menu revealing after I successfully press the parent menu. Also put 5 seconds sleep to be safe that the accordion is well seen (and it is well seen in my eyes).
Question - on 3rd party site how can I force the click on this item?
You could try bringing it to the top by changing the z-index with driver.execute_script().
You could use the inbuilt functions and WebDriver waits
var myWebElement = Driver.FindElement(By.XPath("//a[.=' some value']
WebDriverWait wait = new WebDriverWait(Driver.FindElement, TimeSpan.FromSeconds(10));
var element = wait.Until(ExpectedConditions.ElementIsVisible(myWebElement));
Actions action = new Actions(Driver.FindElement);
action.MoveToElement(element).Perform();
myWebElement.Click();
The problem is that Selenium executes actions one after another, but something like "show a sub menu" is lost between actions.
You're likely going to have to use an Actions chain for this:
Actions action = new Actions(Driver);
action.MoveToElement(Driver.FindElement(By(ParentElementSelector)))
.click()
.MoveToElement(Driver.FindElement(By.XPath("//a[.=' some value'])))
.click()
.Build()
.Perform();
This will move to the parent element, click, then move to the element you wish to find, then click. It will perform all of these as one action, which should be able to click the sub menu element.
When an element is not visible u can try this...It works for me
IWebElement WEHiddenID = driver.FindElement(By.Id(""));
WEHiddenID.SendKeys(OpenQA.Selenium.Keys.Enter);

How to do mouse hover over HTML and click an option from the drop down after hover in selenium web driver

Any one can please tell me how to mouse hover on the menu drop down and click any option from the drop down in SElenium using C#
You could use Actions. First define the complete action and then perform it.
Actions actions = new Actions(driver);
WebElement menuHover = driver.findElement(By.id("ID_Menu"));
actions.moveToElement(menuHover);
WebElement subLink = driver.findElement(By.id("ID_Item"));
actions.moveToElement(subLink);
actions.click();
actions.perform();
Hope help!

How to change the visibility of the button got by Xpath in Selenium

Hello I am working in Selenium Automation testing with Nunit. I have one grid which have bulk of users and there is one remove button on each row of the grid. But that Remove button is visible only on mouse hover. So when I run the script, It gives me the error -
Element is not currently visible and so may not be interacted with
The Xpath of button is
"//div[1]/div[2]/div/section/div[2]/div[contains(.,'IE8 john
smith')]/div/div[2]/button[1]"
I tried working with the actions in selenium but still it gives me the same error.
Actions actions = new Actions(Driver);
var element = Driver.FindElement(By.XPath("//div[1]/div[2]/div/section/div[2]/div[contains(.,'" + fullName + "')]/div/div[2]/button[1]"));
actions.MoveToElement(element);
actions.Click();
actions.Perform();
Can anyone help me out ?
I think the following procedure will help
using Actions, hover over the element step
insert an implicit wait till the element is visible
click the button
Try to force element to be visible using JS:
IWebElement element = driver.FindElement();
js.ExecuteScript("arguments[0].style.visibility = 'visible', arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", element);
element.Click();

Categories