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(...
Related
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);
I want to locate the "item1" div tag in the following DOM. I tried in different ways. But I could not make it happen.
<html></html>
<frameset ... >
<frame>
</frame>
<frame id= "dynamic value" name = "dynamic value">
<html >
<head></head>
<body>
<div name = "item1">
<div name = "item2">
So , I tried the following ways to locate it. But no such element exception was thrown.
driver.getElementsByTagName("frame")[1].getElementsByTagName("div")[0]
driver.swithTo().frame(1);
driver.getElementsByTagName("div")[0]
You should switch to frame before you search for an element.
Try this:
frame = driver.getElementsByTagName("frame")[1]
driver.swithTo().frame(frame);
driver.getElementsByTagName("div")[0]
Note, I am not sure what is getElementsByTagName(), it looks like some sort of custom method for findElement(), so I just copypasted it into my solution from your example.
getElementsByTagName() DOM method in JavaScript
Source
Seems like you are using wrong method to locate an element using tag name.
In Selenium Java, Use below code snippet to switch into frame
driver.swithTo().frame(1); // index = 1 means 2nd frame
driver.findElements(By.tagName("div"))[0]
And with ExplicitWait conditions (to avoid synchronization related issue)
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frameName);
There are a couple of things you need to consider:
Using Selenium while dealing with <iframe> you can safely ignore the <frameset>
It would be difficult to identify the desired <iframe> through their index as it will be inconclusive which <iframe> loads faster.
As the the desired element is within an <iframe> so to locate the element you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt().
Induce WebDriverWait for the desired visibilityOfElementLocated().
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe#dynamicValue[name='dynamicValue']")));
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[name='item1']")));
Using XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#id='dynamicValue' and #name='dynamicValue']")));
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#name='item1']")));
Reference
You can find a coupple of relevant discussions in:
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
Ways to deal with #document under iframe
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");
I want to use Java Selenium to upload the file in the following input element(upload element).
<input type="hidden" ng-model="model[options.key || index]"
id="formly_21_fileupload_args_content_substrate_surface_media_documentURL_0"
name="formly_21_fileupload_args_content_substrate_surface_media_documentURL_0"
formly-custom-validation="options.validators"
class="ng-pristine ng-untouched ng-invalid ng-invalid-wizard-validation">
I have tried to use 'sendKeys' but got the error informatoion:
upload.sendKeys("filePath"); // element is invisible
then I tried to use JavascriptExecutor in the script to change the visibility of the Element (type="file"), but still got the same error: element is invisible:
JavascriptExecutor jsexec = (JavascriptExecutor) driver;
jsexec.executeScript("arguments[0].type='file'", upload);
I also tried to use the parent element 'div' to do the upload, but got the error "cannot focus the element".
is there anyone can help me to solve this problem?
As per your question and the HTML you have shared the <input> tag is having the attribute type="hidden". To invoke sendKeys() on the <input> tag you can use the following solution:
WebElement uploadElement = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#class='ng-pristine ng-untouched ng-invalid ng-invalid-wizard-validation'][contains(#name,'_fileupload_args_content_substrate_surface_media_documentURL_')]")));
String jse = "arguments[0].type='file'";
((JavascriptExecutor)driver).executeScript(jse, uploadElement);
uploadElement.sendKeys("absolute_path_of_the_file_to_be_uploaded");
What I'm guessing your issue to essentially be, is that there is probably a wrapper object to beautiful the default input element. It is common for web developers to 'hide' this default input element and as such, Selenium will fail to click on this object because its display attribute is set to 'none'.
We may need to look at the other HTML tags within that DOM hierarchy, but I suspect there is probably another input tag nested 1 or 2 levels below, that looks something like this. It should at least have a display attribute set to none:
<input display: none;> </input>
To upload a file, you will need to sendKeys directly to this element instead. But first, you will need to make it visible:
//the 'upload' variable here refers to the input element with display: none;
JavascriptExecutor jsexec = (JavascriptExecutor) driver;
//First, change the display to inline to expose the underlying input element
jsexec.executeScript("arguments[0].display='inline;', upload);
After the above code is executed you should see a raw input element appearing. This is the element you would want to sendKeys to:
//After that you can go ahead to upload the file:
upload.sendKeys("path of the file");
In all honesty, without seeing the entire DOM, I am not able to advise whether the below line is necessary. You will need to try and see for yourself:
jsexec.executeScript("arguments[0].type='file'", upload);
If no such nested input element exists, just try the code above with the current input element you found.
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&pluginId=f8acc58b-c6d5-42df-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']"));