Why Am I Getting a NoSuchElementException When Running Tests in Cucumber - java

I've spent the last few days writing tests in Cucumber. The tests I've written so far work fine, I've been able to click objects, sendkeys over. No problem.
I've now gotten to these page elements that cannot be found no matter what selector I use. I've tried using wait, but even though they are clearly visible on the page, they can't be accessed.
This is happening in both table row elements that I want to click on, and a text input I want to sendkeys to. I've included the latter below.
<input type="text" name="EMPLOYEE_label" value="" class=""
onkeypress="return dtPk(event,this);" onkeydown="return dtKd(event,this);"
onchange="dltCh(this,'EMPLOYEE__test');" size="30" wbvalid="true"
isresolving="false">
Here is the code I am using at present.
webdriver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(webdriver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.name("EMPLOYEE_label")));
JOptionPane.showMessageDialog(null, "WebDriver =" + webdriver);
WebElement empIDTextInput = webdriver.findElement(By.name("EMPLOYEE_label"));
empIDTextInput.sendKeys("Bennett");
Thread.sleep(1000);
gtaProxyPage.clickFindButton().click();
Thread.sleep(1000);
gtaProxyPage.checkAssociateBox().click();
gtaProxyPage.loadTimesheet().click();
Thread.sleep(2000);
EDIT:
I changed the code to this. It more closely resembles what I started with
Thread.sleep(30000);
//this calls for the input element by className.
gtaProxyPage.UserEntersNumberUnderTimesheet().click();
gtaProxyPage.clickFindButton().click();
gtaProxyPage.checkAssociateBox().click();
gtaProxyPage.loadTimesheet().click();
This is the error I'm getting now
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"input.triggerButton"}
I switched what I"m doing to click a button that opens a modal, and allows me to use a text field within that. But the button is not being seen.

This error message...
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"class name","selector":"input.triggerButton"}
...implies that the no such element was found using the Locator Strategy with classname attribute as input.triggerButton.
Irespective of all the changes and manipulation done while publishing the relevant HTML within the question, to send a character sequence to the element:
<input type="text" name="EMPLOYEE_label" value="" class="" onkeypress="return dtPk(event,this);" onkeydown="return dtKd(event,this);" onchange="dltCh(this,'EMPLOYEE__test');" size="30" wbvalid="true" isresolving="false">
As the element is a dynamic element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[name='EMPLOYEE_label'][onchange*='EMPLOYEE__test']"))).sendKeys("Bennett");
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#name='EMPLOYEE_label' and contains(#onchange, 'EMPLOYEE__test')]"))).sendKeys("Bennett");

Related

Selenium can't find iframe after postback inside the iframe after finding one element inside the frame

