unable to locate web element selenium - node.js

Hello I am a newbie with Selenium, I am trying to automate a scenario using webdriver framework: find a web element by its id. At the last step of the scenario, I am not able to locate a radio button and my code throws this error Timeout Error
This is my selenium code:
setTimeout(function(){
driver.wait(until.elementLocated(By.id('radRadioSub')), 24000, 'radRadioSub not located');
if (pattern.comment.length > 0) {
driver.findElement(By.name('remarksArea')).sendKeys(pattern.comment);
console.log("Trying to update remarks area")
}
// Select Discard radio >> radRadioDis
console.log("Trying to click on disegard/aprove")
driver.findElement(By.id(actionToDo)).click().then(function()
{
console.log('We clicked on the element' + actionToDo);
});
// Send : >> btnSolveTaskToolbar
driver.findElement(By.id('btnSolveTaskToolbar')).click().then(function()
{
console.log('We clicked on the element btnSolveTaskToolbar');
});
},6000);
Can someone help me? Thanks in advance!

driver.wait(until.elementLocated(By.id('radRadioSub')), 24000,
'radRadioSub not located');
^ That is the line that is failing, and that is caused by an element with id radRadioSub not being found. Look in your page source and find the correct ID.

Related

Cypress: How to know if element is visible or not in using If condition?

I want to know if an element is visible or not. I am not sure how to do that.
I know that we can run this:
cy.get('selector').should('be.visible')
But if element is invisible then test is failed. So I just want a boolean value if element is not visible so I can decide through if condition.
Use case:
I want to open a side menu by clicking on the button only if sidebar is invisible.
if(sidebarIsInvisible){
cy.get('#sideMenuToggleButton').click();
}
Is this possible?
I really appreciate for any contribution.
Thanks in advance
Cypress allows jQuery to work with DOM elements so this will work for you:
cy.get("selector_for_your_button").then($button => {
if ($button.is(':visible')){
//you get here only if button is visible
}
})
UPDATE: You need to differentiate between button existing and button being visible. The code below differentiates between 3 various scenarios (exists & visible, exists & not visible, not exists). If you want to pass the test if the button doesn't exist, you can just do assert.isOk('everything','everything is OK')
cy.get("body").then($body => {
if ($body.find("selector_for_your_button").length > 0) {
//evaluates as true if button exists at all
cy.get("selector_for_your_button']").then($header => {
if ($header.is(':visible')){
//you get here only if button EXISTS and is VISIBLE
} else {
//you get here only if button EXISTS but is INVISIBLE
}
});
} else {
//you get here if the button DOESN'T EXIST
assert.isOk('everything','everything is OK');
}
});
You can also use my plugin cypress-if to write conditional command chains
cy.get('...').if('visible').click()
Read https://glebbahmutov.com/blog/cypress-if/
try this code:
isElementVisible(xpathLocater) {
this.xpath(xpathLocater).should('be.visible');
};
isElementNotVisible(xpathLocater) {
this.xpath(xpathLocater).should('not.exist');
};
then use these two methods with if statement like shown below:
if(isElementNotVisible) {
}
or
if(isElementVisible) {
}

How to display standby dialog in XPages view when expanding a section

In my XPages application I am using the xe:dynamicViewPanel control and would like to add a standby/wait dialog/popup when a section is being expanded by the user (click on the expand-icon to open the section).
Sometimes the view index is not up-to-date and opening a category holding a lot of documents will last a while, in the meantime I want to display some "loading dialog" (which I already have, so, no need to explain how to do this).
My problem is, that I can not find any event or entry point where to start from.
Thank you all !
Alex
You can try code from this link:
https://openntf.org/XSnippets.nsf/snippet.xsp?id=standby-dialog-custom-control
If you want to show stanby dialog on the current section, replace the 79 line
var forms=dojo.body()
with some other container. For example, a partial refresh element
var forms = dojo.byId(refreshId)
In this case you need to replace lines 75 and 140 to pass the id parameter
function StandbyDialog_Started(refreshId) {
try{
if(StandbyDialog_Do==true){
if(this.StandbyDialog_Obj==null) {
var forms= (refreshId)?dojo.byId(refreshId):dojo.body();
this.StandbyDialog_Obj = new dojox.widget.Standby({
target: forms,
zIndex: 10000
});
document.body.appendChild(this.StandbyDialog_Obj.domNode);
this.StandbyDialog_Obj.startup();
}
StandbyDialog_StoreField()
setTimeout("if(StandbyDialog_Do==true){StandbyDialog_StoreField()}",50);
setTimeout("if(StandbyDialog_Do==true){this.StandbyDialog_Obj.show()}",200);
}
}catch(e){
console.log("StandbyDialog_Started:"+e.toString())
}
}
and
dojo.subscribe( 'partialrefresh-start', null, function( method, form, refreshId ){
StandbyDialog_Do=true
StandbyDialog_Started(refreshId)
});
I didn't test it, but I hope it can help you to go further.

