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

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

Related

Caused by: org.openqa.selenium.ElementClickInterceptedException: element click intercepted: [duplicate]

I have a project that I am working on with java and selenium.
the test work OK in UI mode.
However in headless mode I get this error
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
how can I resolve this issue (working in UI mode). this is my code
WebDriver driver = getWebDriver();
WebElement element;
Thread.sleep(60000);
element = driver.findElement(By.xpath("//label[#formcontrolname='reportingDealPermission']"));
element.click();
why in selenium there is no operation to move to the element and break all layers.
this is the UI.
this is working in UI mode not working in headless mode, made sleep for 6 minutes and not resolved so this is not time issue
This error message...
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562). Other element would receive the click: <div _ngcontent-yrc-c26="" class="footer">...</div>
...implies that the click on the desired element was intercepted by some other element.
Clicking an element
Ideally, while invoking click() on any element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
Update
After changing to headless if it still doesn't works and still get exception there still a couple of other measures to consider as follows:
Chrome browser in Headless mode doesn't opens in maximized mode. So you have to use either of the following commands/arguments to maximize the headless browser Viewport:
Adding the argument start-maximized
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
Adding the argument --window-size
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1400,600");
WebDriver driver = new ChromeDriver(options);
Using setSize()
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.manage().window().setSize(new Dimension(1440, 900));
You can find a detailed discussion in Not able to maximize Chrome Window in headless mode
Additionally, you can also wait for the intercept element to be invisible using the ExpectedConditions invisibilityOfElementLocated before attempting the click() as follows:
cssSelector:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("div.footer")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("label[formcontrolname=reportingDealPermission][ng-reflect-name=reportingDealPermission]"))).click();
xpath:
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[#class='footer']")));
new WebDriverWait(getWebDriver(), 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission' and #ng-reflect-name='reportingDealPermission']"))).click();
References
You can find a couple of related relevant discussions in:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
Element MyElement is not clickable at point (x, y)… Other element would receive the click
Try adding an explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
and if this doesn't work then try using the JS Executor
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
None of the above answers worked for me. Try using action class as follows:
WebElement element = driver.findElement(By.xpath("//div[#class='footer']"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
For this issue:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <label _ngcontent-yrc-c26="" formcontrolname="reportingDealPermission" nz-checkbox="" class="ant-checkbox-wrapper ng-untouched ng-pristine ng-valid" ng-reflect-name="reportingDealPermission">...</label> is not clickable at point (161, 562).
Another element receives the click:
Answer is to explicitly wait with javascript executor. This combination is working for me:
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
WebDriverWait wait = new WebDriverWait(driver, 10); ---> has deprecated and it gives an error. Please use below instead:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//label[#formcontrolname='reportingDealPermission']"))).click();
Ensure to import relevant items for the WebDriverWait and ExpectedConditions.
In my case "JavaScript" works:
WebElement ele = driver.findElement(By.xpath("(//input[#name='btnK'])[2]"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click()", ele);
It was taken from:
http://makeseleniumeasy.com/2020/05/25/elementclickinterceptedexception-element-click-intercepted-not-clickable-at-point-other-element-would-receive-the-click/
Answer is worked for me.
Please check the below sreenshot for the my problem reference.
Below alert is comes in between the the my button and frame.
enter image description here
In my case, nothing worked. Only the THREAD.SLEEP() method worked!

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

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.

NoSuchElementException: Unable to locate element uisng selenium web-driver

Here i am trying to click a button using css selector find method.But its unable to locate the element. So i tried giving the time also but still its unable to click the element
Code sample -->
WebDriverWait waitr = new WebDriverWait(driver, 10);
WebElement element = waitr.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#userTable > tbody > tr:nth-child(3) > td.sorting_1 > div > a")));
element.click();
My HTML body Looks as follows
<a data-toggle="dropdown" class="mainAnchor" aria-expanded="false">CA05
<span class="caret" style="margin-left:5px;"><span> </span></span></a>
If i use Thread.sleep(1000); instead of wait it works fine but i don't want to use Thread.sleep.Please help me to solve this issue
To click on the element with text as CA05 you can use the following line of code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.mainAnchor[data-toggle=dropdown]"))).click();
Hi you can use a fluentwait like the following below.
static Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(elementWaitTime, SECONDS)
.pollingEvery(2,SECONDS)
.ignoring(NoSuchElementException.class);
WebElement element = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver aDriver) {
driver= aDriver;
element.click();
return aDriver.findElement(cssSelector("#userTab a.mainAnchor"));
}
});

Selenium driver find element not working

Selenium web driver finds element unable to find input field on the web page I tried this each and every option XPath, by CSS, by name. the testing site on the local environment.
HTML:
<input class="form-control ng-pristine ng-untouched ng-valid ng-empty" ng-model="email" placeholder="Username" aria-describedby="username" type="text">
xpath:
/html/body/div[2]/div/div/div[1]/div/div/div[2]/div/form/div[2]/div/div/input
css selector:
html.no-js.ng-scope body.pace-done.full-width div#wrapper
div.page-wrapper.white-bg.ng-scope
div.wrapper.wrapper-content.ng-scope div.login-bg.ng-scope
div.container div.row
div.col-sm-6.col-sm-offset-3.col-md-4.col-md-offset-4 div.login-form
form.ng-pristine.ng-valid div.col-sm-12.plr10px div.form-group
div.input-group
input.form-control.ng-pristine.ng-untouched.ng-valid.ng-empty
driver.findElement(By.name("username"))
Here is the Answer to your Question:
As per the HTML you provided, you can use the following xpath to identify the element-
WebElement element = driver.findElement(By.xpath("//input[#ng-model='email' and #placeholder='Username']"));
Incase you are facing an ElementNotVisible exception you can induce ExplicitWait to wait for the element to be clickable as follows:
WebDriverWait wait7 = new WebDriverWait(driver, 10);
WebElement element7 = wait7.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#ng-model='email' and #placeholder='Username']")));
element7.click();
Let me know if this Answers your Question.
Try this, this should work.
WebElement emailInput = new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[ng-model='email']")));
JavascriptExecutor je = (JavascriptExecutor) driver;
WebElement Username = driver.findElement(By.xpath("//*[#placeholder='Username' and #type='text'"]));
je.executeScript("arguments[0].scrollIntoView(true);",Username);
Username.sendKeys("Name");

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