I am trying to create a test where I have to fill out some information inside an iframe. Getting access to the iframe work fine and I can fill out information inside the frame. The issue is that when I find out a element 'A' it has a postback attached to it which reloads the content inside the iframe, to find out another element 'B'. So i am not able to find that element.I am getting below error:
org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
(Session info: chrome=85.0.4183.83)
Here are my observations: When I first locate the iframe it looks like this:
<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7">
After the postback has occurred it looks like this:
<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="a5006acf28d8c288313681ab9ad7a4fa">
I can easily find element A:
But element B i am not able to find
The code fails when I try to get hold of the iframe element.
How can I get hold of the iframe again, after the postback inside the frame?
I have tried this suggestion also but it is not working
//Ensure that you are back to the base frame
driver.SwitchTo().DefaultContent();
//SwitchTo the intended frame
driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[contains(#src,'<removed for clearity>')]")));
Use a driver.executescript() for the first problem since another element is receiving the click.
element = driver.findElement(By.id(""));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
This error message...
org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
(Session info: chrome=85.0.4183.83)
...implies that the WebDriverException was raised as you tried to invoke click() on the <iframe> element.
Factually, instead of clicking on the <iframe> element you would invoke click() on an element within the <iframe>. Moreover, as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe_xpath")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
References
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
I am able to extract the data of the active cases rise and fall for every state. There is a single frame which contains both the locators shared by you. Check below working code.
driver.get("https://www.ndtv.com/coronavirus");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.className("tab-wrapper")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1')]")));
//cases down
List<WebElement> eleCasesUp = driver.findElements(By.xpath("//tr//td[3]//p//span[#class='data-up']"));
List<String> casesUpList = new ArrayList<String>();
for (WebElement element : eleCasesUp) {
casesUpList.add(element.getText());
}
//cases up
List<WebElement> eleCasesDown = driver.findElements(By.xpath("//tr//td[3]//p//span[#class='data-down']"));
List<String> casesDownList = new ArrayList<String>();
for (WebElement element : eleCasesDown) {
casesDownList.add(element.getText());
}
System.out.println("Cases Up List -->" + casesUpList);
System.out.println("Cases Down List -->" + casesDownList);

Always getting "org.openqa.selenium.ElementNotVisibleException: element not visible" exception

I have tried maximum possible ways but always getting :
org.openqa.selenium.ElementNotVisibleException: element not visible
exception for specific web element (with in popup window) during execution of selenium script.
Element Xpath value is:
Element selector value is: #edited_name
Copied value of element is:
<input required="required" type="text" id="edited_name" name="edited_name" value="AT Main Category1" placeholder="" class="form-control">
Try adding a wait - Explicit wait or Implicit wait to wait for the element to become visible. May be add a 10 seconds wait.
Also, when using Xpath, don't use #, use by.cssSelector("input#edited_name");
As the desired element is a <input> element you can induce WebDriverWait as follows :
cssSelector :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("//input.form-control#edited_name"))).click();
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='form-control' and #id='edited_name']"))).click();
If your unvisible element is in a pop-up window, you need to switch the driver to the pop-up window.
driver.switchTo().alert();
driver.findElement(...

Cannot find webelement using Selenium if they are dynamic

I'm having difficulties in locating a web element in seleniumn the html is as follows:
<div class="d2l-fieldgroup">
<div class=" vui-field-row">
<span class="d2l-field vui-label d2l-offscreen">Do not ask me again for this application</span>
<label class="d2l-checkbox-container">
<input name="dontAsk" value="0" type="hidden">
<input id="dontAsk" class="d2l-checkbox vui-input vui-outline" type="checkbox">
Do not ask me again for this application
</label>
</div>
</div>
I need to click on the checkbox or text but I just cant seem to be able to do this as I always get an error back saying:
Unable to locate element: {"method":"class name","selector"
I tried this:
WebElement do_not_ask_resourcebank = driver.findElement(By.className("d2l-fieldgroup"));
do_not_ask_resourcebank.click();
But get the following error:
no such element: Unable to locate element: {"method":"class name","selector":"d2l-fieldgroup"}
I also tried replacing d2l-fieldgroup with vui-field-row and d2l-checkbox-container
I also tried:
WebElement do_not_ask_resourcebank = driver.findElement(By.cssSelector("html/body/form/div/div[3]/div/label"));
and
WebElement do_not_ask_resourcebank = driver.findElement(By.xpath("id('confirmForm')/x:div/x:div[3]/x:div/x:label"));
but I just cannot seem to click on this element, very frustrating for a newbie like myself. Can someone point me to where I am going wrong?
If you are getting NoSuchElementException as your provided exception, There may be following reasons :-
May be you are locating with incorrect locator, you're locating actually <div> element with class name which looks like dynamically generated class name instead of actual <input type = 'checkbox'> element which you want. And other also looks unstable, You should try to locate desire element using By.id() as :-
WebElement do_not_ask_resourcebank = driver.findElement(By.id("dontAsk"));
May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Edited :
I havent tried the iframe yet because I am not sure what I am doing I havent used this tool much. The html code for the iframe is : <iframe id="d2l_1_69_954" class="d2l-iframe d2l-iframe-fullscreen d2l_1_67_746" name="d2l_1_69_954" src="/d2l/lms/remoteplugins/lti/launchLti.d2l?ou=6606&plugin‌​Id=f8acc58b-c6d5-42d‌​f-99d9-e334f825c011&‌​d2l_body_type=3" title="TALLPOD" scrolling="auto" allowfullscreen="" onload="this.m_loaded=true;" style="height: 314px;" frameborder="0"> how do you locate this iframe using your example above?
Looks like iframe id and name both are dynamically generated which could be change every time when you locate, you should try to locate this iframe with unique locator, I think it's title name is unique and fixed so you should try to locate this iframe before locating desired element as :-
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='TAFEPOC']"));

I am unable to get xpath for walmart homepage dropdown box using selenium webdriver in Java

I am trying to code in java using selenium webdriver to click on Dropdown list in walmart page. But I am unable to access the li elements.
<button class="js-flyout-toggle dropdown" aria-haspopup="true" type="button" data-cat-id="0" aria-expanded="false"> All </button>
<div class="js-flyout-modal flyout-modal">
<ul class="block-list">
<li><button class="no-margin font-semibold" type="button" data-cat-id="0" tabindex="-1"> All Departments </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="91083" tabindex="-1"> Auto & Tires </button></li>
<li><button class="no-margin font-semibold" type="button" data-cat-id="5427" tabindex="-1"> Baby </button></li>
I wanted to access Baby using selenium webdriver in Java.
Below is my code:
driver.get("http://www.walmart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = driver.findElement(By.xpath("html/body/div[2]/header/div[3]/div/div/div/div/div[3]/form/div/div[1]/div/button"));
dropdown.click();
List<WebElement> liElements = driver.findElements(By.xpath("//*[#class='block-list']/li"));
for ( WebElement we: liElements) {
System.out.println(we.getText());
}
But this gives an error as below:-
"Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible and so may not be interacted with Command duration
or timeout: 132 milliseconds".
Please help
You defined WebDriverWait but you don't use it
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("dropdown")));
Implicit wait will wait for element to exist in the DOM. If you want to use explicit wait you need to use Expected Conditions as well. Only defining WebDriverWait doesn't actually do anything.
Your code looks fine. Please try below xpath :-
//div[#class='js-flyout-modal flyout-modal']//ul[#class='block-list']/li
Now first try to put thread.sleep if it works then it is problem of wait only
Thread.sleep(30000);
But then don't use Thread for your script as it's not recommended .. It's just to ensure that script is failing because of time
Hope it will help you :)
May be other element in walmart page match your xpath //*[#class='block-list']/li and that element is not visible.

