Button element is not getting clicked - c#

Button's Html
<button id="exist_user" onclick="ExtUpdtPnl();" type="button" style="background-color: rgb(187, 187, 187);">
<i class="fa fa-gear fa-fw"></i>
Update Existing User
</button>
I have used multiple ways as below
driver.FindElement(By.Id("exist_user")).Click();
driver.FindElement(By.XPath("/html/body/form/div[4]/div/div/div[2]/button[1]"));
Please help..

Try driver.FindElement(By.Id("exist_user")).InvokeMember("click"); instead.

Try with JavascriptExecutor:
WebElement element = driver.findElement(By.xpath(".//button[#id='exist_user']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Or
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('exist_user').click();");

Related

Element Click Intercepted Exception

I am unable to perform click operation on Sign-in button in my application.
Below is the HTML code.and u can find the exact button element in last line.
<div class="form-group dl-padding-10">
<select class="form-control form-control-solid" name="SelectedRoleID" id="SelectedRoleID" onchange="removeBorderColor()" required="">
<option id="default_val" selected="" disabled="" hidden="">Profile </option>
<option value="15">Service Consultant</option>
<option value="11">DLBO Developer</option>
<option value="16">Admin Agent</option>
<option value="17">Team Leader</option>
<option value="18">Manager</option>
<option value="19">CV Mandator</option>
<option value="20">CV Agent</option>
<option value="21">Forensics Agent</option>
</select>
<div class="dl-align-left" id="show_text" style="color:red">
</div>
</div>
<div class="circle1-mask form-group" id="FingerprintContent" style="height:140px;z-index:2; background-image: none;">
<img src="Assets/img/fingerprint4.gif" id="fingerprint-img" data-status="active" style="height:140px; width:100px;" onclick="DeviceScript.scanFingerPrint(event)">
</div>
<div class="form-group dl-padding-10">
<button type="submit" id="register-btn" class="btn btn-block dl-button-primary dl-no-margin">Sign In (For Testing Purpose Only)</button>
</div>
</div>
</div>
</form> </div>
</div>
Kindly help me with the suitable xpath to perform click operation on the sign-in button.
Also find the image of code tried.Code
Xpath=”//button[contains(text(), 'Sign In (For Testing Purpose Only)')]”
(Or)
IWebElement Signin = driver.FindElement(By.Id("register-btn"));
Signin.Click();
(Or)
IWebElement Signinbutton = driver.FindElement(By.XPath("//button[contains(text(), 'Sign In (For Testing Purpose Only)')]"));
Actions action = new Actions(driver);
action.MoveToElement(Signinbutton).Click().Perform();
The error which i get:
OpenQA.Selenium.ElementClickInterceptedException : element click intercepted: Element <button type="submit" id="register-btn" class="btn btn-block dl-button-primary dl-no-margin">...</button> is not clickable at point (758, 646). Other element would receive the click: <div class="footer navbar-fixed-bottom">...</div>
Try with javascript:
IWebElement Signinbutton = driver.FindElement(By.XPath("//button[contains(text(), 'Sign In (For Testing Purpose Only)')]"));
IJavaScriptExecutor javascriptExecutor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click();", Signinbutton );
Looks like you are missing some html since the error references a footer.
Not a fan of thread sleeps but try one and see if the thread sleep allows the page to load. I am wondering if your page is still loading and trying to click. If the sleep works, I would remove that and do a move to element or try a java click.
Thread.Sleep(1000);
driver.FindElement(By.XPath("//button[contains(text(), 'Sign In')]")).Click();
driver.FindElement(By.Id("register-btn")).Click();
The exception you are getting is because the button you are trying to click is behind this element
<div class="footer navbar-fixed-bottom">...</div>
Which seems to be the footer of your page.
You can try any of the following steps to solve the issue
Scroll to any element below the sign-in button(If any). You can use the below code for that:
protected boolean scrollToElement(WebElement element)
throws NoSuchElementException, StaleElementReferenceException {
try {
jsExecutor.executeScript("arguments[0].scrollIntoView(true);", element);
return true;
} catch (NoSuchElementException e) {
logError("Element Not found exception when scrolling to element (JavaScript)", e);
throw e;
} catch (StaleElementReferenceException e) {
logError("Stale element exeption when scrolling to element (JavaScript)", e);
throw e;
}
}
Close the footer if it is for accept cookies or something similar, or you can also apply Custom CSS to that element to hide it and then try to click the element.
String css= "display:none !important;"
protected void addCustomCSS(WebElement webElement, String css) {
registerCall(new Object() {
}.getClass().getEnclosingMethod().getName());
try {
String style = (String) jsExecutor.executeScript("arguments[0].getAttribute('style')", webElement);
jsExecutor.executeScript("arguments[0].setAttribute('style', " + css + ")", webElement);
stylesQueue.add(style);
} catch (Exception e) {
e.printStackTrace();
}
}

How to click on an element with aria-hidden="true" using Selenium

I have HTML as below
<div class="summary-row">
<div class="text-center" id="summary-back">
<a href="/Health/Dependents">
<svg class="svg-inline--fa fa-chevron-left fa-w-8 font-32" data-auto="back-btn" aria-hidden="true" data-prefix="fal" data-icon="chevron-left" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 512" data-fa-i2svg=""><path fill="currentColor" d="M238.475 475.535l7.071-7.07c4.686-4.686 4.686-12.284 0-16.971L50.053 256 245.546 60.506c4.686-4.686 4.686-12.284 0-16.971l-7.071-7.07c-4.686-4.686-12.284-4.686-16.97 0L10.454 247.515c-4.686 4.686-4.686 12.284 0 16.971l211.051 211.05c4.686 4.686 12.284 4.686 16.97-.001z"></path></svg><!-- <i class="fal fa-chevron-left font-32" data-auto="back-btn"></i> -->
</a>
</div>
And RemoteWebDriver unable to find the element and I assume its due to the hidden attribute, How can click on this element ?
You can click on the element using JavaScriptExecutor like:
WebElement element = driver.findElement(By.id("summary-back"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
To click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
CssSelector:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.summary-row > div#summary-back > a[href=/Health/Dependents]"))).Click();
XPath:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='summary-row']/div[#id='summary-back']/a[#href='/Health/Dependents']"))).Click();
I just changed attribute "aria-hidden" from "true" to "false" and after that I clicked.
Perhaps that code can help you.
public void ClickElementHidden()
{
((IJavaScriptExecutor)driver).ExecuteScript("document.getElementsByClassName('svg-inline--fa fa-chevron-left fa-w-8 font-32')[0].setAttribute('aria-hidden', 'false')");
driver.FindElement(By.CssSelector("#summary-back > a > svg")).Click();
}

Selenium: How to find the element from the HTML provided through CssSelector or XPath

Here is a button on the page:
<button data-purpose="add-section-btn" type="button"
class="ellipsis btn btn-default btn-block">
<span class="a3 udi udi-plus-square"></span>
<!-- react-text: 255 --> <!-- /react-text -->
<!-- react-text: 256 -->Add Section<!-- /react-text -->
</button>
When I try to find it using the following code:
var btns = _driver.FindElements(By.TagName("button"));
var sectionTitle = btns.Where(x => x.GetAttribute("data-purpose") == "add-section-btn");
It returns null.
If I try the following XPath:
var btn = _driver.FindElement(By.XPath("//button[data-purpose=\"add-section-btn\"]"));
then I get an exception.
How to find such a button?
Try with,
var btn = _driver.FindElement(By.XPath("//button[#data-purpose='add-section-btn']"));
As per the HTML you have shared to find the element with text as Add Section as the element is a React Element you have to induce WebDriverwait for the element to be visible and you can use either of the following Locator Strategies:
CssSelector:
var btn = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.CssSelector("button.ellipsis.btn.btn-default.btn-block[data-purpose='add-section-btn']")));
XPath:
var btn = new WebDriverWait(_driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//button[#class='ellipsis btn btn-default btn-block' and #data-purpose='add-section-btn'][normalize-space()='Add Section']")));

Selenium clicking button hidden by span

I am trying to automate an environment selection screen where there are multiple selectable buttons individually hidden by a span, these display as tiles.
I have managed to navigate to a given tile and pull up the button but I am unable to click it.
Here is the code I have
public static void NavigateToEnvironment(IWebDriver driver, string environment)
{
IWait<IWebDriver> wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5.00));
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath($"//span[text()='{environment}']")));
var tile = driver.FindElement(By.XPath($"//span[text()='{environment}']"));
Actions action = new Actions(driver);
action.MoveToElement(tile).Perform();
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath($"//*[#span=(text()='{environment}')][#btn=(starts-with(text(), 'Start'))]")));
driver.FindElement(By.XPath($"//*[starts-with(text(), 'Start')]")).Click();
}
The first part successfully moves to the correct tile and opens the span so on screen the button is there.
The wait.until condition is fine too so Selenium can see the element so its the final click command I have an issue with.
It seems only to look for the button hidden within tile one but I am trying tile three. All the buttons have the same HTML tags.
In the current code state I get element not visible.
I have tried to use the xpath as in the wait condition but that returns that the parameters are not elements so again fails.
I am kind of at a loss. Any ideas?
UPDATE:
Some HTML of one of the buttons. This basically repeats with a different application name
<li class="trans tile">
<div class="tileWrap noselect" aria-haspopup="true">
<div class="divNavIcon">
<span class="spnNavIcon primarycolorfont enable" data-bind="css: Code"></span>
</div>
<div class="tilePopup primarycolor">
<span data-bind="text: ApplicationNameAlias ? ApplicationNameAlias : ApplicationName">Enable QA</span>
<span data-bind="text: Description" class="tileSubText">Enable CI Environment</span>
<div class="tilePopupToggle">
<button type="button" data-bind="click: $parent.startApp, css: { disabled: IsRevoked }" class="btn">Start <i class="fa fa-fw fa-desktop"></i></button>
<button type="button" style="display:none;" data-bind="click: $parent.startAppNew, css: { disabled: IsRevoked }" class="btn">Start New <i class="fa fa-fw fa-external-link"></i></button>
<button type="button" style="display:none;" data-bind="attr: { "data-target": "#appPreview_" + ApplicationID }" class="btn" data-toggle="modal" data-target="#appPreview_3043">Preview <i class="fa fa-fw fa-play"></i></button>
</div>
</div>
</div>
Screenshot to help understanding - Each tile acts in the same way with a hidden start button. My code works fine for this first tile but if I want the second or third tiles it cannot find the start button
As per the HTML you have shared to click on the button with text as Start you can use the following code block :
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='tilePopup primarycolor']//div[#class='tilePopupToggle']/button[#class='btn' and normalize-space()='Start']/i[#class='fa fa-fw fa-desktop']"))).Click();
Update
Can you try removing the <button> tag as :
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[#class='tilePopup primarycolor']//div[#class='tilePopupToggle']//i[#class='fa fa-fw fa-desktop']"))).Click();
Note : As per aurelia/binding/issues/163 disable.bind disables button but inner content is still clickable and we are targeting i[#class='fa fa-fw fa-desktop']
I have managed a pretty elegant work around to this issue. The buttons are contained in li items so i'm just finding the relevant one of those.
public void NavigateToEnvironment(IWebDriver driver, string environment)
{
var tile = driver.FindElement(By.XPath($"//span[text()='{environment}']"),5);
Actions action = new Actions(driver);
action.MoveToElement(tile).Perform();
var tile2 = driver
.FindElement(By.XPath("//*[#id='content']/div/div/div/div/ul"))
.FindElements(By.TagName("li"))
.Where(x => !string.IsNullOrWhiteSpace(x.Text))
.ToList();
var singleTile = tile2.Single(x => x.Text.Contains(environment));
driver.FindElement(By.XPath($"//*[#id='content']/div/div/div/div/ul/li[{tile2.IndexOf(singleTile) + 1}]/div[1]/div[2]/div/button[1]")).Click();
}

Why can I not click on this element in this code?

Actions action = new Actions(driver);
IWebElement we = driver.FindElement(By.XPath(".//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label']"));
action.MoveToElement(driver.FindElement(By.XPath(".//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered']"))).Click().Build().Perform();
expect element as followings:
< span tabindex = "-1" class="ms-crm-CommandBar-Button ms-crm-Menu-Label" style="max-width: 200px;">
<a tabindex = "0" class="ms-crm-Menu-Label" onclick="return false">
<img tabindex = "-1" class="ms-crm-ImageStrip-New_16 ms-crm-commandbar-image16by16" style="vertical-align: top;" src="/_imgs/imagestrips/transparent_spacer.gif">
<span tabindex = "-1" class="ms-crm-CommandBar-Menu" [enter image description here][1]style="max-width: 150px;" command="lead|NoRelationship|HomePageGrid|Mscrm.NewRecordFromGrid">
New
</span>
<div class="ms-crm-div-NotVisible">
Create a new Lead record.
</div>
</a>
</span>
Note that this class "ms-crm-CommandBar-Button ms-crm-Menu-Label" turns to be "ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered" when mouseover.
Many thanks.
When you search for the hovered element it doesn't exist.
So you need to hover element first, then you'll be able to find and click hovered element.
action.MoveToElement(driver.FindElement(By.XPath("//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label']"))).Build().Perform().MoveToElement(driver.FindElement(By.XPath("//*[#class='ms-crm-CommandBar-Button ms-crm-Menu-Label-Hovered']"))).Click().Build().Perform();
In hover case first you need to moveTo That element after that you can click on it.
Actions actions = new Actions(driver);
action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath("ur element"))).click().build().perform();

Categories