Access submenu in Selenium using c# - selenium

Hi I am trying to access a submenu in selenium using c#. On my the website that I am testing the mouseover to the menu opens another submenu1,mouseover to submenu1 options open submenu2. I want to click on one of the submenu2 options. I tried to run below, everytime it throws an error of element not visible on custonboarding.Click();
Actions builder = new Actions(driver);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
var hoverover = driver.FindElement(By.XPath("menu1"));
builder.MoveToElement(hoverover).Build().Perform();
var hoverover1 = driver.FindElement(By.XPath("submenu1));
builder.MoveToElement(hoverover1).Build().Perform();
var custonboarding = driver.FindElement(By.XPath("submenu2"));
custonboarding.Click();
Can someone help me out here?

It might take some time for the element to fully load. You can use explicit wait and ExpectedConditions to wait for the element to be visible
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
IWebElement custonboarding = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("submenu2")));
custonboarding.Click();
This will wait up to 5 seconds for the element to be visible.

Related

Selenium Web Driver

We are working on IE Automation using Selenium Web driver in C#.Net.
We are getting an exception in handling model popup window. We supposed to do below the action.
When we click on Link button it will open a popup window then we need switch to popup window selecting check box options and click on Submit button.
When clicking on Link button we are able to open the popup window. But here we are facing an issue like the child popup window is not loading with data and getting HTTP 500 Internal server Error.
I don't understand sometimes it was working properly with the same code but not all the times I am getting above issue when I am trying to perform above actions on child window.
is this any IE settings issue or my code issue even i ignored protected mode settings in IE settings.
I am trying with below code :
js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[#id='ByNewNotes']")));
(or)
string jsWindowString = "NewWindow('pop_Type.jsp?Type=External&IuserId=NUVJK50'," + sessionId + ",'400','500');return false";
((IJavaScriptExecutor)driver).ExecuteScript(jsWindowString);
Could you please help on this issue.
Thanks in Advance.
Instead of using ExpectedConditions.ElementEx‌​ists use ExpectedConditions.elementToBeClickable or presenceOfElementLocated
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""//*[‌​#id='ByNewNotes']")));
element.click();
Either Try to use FluentWait. Create a by function of your element which you want to wait and pass it in below method
WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}});
}
Hope it will help you :)
Have you tried
Thread.Sleep(2000);
We had the same issues and solved it with this simple way.

Selenium click sometimes not working using Edge driver

Please see pic below. I have tried to select the input tag through the following method using Selenium:
1. `element.Click();`
2. `element.SendKeys(Keys.Enter);`
3. Actions action = new Actions(Browser.Driver).MoveToElement(element);
action.Click().Perform();
I am trying to click the checkbox by returning it through a property in code that has the following code:
WebDriverWait wait = new WebDriverWait(Browser.Driver, TimeSpan.FromSeconds(60));
IWebElement checkbox = wait.Until(ExpectedConditions.ElementExists((By.CssSelector("label[for='PerformerIndependence_AcceptTermsAndConditions']"))));
return checkbox;
or
WebDriverWait wait = new WebDriverWait(Browser.Driver, TimeSpan.FromSeconds(60));
IWebElement checkbox = wait.Until(ExpectedConditions.ElementExists((By.CssSelector("input#PerformerIndependence_AcceptTermsAndConditions"))));
return checkbox;
For some reason If I run the code in debug mode to test the element returned to click it will click. If I just run my test in run mode using Visual Studio2015, the checkbox is not clicked. The method using the element to click for the three attempts is wrapped in a try/catch block. Please help!!!
Screenshot
I am also facing this issue. Click on checkbox is not working in Edge..
There are two work around of this:
click using JS
use action class..
WebElement element=driver.findElement(By.id("abc"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
==========================================================
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
Both worked fine in my case.

Selenium click not work

Help me please.
Selenium does not click on element and element is clickable(selenium does not generate exception).
I try use Id, css, xpath locators, nothing did not help me.
What should i do to decide my problem?
Java code example.
WebElement sector = webDriver.findElement(By.id("sector-1"));
sector.click();
After click system must open this page
Seems that you try to interact with object inside a <svg> element. If so, you cannot manage it's child elements simply using click() method.
Try this instead:
WebElement svgObject = driver.findElement(By.xpath("//polygon[#id='sector-1:canvas']"));
Actions builder = new Actions(driver);
builder.click(svgObject).build().perform();
Use below XPath :
WebElement sector = webDriver.findElement(By.xpath("//g[#id='sector-1']/polygon"));
sector.click();
OR
WebElement sector = webDriver.findElement(By.xpath("//polygon[#id='sector-1:canvas']"));
sector.click();
I decide my problem.
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"sector-2:canvas\"]")));
WebElement svgObject = webDriver.findElement(By.xpath("//*[#id=\"sector-2:canvas\"]"));
Actions builder = new Actions(webDriver);
builder.click(svgObject).build().perform();

