NoSuchElementException: no such element: Unable to locate element:while identifying draggable and droppable element - selenium

I am actually new to selenium. I am trying to do drag and drop operation on a demo website : http://jqueryui.com/droppable/ (Here go to demo-->Droppable).
Following is my html source :
<div id="draggable" class="ui-widget-content ui-draggable ui-draggable-handle" style="position: relative;">
<p>Drag me to my target</p>
</div>
Following is my code block :
WebElement drag=dr.findElement(By.xpath("//*[#id='draggable']"));
wait.until(ExpectedConditions.elementToBeClickable(drag));
WebElement drop=dr.findElement(By.xpath("//*[#id='droppable']"));
wait.until(ExpectedConditions.elementToBeClickable(drop));
//act.moveToElement(drop).build().perform();
act.dragAndDrop(drag, drop).build().perform();

The element to drag and the element to drop are within an <iframe>. So you have to switch to the intended frame first then locate the draggable and droppable elements and perform dragAndDrop() as follows :
Here is the complete code snippet :
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://jqueryui.com/droppable/");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[#class='demo-frame']")));
WebElement from = driver.findElement(By.id("draggable"));
WebElement to = driver.findElement(By.id("droppable"));
new Actions(driver).dragAndDrop(from, to).build().perform();
System.out.println("Drag and Drop Completed");
driver.quit();

The problem is that the drag & drop elements are located within an iframe. You need to switch to the iframe with class='demo-frame'
Once you switch to the iframe, you can then locate and interact with your element.

Related

Unable to switch to iframe using Selenium Webdriver