Selecting a UI element not on the screen of native android app using Appium, Selenium web driver, Node js, Mocha etc

I'm testing a native Android app and need to click on a button that is off the bottom of the screen. I've seen tons of examples of this using Java and Javascript but I'm using Node.js and nothing seems to work. I'm pretty new to this stuff and have wasted far too much time on something so simple.
For example to click on an onscreen element this works:
it("Select Button Test",function(){
return driver
.setImplicitWaitTimeout(timeoutWait)
.elementByXPath('//android.widget.TextView[#text=\'My Button\']').click();
});
Also the full xpath works for onscreen elements - in this case:
var myButton ='//android.widget.LinearLayout[1]/android.widget.FrameLayout[1]/android.view.View[1]/android.widget.FrameLayout[1]/android.widget.RelativeLayout[1]/android.widget.ScrollView[1]/android.widget.LinearLayout[1]/android.widget.TableLayout[1]/android.widget.TableRow[10]');
it("Select Button Test",function(){
return driver
.setImplicitWaitTimeout(timeoutWait)
.elementByXPath(myButton).click();
});
I found the full xpath for this button by scrolling to it while the test was running and firing up the screen in appium inspector. I've tried various scroll, touch, and swipe methods from the webdriver docs but nothing is available so apparently I've wandered off the ponderosa...
So how can I access a button that is not on the screen?
I'm sure its some mundane detail. Thanks!
try the below scroll function to scroll using dimensions
public void scroll() throws IOException {
Dimension dimensions = driver.manage().window().getSize();
System.out.println("Size of Window= " +dimensions);
int scrollStart = (int) (dimensions.getHeight() * 0.5);
System.out.println("Size of scrollStart= " +scrollStart);
int scrollEnd = (int) (dimensions.getHeight() * 0.2);
System.out.println("Size of scrollEnd= " + scrollEnd);
driver.swipe(0,scrollStart,0,scrollEnd,1000); }
and then use your locator strategy to find the element
I'm use node.js to test react native app solve this problem .
The appium node.js sample code have scroll feature.First,you need import all what sample imported.Then there's the codeļ¼š
it("should scroll", function () {
return driver
.elementByXPath('//android.widget.TextView[#text=\'Animation\']')//change your element which doesn't cover it.
.elementsByXPath('//android.widget.TextView')
.then(function (els) {
return Q.all([
els[7].getLocation(),
els[3].getLocation()
]).then(function (locs) {
console.log('locs -->', locs);
return driver.swipe({
startX: locs[0].x, startY: locs[0].y,
endX: locs[1].x, endY: locs[1].y,
duration: 800
});
});
});
});
good luck !!

Element is not currently visible and so may not be interacted with node and selenium driver