Selenium Unable to Find Element

I am trying to find an element on a website using Selenium. The page I am looking at is:
http://www.usaswimming.org/DesktopDefault.aspx?TabId=1470&Alias=Rainbow&Lang=en-US
Specifically I am trying to find the element for the "Last Name" input box and send keys in Java. the html looks like this for the text box:
<div class="field">
<input id="ctl62_txtSearchLastName" type="text" maxlength="36" name="ctl00$ctl62$txtSearchLastName">
</div>
Therefore, I initially attempted to get the element through the id which is unique:
WebDriver driver = new InternetExplorerDriver();
driver.get(timeSearchSite);
...
driver.findElement(By.id("ctl62_txtSearchLastName")).sendKeys(lastName);
However this generated the following error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with id == ctl62_txtSearchLastName (WARNING: The server did not provide any stacktrace information)
I also tried to use the name which failed similarly. Since this is a javascipt page I thought that I might need to wait before trying to access the element. I tried an explicit and an implicit wait and both resulted in timeouts. My next thought was that I was in the wrong frame but I cannot seem to find another frame. I tried indexing since I don't have names but I couldn't locate any other frames. There is this at the top of the page which might be messing selenium up:
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-WMWM93" height="0" width="0"
style="display: none; visibility: hidden"></iframe></noscript>
What could be causing this error? How can I locate the element? Any help would be greatly appreciated/
Using an explicit wait worked for me (tried both chrome and firefox):
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id("ctl62_txtSearchLastName"))
);
element.sendKeys("test");
Resulted into:

Categories