NoSuchElementException on performing Mousehover action in selenium

I have a webpage in which I am trying to perform a mouse hover.. There is an element named "Entity Records" on which if you hover your mouse it displays a menu. In that menu I need to click on an element named Create New record.
I have tried 3 different set of codes but it's not working for me..
1st One:
WebElement el = driver.findElement(By.xpath("\\...."));
action.moveToElement(el).build().perform();
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.cssSelector("..."))).click().build().perform();
2nd one:
WebElement entityrecordsmenu = driver.findElement(By.cssSelector("..."));
Actions builder = new Actions(driver);
builder.moveToElement(entityrecordsmenu).build().perform();
WebElement createnewrecord = driver.findElement(By.cssSelector("..."));
createnewrecord.click();
3rd one:
Actions action2 = new Actions(driver);
WebElement ele = driver.findElement(By.xpath("...));
action2.moveToElement(ele).build().perform();
Actions build2 = new Actions(driver);
build2.moveToElement(driver.findElement(By.xpath("..."))).click().build().perform();
1st and 2nd code are doing the mouse hover fine, but its not clicking on the menu element. Can anyone suggest me the correct way of doing this?
After mouse hover, try by giving some wait to get that element displayed and then click.
WebElement entityrecordsmenu = driver.findElement(By.cssSelector("..."));
Actions builder = new Actions(driver);
builder.moveToElement(entityrecordsmenu).build().perform();
//provide wait here to display dropdown
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//some time implicity wait may fails then use thread.sleep
//Thread.sleep(3000);
WebElement createnewrecord = driver.findElement(By.cssSelector("..."));
createnewrecord.click();
//if still above once does not work you can try
Actions builder1 = new Actions(driver);
builder1.moveToElement(entityrecordsmenu).click(driver.findElement(By.cssSelector("..."))).build().perform();
Thank You,
Murali

Actions and htmlunitdriver - speed issue

My web application has menus that open on MouseOver. I'm writing tests using htmlunitdriver.
Test code to trigger menu is
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
Thread.sleep(2000);
builder.moveToElement(menu).build().perform();
Thread.sleep(2000);
driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]")).click();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
When I run a single test, it passes just fine. But when I try to run all my 80 tests at once, I get
unable to locate node using //a[starts-with(#href,'/submenuitem'
I guess the submenu is not yet open, htmlunitdriver has too much speed. Somethimes a "You may only interact with elements that are visible is occured on single runs too. Can someone help me fix this issue? Using FirefoxDriver or so is not an option for me.
Using a manual Thread.sleep(time) to wait for selenium actions is a dirty solution and should not be used at all.
Instead you could run a check is the element is visible before interacting with it.
public void waitUntilVisible(WebDriver driver, WebElement element){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.visibilityOf(element));
}
public void waitUntilClickable(WebDriver driver, By locator){
WebDriverWait waiting = new WebDriverWait(driver, 10);
waiting.until(ExpectedConditions.elementToBeClickable(locator));
}
Actions builder = new Actions(driver);
WebElement menu = driver.findElement(By.xpath("//a[starts-with(#href,'/index.html')]"));
waitUntilVisible(driver, menu);
builder.moveToElement(menu).build().perform();
WebElement menuItem = driver.findElement(By.xpath("//a[starts-with(#href,'/submenuitem')]"));
waitUntilClickable(driver, By.xpath("//a[starts-with(#href,'/submenuitem')]"));
menuItem.click();
You are using the implicit wait after finding the submenu item. I think there is no use for implicit wait there. The most advisable place to use the implicit wait is to declare after initializing the Driver instance.
One More solution you can use Explicit Wait to wait for an element in the Page.
Refer this post for more info about the Selenium waits.