Selenium click event does not trigger angularjs ng-click - angularjs

I have this page where there is a textbox and there is save button associated with each text box. I need to click on the save button so that it will save the value in text box. It is working manually and using selenium. But when running through Selenium WebDriver it's not saving the text box value. But there is no error exception being thrown. Input, Click is working.
savetextvalue() is not triggered in short.
There is similar issue Selenium click event does not trigger angularjs event
<pp-save-control fn-save-text="saveText();" btn-class="btn btn-default btn-mtl" button-id="btnkbaemailauthsub" place-holder-text="" input-class="tb-mtl" input-id="txtkbaemailauthsub" config-name="40" title-text="KBA email authentication subject" outer-container-class="div-mtl-header" class="ng-isolate-scope"><div class="div-mtl-header">
<span class="label-mtl ng-binding">KBA email authentication subject</span><img ng-hide="(isHelpHidden != null && isHelpHidden != 'true') ? false : true" class="help-mtl ng-hide" src="/Images/help.png">
<div class="div-mtl-tb-holder">
<input type="text" placeholder="" class="tb-mtl" name="txtkbaemailauthsub" id="txtkbaemailauthsub">
<button ng-click="saveTextValue();" ng-hide="false" class="btn btn-default btn-mtl btn-mtl-alignment" name="btnkbaemailauthsub" id="btnkbaemailauthsub" type="button">save</button>
</div>
</div>
</pp-save-control>
There are multiple text box and associated save button. Depending on the 'config-value'(You can see at top) value is getting saved.

Replace the locator according to your convenience
WebElement element= driver.findElement(By.id("btnkbaemailauthsub"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
OR
JavascriptLibrary jsLib = new JavascriptLibrary();
jsLib.callEmbeddedSelenium(driver,"triggerMouseEventAt", element,"click", "0,0");
OR
WebElement element= driver.findElement(By.id("btnkbaemailauthsub"));
// Configure the Action
Actions action = new Actions(driver);
//Focus to element
action.moveToElement(element).perform();
// To click on the element
action.moveToElement(element).click().perform();
Hope it will help you :)
Get back to me if still facing issue :)

driver = webdriver.Chrome('/path to /webdriver 22');
driver.find_element_by_css_selector('button[ng-click="func()"]');

Try putting in wait's in between your actions because Selenium doesn't know how angular loads and works. Protractor was created right for the purpose of handling angular web pages, which is a wrapper over selenium webdriver. However if you still want to test angularjs with Selenium then waiting for few seconds implicitly or fluent wait's between each action should help you with your need and accomplish what you intend to. Hope it helps.

In Selenium IDE try:
<td>sendKeysAndWait</td>
<td>id=mybutton</td>
<td>${KEY_ENTER}</td>
same with Webdriver:
WebElement element_p = (new WebDriverWait(_driver, 3))
.until(ExpectedConditions.visibilityOfElementLocated(By
.id("myButton")));
element_p.sendKeys(Keys.RETURN);

The same issue happens when using Selenide over Selenium. I found a workaround using Selenide, if that is an option for you:
After finding the clickable element, use Selenide's pressEnter() instead of click().
element.should(exist).pressEnter();

I faced this issue myself before. Here is how I solved it.
Right click on the button -> Go to inspect element -> Copy css selector and store it in a variable
The code to click the button :
your_element = your_driver.find_element_by_css_selector(css_selector_variable)
your_driver.execute_script('arguments[0].click()',your_element)

Related

Python-selenium action chains not working in Safari

I am trying to click on a menu and then a submenu in selenium-python on a Safari Webdriver. No matter what I do, I cannot seem to make the ActionChains do anything whatsoever. Am I doing something wrong or is this an issue with Safari?
I have tried a number of different actions using the ActionsChain, but none of them seem to be working
```driver = webdriver.Safari()
wait = WebDriverWait(driver, 20)
url = "someurl.com"
link_text = "link text"
driver.get(url)
driver.maximize_window()
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, link_text)))
ActionChains(driver).move_to_element(driver.find_element(By.LINK_TEXT, link_text)).click().perform()
print('Hello World')```
I expect to see the browser clicking on the element, but I see only the terminal of my program printing 'Hello World'.
You could try clicking with JavaScript instead of ActionChains here:
element_to_click = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, link_text)))
driver.execute_script(“arguments[0].click();”, element_to_click)

How to close file upload dialogue after click on upload button in protractor

I want to upload an image file in protractor. the problem is i can not get "input[type = "file"]" element without clicking on upload button.
when I click upload button, file upload dialogue popup opens.
i have tried
browser.actions().sendKeys(protractor.Key.ESCAPE).perform(); but it does not work.
This is what I am doing:
var image = '../images/image1.png';
var absPathImg = path.resolve(__dirname, image);
element(by.id('uploadImage')).click();
browser.actions().sendKeys(protractor.Key.ESCAPE).perform();
element(by.css('input[type=file]')).sendKeys(absPathImg);
element(by.id('upload')).click();
How can i close that file upload dialogue to upload an image?
Try it like this. I used this for Protractor, but I think it's similar for Selenium.
I also have a problem for Windows File dialog not closing.
This is how I solved that:
element(by.css('elementForOpenDialog')).sendKeys(absPathImg);
Send Keys to the element which will open file dialog, not click on element to open file dialog. Just send to that element absolute path and it will upload without opening dialog.
It's worked for me. Good Luck!
This is an old thread but I'll answer in case anyone else runs to this problem.
I had to solve this same problem today. I was able to work around opening the upload dialog, because I am quite sure it cannot be closed without some external libraries and that would not work in headless mode.
You need to inspect the javascript linked to the upload button's click event and execute some script before sending keys to the file input.
This is individual to each case, but in my case i had to do this (Python):
script = (
"var url = '/Contacting/UploadCsv'; "
"$('#buttonFileUpload').data('url', url);"
)
driver.execute_script(script)
driver.find_element_by_id('buttonFileUpload').send_keys(csv_path)
I picked that script from the JS function linked to the button.
I landed here because I was searching on HOW TO CLOSE AN IMAGE WHEN CLICKED
In case that helps someone.
So what I did.
var Img = Driver.FindElement(XPath.News.YourImg);
Img.Click();
try
{
Img.Click();
Assert.IsFalse(feedImg.Displayed);
}catch(Exception ex)
{
}
This way you can check if the image is closed by trying to click on it again.
Instead of clicking on to ESCAPE, why not simulate the ENTER key press to simulate a person entering the path and pressing the ENTER key to upload the image.
I am doing this in my code and this works perfectly fine.
var path = require('path')
this.fileUploadOne = element(by.css('locator_to_element'));
var fileUpload = 'path_of_file_to_upload'
absolutePath = path.resolve(__dirname,fileUpload);
console.log(absolutePath);
this.fileUploadOne.sendKeys(absolutePath);
browser.actions().sendKeys(protractor.Key.ENTER).perform();
//just for debugging purposes.
browser.sleep(20000);

