Unable to locate element using Selenium Webdriver - java

I am having sticky nav bar which has few list elements where each contains href element. When I tried to locate element I am getting the error.
Following is my HTML code :
<div class="nav" id="sticky">
<div class="container">
<ul class="main-nav">
<li>Dashboard</li>
<li><a href="../MEFAcademicDash/StudentUI/StudentHome.aspx"
title="Academic Dashboard">Academic</a></li>
<li>Notices</li>
I want to locate Academic through Webdriver, I am getting the error like this Unable to locate the element.
org.openqa.selenium.NoSuchElementException: Unable to locate element: /html/body/div/form/div[5]/div[1]/ul/li[2]/a

As per the HTML you have shared you need to induce WebDriverWait for the element to be visible / clickable. To click() on the link with text Academic you can use the following line of code :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='nav' and #id='sticky']/div[#class='container']/ul[#class='main-nav']//li/a[#title='Academic Dashboard' and contains(#href,'../MEFAcademicDash/StudentUI/StudentHome.aspx')]"))).click();

Can you try below-mentioned xpath?
//*[text() = 'Academic']
Also, use actions driver to perform click action

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);

How to switch to the iframe as per the HTML provided through Selenium and Java?

This way I tried to switch to the frame:
driver.switchTo().frame(driver.findElement(By.xpath(("//iframe[contains(#name,'vfFrameId')]//following::iframe[2]"))));
There are three iframes on the complete html code and I am trying to switch to the third one and I am able to the locate that as well using xpath but after running the script driver is not able to locate that and throwing an exception.
Exception:
no such element: Unable to locate element: {"method":"xpath","selector":"//iframe[contains(#name,'vfFrameId')]//following::iframe[2]"}
HTML:
<div class="windowViewMode-normal oneContent active forcePageHost" data-aura-rendered-by="1330:0" data-aura-class="forcePageHost">
<div class="oneAlohaPage" data-aura-rendered-by="1335:0" data-aura-class="oneAlohaPage">
<force-aloha-page data-data-rendering-service-uid="240" data-aura-rendered-by="1338:0" force-aloha-page_aloha-page="">
<div class="slds-template_iframe slds-card" force-aloha-page_aloha-page="">
<iframe height="100%" width="100%" scrolling="yes" allowtransparency="true" id="vfFrameId_1536141890078" name="vfFrameId_1536141890078" allowfullscreen="true" force-aloha-page_aloha-page="" allow="geolocation *; microphone *; camera *" title="Deploy Data Set"></iframe>
</div>
</force-aloha-page>
</div>
</div>
Here is how you can find the index of each iframe element
List <WebElement> options= driver.findElements(By.tagName("iframe"));
for(WebElement e: options) {
String index=e.getAttribute("index");
System.out.println("Index for: "+e.getAttribute("name")+" is: "+index);// to print out name attribute and index
}
If you are switching from iframe to iframe that don't work all the times. You may need to switch to default content from iframe(1) first and then switch to iframe(2)
driver.switchTo().frame(your locator for 1st iframe here);// to switch to 1st iframe
// your action here
driver.switchTo().defaultContent();
driver.switchTo().frame(your locator for 2nd iframe here); // to switch to 2nd iframe
I am working from your comment:
so you can see the code above where you can see that there are three iframes available on the browser. what happens that i click on one link then to interact with the elements i have to switch to the iframe and here i am able to switch but after clicking on an element ,i am getting navigated to the next page and there is another iframe introduced where i am not able to locate the iframe
You need the following sequence of events:
Switch to your iframe. From this point, your driver instance is pointing at this iframe!
Click on the element that transitions to the next page.
Your driver instance is still pointing at the iframe from step 1! You need to reset it to the top document using: driver.switchTo().defaultContent();.
Only now can you switch to the new iframe on the page you just navigated to.
As per the text based HTML and the HTML snapshot you have shared, it is not evident if there are multiple iframes available. So considering this <iframe> as a unique iframe you need to induce WebDriverWait while switching to the <iframe> as follows:
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='vfFrameId_'][name^='vfFrameId_'][title='Deploy Data Set']")));
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#id,'vfFrameId_')][starts-with(#name,'vfFrameId_')][#title='Deploy Data Set']")));
Incase there are multiple iframes available with the same attributes which we have used above you can use the following solution:
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//div[#class='windowViewMode-normal oneContent active forcePageHost' and #data-aura-class='forcePageHost']//following::iframe[1]")));

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']"));

Opening a href link in Selenium Java

I am trying to click on the following a href link in selenium. I have tried using xpath and By.linkText() and By.cssSelectort() but I have not be able to locate the element. Any help is much appreciated
driver.findElement(By.linkText("CCC_PH3_Sandbox_Keybridge: CCC PH3 Sandbox Keybridge")).click();
and By.xpath() but its unable to locate the element
WebElement course = driver.findElementByXPath("html/body/table/tbody/tr/td/div/div[2]/table/tbody/tr/td[3]/a/span[1]");
course.click();
css locator :
driver.findElement(By.cssSelector("a[href*='/webapps/portal']")).click();
Here is the html snippet:
<img width="12" height="12" src="/images/ci/icons/bookopen_li.gif" alt="">
<a target="_top" href=" /webapps/portal/frameset.jsp?tab_tab_group_id=_2_1&url=%2Fwebapps%2Fblackboard%2Fexecute%2Flauncher%3Ftype%3DCourse%26id%3D_2135_1%26url%3D">CCC_PH3_Sandbox_Keybridge: CCC PH3 Sandbox Keybridge</a>
Try using By.partialLinkText() as below :-
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("CCC_PH3_Sandbox_Keybridge"))).click();
Edited:- As you have mentioned this element is inside two iframes with id navFrame and contentFrame, you need to switch that iframes one by one before finding element as :-
driver.switchTo().frame("navFrame");
driver.switchTo().frame("contentFrame");
//Now find desire element
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("CCC_PH3_Sandbox_Keybridge"))).click();
//After doing all stuff inside iframe switch back to default content
driver.switchTo().defaultContent();

How to get a text from following div using Selenium Webdriver

I am trying to get the text content of the items inside the following 2 div elements but getting an error that the element cannot be found. For example the text content for first div is "Text1". How can I get that text?
So far, I have tried:
driver.findElement(By.xpath("//*[#id='ctrlNotesWindow']/div[3]/ul/div[1]/div[2]/div[2]")).getText())
and that complains of not finding that element.
Here is the html code:
<div class="notesData">
<div class="notesDate" data-bind="text: $.format('{0} - {1}', moment(Date).format('MMM DD, YYYY h:mm a'), User)">Text1</div>
<div class="notesText" data-bind="text: Note">Text2 on next line</div>
</div>
Here is the error, I get:
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to locate element:
{"method":"xpath","selector":"//*[#id='ctrlNotesWindow']/div[3]/ul/div[1]/div[2]/div[2]"}
Don't use meaningless XPath. Your error message tells you "NoSuchElement", so you had your locator wrong, which has nothing to do with getText (not yet). Try using CssSelector or meaningful XPath:
driver.findElement(By.cssSelector("#ctrlNotesWindow .notesData > .notesDate")).getText();
If for whatever reason you'd want to stick with Xpath:
driver.findElement(By.xpath("//div[#class='notesData']/div[#class='notesDate']")).getText();
Otherwise, the css selector answer above is a good option.
Another alternative is to locate the element by class name:
driver.findElement(By.className("notesDate")).getText();
It works with:
driver.findElement(By.xpath("//div[#class='ui-pnotify-text']")).getText();
which is an PNotify notification.

Categories