I have the following code and I cannot get the driver to click the div. It keeps throwing the error
"Element is not currently visible and so may not be interacted"
when debugging you can clearly see that the element is visible. How can I ignore the warning or the error?
var webdriver = require('selenium-webdriver')
, By = webdriver.By
, until = webdriver.until;
var driver = new webdriver.Builder().forBrowser('firefox').build();
driver.get('http://www.vapeworld.com/');
driver.manage().timeouts().implicitlyWait(10, 10000);
var hrefs = driver.findElements(webdriver.By.tagName("a"));
hrefs.then(function (elements) {
elements.forEach(function (element) {
element.getAttribute('name').then(function (obj) {
if (obj == '1_name') {
console.log(obj);
element.click();
}
});
});
});
Your code is clicking an A tag with the name "1_name". I'm looking at the page right now and that element doesn't exist, hidden or otherwise.
You'd be better served by replacing the bulk of your code with a CSS selector, "a[name='1_name']" or "a[name='" + tagName + "']", that will find the element you want with a single find. You can then click on that element.
The issue you are running into is that the element you are trying to click is not visible, thus the error message. Selenium is designed to only interact with elements that the user can see, which would be visible elements. You will need to find the element you are looking for and figure out how to make it visible. It may be clicking another link on the page or scrolling a panel over, etc.
If you don't care about user scenarios and just want to click the element, visible or not, look into .executeScript().
Looked at the website and used the F12 tool (Chrome) to investigate the page:
var elements = [].slice.call(document.getElementsByTagName("a"));
var elementNames = elements.map(function (x) { return x.getAttribute("name"); });
var filledElementNames = elementNames.filter(function (x) { return x != null; });
console.log(filledElementNames);
The content of the website http://www.vapeworld.com is very dynamic. Depending on the situation you get one or more anchors with "x_name" and not always "1_name": the output of the script in Chrome was ["2_name"] and Edge returns ["1_name", "9_name", "10_name", "17_name", "2_name"]. So "you can clearly see that the element is visible" is not true in all situations. Also there were some driver bugs on this subject so it is worthwhile to update the driver if needed. See also the answers in this SO question explaining all the criteria the driver uses. If you want to ignore this error you can catch this exception:
try {
element.click();
} catch (Exception ex) {
console.log("Error!");
}
See this documentation page for more explanation.

Mouse Hover action in C# Selenium webdriver

I'm newbie to C# Selenium. I tried to automate an "sign in" and "sign out" of an LinkedIn Application. For that I have written the below code,
Here the "Sign out" is an hidden element.
My code
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.linkedin.com/");
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("login-email")).SendKeys("valid email ID");
driver.FindElement(By.Id("login-password")).SendKeys("valid password");
driver.FindElement(By.Name("submit")).Click();
Actions action = new Actions(driver);
action.MoveToElement(driver.FindElement(By.XPath("//*[#id='img-defer-id-1-6775']"))).Build().Perform(); // Getting an exception here
Thread.Sleep(3000);
driver.FindElement(By.XPath("//*[#id='account-sub-nav']/div/div[2]/ul/li[1]/div/span/span[3]/a")).Click();
But I'm getting an "No Such Element Exception". Even I tried to find the element by ID but getting the same exception. Not sure what I did wrong.
Can anyone help me.
Hoverable elements I find it's best to use JavaScript. Action Builder tends to have a high rate of failure, and will cause other hoverable elements to become visible as it scrolls through the page, causing the element that you want to become obscured. I've found this method somewhere online (can't remember where) and it works significantly better than any other method I've tried.
String javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
IJavaScriptExecutor executor = driver as IJavaScriptExecutor;
executor.ExecuteScript(javaScript, webElement);
Try adding a Thread.sleep after clicking submit button.
Here issue is selenium driver is searching for element even before page is loaded.Hence element not found exception is thrown.
Instead of Thread.sleep in your code ,you could use explicit waits.
Try this
IWebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
driver.Navigate().GoToUrl("https://www.linkedin.com/");
driver.Manage().Window.Maximize();
driver.FindElement(By.Id("login-email")).SendKeys("valid email ID");
driver.FindElement(By.Id("login-password")).SendKeys("valid password");
driver.FindElement(By.Name("submit")).Click();
Actions action = new Actions(driver);
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//*[#id='img-defer-id-1-6775']")));
action.MoveToElement(driver.FindElement(By.XPath("//*[#id='img-defer-id-1-6775']"))).Build().Perform();
wait.Until(ExpectedConditions.ElementIsClickable(By.XPath("//*[#id='account-sub-nav']/div/div[2]/ul/li[1]/div/span/span[3]/a"))).Click();
If the element you are trying to click is invisible, you can make it visible trough JavaScript and then click on it:
((IJavaScriptExecutor)Driver).ExecuteScript("THE ELEMENT YOU WANT TO CLICK.hidden = false;", element);
element.click

Resources