Handling angularjs dialog box in webpage using selenium

I have an angularjs webpage that prompts when I perform certain action on the page. The code should ignore and continue if there is no prompt, but if a prompt appears then ensure that the javascript prompt is accepted.
When I use firebug, I get the following property of the element.
/html/body/div[2]
The div[number] changes as and when I go to this page.I am not able to use the same in XPath as everytime the test case is executed, the div element would change. Also the div has an attribute of tabindex = -1.
bool status = false;
WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
IWebElement alert;
if (wait.Until(ExpectedConditions.InvisibilityOfElementLocated((By.XPath("/html/body/div[2]")
{
Console.WriteLine("I am in if loop");
status = false;
}
else
{
Console.WriteLine("I am in else loop");
alert = Browser.Driver.FindElement(By.XPath("/html/body/div[2]"));
((IJavaScriptExecutor)webDriver).ExecuteScript("arguments[0].click()", alert);
status = true;
}
return status;
How do I handle such a dialog box and ensure that the same is accepted when it appears.
Thanks.
You can derive the xpath using the title of the alert. May be like below. (I do not know the exact html, just imagining the header text is in some span tag)
alert = Browser.Driver.FindElement(By.XPath("/html/body//div/span[contains(text(),'Apply changes to controller configuration')]"));
(//) can avoid using index to your div tag.

What's the best way in Selenium WebDriver to wait for buttons to be enabled?

I am creating an automated testing program with webdriverjs of an application where the 'Next' buttons are often disabled for a time, then enabled. The automation waits for the button to become enabled, then clicks the button.
The class of a button in particular toggles from : 'btn btn-next disabled' to 'btn btn-next'
I have come up with the following solution but wonder if there was a better way?
browser.wait(until.elementLocated(By.css('nav a[class="btn btn-next"]')),
timeout, 'button still disabled after timeout').then(function() {
browser.findElement(By.css('nav a[class="btn btn-next"]')).click();
});
Which seems to do the trick.
Originally my idea was to run function such as:
tryToClick = function(element){
var classValue = element.getAttribute("class");
isDisabled = classValue.search('disabled') !== -1 ? true : false;
if(isDisabled){
setTimeout(function () {
tryToClick(element);
}, 500);
} else {
element.click();
}
}
However, methods such as search, split, contains, etc resulted in errors. Does anyone know why?
Function name is elementToBeClickable()
Example:
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.css('nav a[class="btn btn-next"]')));
In Webdriver JS you can use until.elementIsDisabled or until.elementIsEnabled
var theButton = driver.findElement(By.css('nav a[class="btn"));
driver.wait(until.elementIsDisabled(theButton)), 10000, 'element is still enabled, sorry.').then(function(){
theButton.click();
});
But it looks like you are selecting an entire class of buttons, if so, make sure your iterate over them and click the one you want, or use css nth-child();
I typically work in Java and C#, but there should be an equivalent option in JS. You will want to do an explicit wait using:
Until.ElementIsEnabled(<locator>)
If there isn't an equivalent to the above wait..until, you could resolve the issue by using element.isEnabled() == false in place of the isDisabled variable. In your code.

Protractor use sendKeys() to upload a file in a <div>

As you can see in the title, I am trying to upload a file using protractor.
In my project, I have a webpage where I can drag & drop a file, using resumable.js assigned to a div representing the drop area.
The fact is, when doing e2e testing, I obviously can't drag & drop a file, so I chose to upload it using sendKeys() with the absolute path of my file path.resolve('./tests', 'test.csv').
Until here, everything works (path is good and I can find my element with element(by.id(...)) but when I use sendKeys() it throws an error saying "unknown error: cannot focus element".
I tried to upload on an <input type="file"></input>, it works, but since it's on what I use I can't upload this on my original div.
I think I have to specify that this is a file, I saw the setFileDetector(detector) function but I am not sure this is the right way to proceed, and it doesn't seem to work with it anyway.
Here is the HTML drag & drop :
<p translate>Drag & Drop your file below</p>
<div class="drop" ng-class="dropClass" id="import-drop-area" data-ng-init="init()" translate> </div>
And here is the test that works on an but not on my div :
it('Should begin the import wizard by dropping a file', function(done) {
// Begin the wizard
var nextButton = element(by.id('next'));
nextButton.click();
// Upload a file
var uploadArea = element(by.id('import-drop-area'));
var file = path.resolve('./tests/import', 'test.csv');
uploadArea.sendKeys(file);
setTimeout(done, 1000);
});
I hope someone could help me, thank you by advance !
Best regards.

Resources