I am trying to learn the feature drag and drop in the webpage
Link
The Section Books is inside the iframe.
But i am unable to access the iframe I am getting below error
no such element: Unable to locate element:
Below are the Xpath i tried
// WebElement frame =driver.findElement(By.xpath("//div[#id='root']//iframe[#class='st-preview-body']"));
// WebElement frame =driver.findElement(By.tagName("iframe"));
WebElement frame =driver.findElement(By.xpath("//iframe[#src='https://snippet.webixcode.com/snippet.html?0.0.3']"));
driver.switchTo().frame(frame);
I also tried using by.id and also xpath using id and class name
What will be the mistake i am doing? To my knowledge there is only one iframe present
The code for drag and drop. will this work?
WebElement fromdrag=driver.findElement(By.xpath("//span[#class='dhx_tree-list-item__text'][normalize-space()='Lawrence Block']"));
WebElement todrop=driver.findElement(By.id("treeTarget"));
Actions act=new Actions(driver);
act.dragAndDrop(fromdrag, todrop).build().perform();
Its a Nested iframe. We need to switch to those iframes one by one to access Elements from All Books.
The page takes time to load, better apply Explicit waits to find the Elements. And you also need to driver.switchTo().defaultContent(); to come out of the iframe and find Elements outside the iframe
public void iframequestion() {
System.setProperty("webdriver.chrome.driver","C:\\expediaproject\\Chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
WebElement frame1 = driver.findElement(By.xpath("//iframe[#src='https://snippet.dhtmlx.com/3e0b5r57?mode=mobile']"));
js.executeScript("arguments[0].scrollIntoView(true);", frame1);
driver.switchTo().frame(frame1);
WebElement frame2 = driver.findElement(By.xpath("//iframe[#class='st-preview-body']"));
driver.switchTo().frame(frame2);
WebElement frame3 = driver.findElement(By.id("content"));
driver.switchTo().frame(frame3);
WebElement ele1 = driver.findElement(By.xpath("//span[text()='Fiction & Fantasy']"));
System.out.println(ele1.getText());
driver.switchTo().defaultContent();
WebElement ele2 = driver.findElement(By.xpath("//a[text()='View more demos']"));
System.out.println(ele2.getText());
driver.quit();
}
Fiction & Fantasy
View more demos

How to fix error in identifying element using xpath for SAP Fiori UI

I'm writing a selenium script for identifying an element and send keys to it for a SAP interface.
This for a SAP Fiori UI application.
Even though the xpath I have identified is unique when i run the code in chrome its not identifying the element.
Below is my code :
public static void main(String[] args) {
// declaration and instantiation of objects/variables
String baseURL = "https://mercury.abc.net/default.aspx";
WebDriver driver;
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\abcde\\Desktop\\Eclipse\\Selenium
Practice\\libs\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
driver = new ChromeDriver(options);
// launch Chrome and direct it to the Base URL
driver.get(baseURL);
WebElement MyTimesheetButton =
driver.findElement(By.id("Tile_WPQ8_8_7"));
MyTimesheetButton.click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*
[#id=\"WD027B\"]")));
WebElement Engagement =
driver.findElement(By.xpath("//[#id=\"WD027B\"]"));
Engagement.sendKeys("test");
//close Chrome
driver.close();
}
I should get expected result as element identified and data keyed in to the element but I'm getting actual result as below :
Cannot locate an element using By.xpath: //*[#id="WD027B"]
HTML:
<iframe role="main" frameborder="0" title="The title of the hosted application within the canvas area: My Timesheet" '="" src="/nwbc/ZEP3_FIN_TM_ENTRY_DYNPRO_MSTR/?sap-client=200&sap-language=EN&sap-nwbc-node=page_collection&sap-nwbc-version_hash=392D742742D08304E969040C8A992A95&sap-theme=zcorbu" id="iFrameId_1550067729776" name="iFrameId_1550067729776" style="display: block; width: 1036px; height: 641px;">Your browser is currently configured not to display inline
<table id="WD027B-r" class="lsTblEdf3Whl lsField--table"> <tbody> <tr> <td class="lsTblEdf3Td urBorderBox" style="vertical-align:top;"><input id="WD027B" ct="CBS" lsdata="{0:'WD027B',5:'FREETEXT',7:'WD027C',14:true,20:40,25:'CLIENT_SERVER_PREFIX',26:'F4LOOKUP',30:true,32:40,34:true,35:'VALUE1'}" class="lsTblEdf3 urBorderBox urEdf2TxtHv" value="" role="combobox" name="WD027B" style="vertical-align:top;"> </td> </tr> </tbody> </table>
</iframe>
Try this XPath It should work.
WebElement Engagement =driver.findElement(By.xpath("//table[#id='WD027B-r']/tbody/tr/td/input[#id='WD027B']"));
Engagement.sendKeys("test");
Please let me know if this work for you.
As the element is within an <iframe> and a dynamic element so you need to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='iFrameId_'][src*='FIN_TM_ENTRY_DYNPRO_MSTR']")));
WebElement Engagement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.lsField--table td.urBorderBox>input.urBorderBox[ct='CBS']")));
xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#id, 'iFrameId_')][contains(#src, 'FIN_TM_ENTRY_DYNPRO_MSTR')]")));
WebElement Engagement = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[contains(#class, 'lsField--table')]//td[contains(#class, 'urBorderBox')]/input[contains(#class, 'urBorderBox') and #ct='CBS']")));

Selenium cannot click menu-item from bootstrap dropdown

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", tmpElement);
List<WebElement> dd_list = driver.findElementsByXPath("//*[#id='masterNavigation']/ul/li[1]/ul/li");
for (WebElement ele : dd_list)
{
System.out.println("Values " + ele.getAttribute("innerHTML"));
if (ele.getAttribute("innerHTML").contains("Event Dashboard")) {
ele.click();
break;
}
}
}
Hi I am trying to Automate bootstrap drop-down menu. It's visibility is hidden by default.Once you hover mouse on it, its visibility property shows visible.I am able to click on drop-down , but after clicking on drop-down my selenium script is not selecting value from drop-down.
Error: Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: Cannot click on
element
HTML Code Snippet
<a class="ui-button-text-icons" id="ctl00_Sitemap1_HyperLink1" href="javascript:void(void);">
<span style="padding-right: 1.3em;">Dashboards</span>
<span class="ui-button-icon-secondary ui-icon ui-icon-triangle-1-s"></span>
</a>
<ul style="visibility: hidden;">
<li class="first featureGranted">
Classic Dashboard
</li>
</ul>
Few things
You don't need to traverse though all li elements to find your desire element you can do it with Xpath
I have no Idea why you are using JavaScript to click first Element, but unless click method provided by Selenium is not working I would suggest not to use JavaScript Click
Error suggests that element is not visible, it could be due to multiple reasons. You could wait using Explicit wait until element is visible as mentioned below. it might resolve your issue
Code
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
action.moveToElement(we).build().perform();
WebElement tmpElement= driver.findElement(By.xpath("//*[#id='ctl00_Sitemap1_HyperLink1']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
// I have no idea why you are clicking using JavaScript
js.executeScript("arguments[0].click();", tmpElement);
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement eventDashboardMenu = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[contains(text(),'Event Dashboard')]")));
eventDashboardMenu.click();

I would like to click on New Contact button from web page for my selenium script

I would like to click on a "New Contact" button for my selenium script. I have tried:
driver.findElement(By.id("btn-group.contact_list-menu-contact_add")).click();
And by xpath as well, but it is not working. How could I get this working?
<div class="btn-group left">
<a id="contact_list-menu-contact_add" class="Button btn-contactadd primary SaveItem" href="javascript:">New Contact</a>
</div>
You are searching by an incorrect id value, use contact_list-menu-contact_add instead:
driver.findElement(By.id("contact_list-menu-contact_add")).click();
Or, by a CSS selector:
driver.findElement(By.cssSelector(".btn-group .btn-contactadd")).click();
driver.findElement(By.cssSelector(".btn-group #contact_list-menu-contact_add")).click();
driver.findElement(By.cssSelector("#contact_list-menu-contact_add")).click();
Or, by a link text:
driver.findElement(By.linkText("New Contact")).click();
If the target element is inside an iframe, you would need to switch into the context of the frame before searching for the element. Assuming that your frame has contactURL id, this is how to switch to it:
driver.switchTo().frame("contactURL");
If you are getting NoSuchElementException as you have mentioned in the comment, There are may be two reason :-
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 and clickable as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
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);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("contactURL"));
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
Edited :- As I see from your provided HTML this button is inside <div id="btn-new-group" class="btn-group-actions left" style="display: none;"> which is set to be invisible, that's why you are not able to find button. You should make it visible first then try to find as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("contactURL"));
WebElement invisibleDiv = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("btn-new-group")));
//Now make it visible first
((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block';", invisibleDiv);
//Now find contact button
WebElement el = wait.until(ExpectedConditions.elementToBeClickable(By.id("contact_list-menu-contact_add")));
el.click();
Hope it helps...:)

How to click a button created using <span> tag for Selenium webdriver?

I have span tag which looks like a button on html tag
<span class="middle">Next</span>
I tried using
xpath=driver.findElement(By.xpath(".//*[#id='modal-actions-panel']/div[2]/a/span/span/span")); // by considering fixed id as reference
Using absolute
xpath=driver.findElement(By.xpath("html/body/div[4]/div[2]/a/span/span/span")); // took this from firebug
and Using
driver.findElement(By.cssSelector("span[class='middle']"));
No success!! It is throwing below exception :
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[contains(., \"Next\")]"}
Command duration or timeout: 30.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
For all the ways I tried it is showing the same exception with change in selector details. Can someone please help me out in finding solution so that I can find Next button that is in span tag and click it.
Next button is in iFrame: Below is the part of html covering required span tag.
Next
I also tried with :
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
But throwing below error :
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Kindly let me know if needed something that I'm missing..
I think this element is inside a frame or iframe, if it is then you need to switch that frame or iframe before finding element as below :-
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
el.click();
//Now after all your stuff done inside frame need to switch to default content
driver.switchTo().defaultContent();
Edited1 :- If you are getting exception as element is not currently visible need to implement WebDriverWait to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//span[#class = 'middle' and contains(text(), 'Next')]")));
el.click();
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Edited2 :- If unfortunately it's not getting visible try to click on it using JavascriptExecutor as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//span[#class = 'middle' and contains(text(), 'Next')]")));
//Now click using JavascriptExecutor
((JavascriptExecutor)driver).executeScript("arguments[0].click()" el);
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Try this....
driver.findElement(By.xpath("//span[contains(#class,'middle') and contains(text(), 'Next')]"))
I tried it and it worked for me:
WebElement actionBtn=driver2.findElement(
By.xpath("//span[contains(#class,'v-menubar-menuitem-caption')
and contains(text(), 'Actions')]")
);
actionBtn.click();
Try this
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(#class,'middle') and contains(text(), 'Next')]"))).click();