window.showModalDialog not appear when running with selenium - java

In java web application there's a line of code which opens a popup window whenever clicking on a link and this window has ok/cancle button:
return window.showModalDialog("popupWindow", obj, sFeatures);
I use below code in selenium to click on the link
geckoDriver.findElement(By.xpath(".//a[contains(#onclick, 'return openlink(8251')]")).click();
I'm sure the link is clicked successfully but the window.showModalDialog does not open and I can not go on because of that.
What is the problem?
Note that I use gecko driver and I also test my application with chromeDriver and ieDriver with no success
Html tag:
<td width="80" aria-describedby="grdOpeningTrustCartable_" title="openning" style="text-align:center;" role="gridcell">
<a class="gridHighlight" onclick="return openLink(8251,'04/12/17 15:50:00')" href="javascript:void(0);">openning</a>
</td>

You can use JavaScript Exceutor to overcome this problem; given that your click is a working but not resulting into any action:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return openLink(8251,'04/12/17 15:50:00');");

Related

Button not working as expected in headless mode

I am using the latest chrome driver with selenium to test a web application hosted on Microsoft Azure.
The script starts by logging into the web application. An authentication window opens that requires the user to login through Azure and then press a, "Grant" button that will allow the web application to speak to a Therefore database to populate a few metadata fields.
This all works perfectly when headless mode is disabled. However, when headless mode is enabled it seems as though the, "Grant" button doesn't function. I am logging and taking screenshots during this process, which is how I know that the "Grant" button element is found and is being clicked. The button becomes highlighted when clicked, which is shown in the screenshot, but nothing happens and the authentication window times out in headless which kills the test.
I have tried different clicking methods, but this made no difference as the element is found and is being clicked. I have also pressed the, "Sign in as a different user" button on the form to ensure that the .click() method is functioning as expected, which of course works. I tried to add longer wait times but to no avail.
I have also added the following Chromium driver options:
ChromeOptions options = new ChromeOptions();
options.addArguments("enable-automation");
options.addArguments("--headless");
options.addArguments("--start-maximized");
options.addArguments("--window-size=1920,1080");
options.addArguments("--no-sandbox");
options.addArguments("--disable-extensions");
options.addArguments("--dns-prefetch-disable");
options.addArguments("--disable-gpu");
options.addArguments("--incognito");
options.addArguments("--disable-web-security");
options.addArguments("--allow-running-insecure-content");
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--allow-insecure-localhost");
options.addArguments("--disable-popup-blocking");
options.setPageLoadStrategy(PageLoadStrategy.EAGER);
How I am clicking the element:
System.out.println("Grant permission...");
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Grant')]"))).click();
What baffles me is how this works seamlessly when headless is disabled but not when it's enabled. I'm wondering if this could be a Chrome driver issue? However I know that this is unlikely.
Any recommendations are appreciated, thanks.
Adding button HTML as requested:
<form method="POST">
<p>Hello, Test</p>
<ul>
<li class="text-left">Act with your access permissions</li>
<li class="text-left">Allow continuous access while you are not online.</li>
</ul>
<p>
<button type="submit" name="submit.Grant" value="Grant" class="btn btn-primary btn-block">Grant</button>
<button type="submit" name="submit.Login" value="Sign in as different user" class="btn btn-outline-primary btn-block">Sign in as different user</button>
</p>
Try adding a wait. I mean use WebDriverWait to wait for the element to be clickable.
Something like:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/div/div/div/div/div/div/div[2]/form/p[3]/button[1]"))).click();
Also you need to improve your locator.
Absolute XPaths are extremely fragile.
First, try to avoid very long CSS or Xpath expression. You can find the button you need to click on like this:
driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
This code is more readable. If the site will be changed and the element will be moved to another div or span - your code will not work if it relays on the structure of all the divs and spans.
Next, never just click on an element of hover over it in a Selenium test. First check if the element is clickable, then click:
WebElement term = driver.findElement(By.xpath("//button[contains(text(),'Grant')]"));
WebDriverWait wait = new WebDriverWait(webDriver, 20);
wait.until(ExpectedConditions.elementToBeClickable(term));
term.click();

Selenium Webdriver - unable to click on button

HTML code:
<div class="buttonBG">
<input type="button" onclick="window.location.href='?mod=eA&act=00001';" class="buttonGreen" value="TK">
<input type="button" onclick="ttoggle('carianDiv');" class="buttonGreen" value="CK">
</div>
Below is my java code, when I try with below code. Can I know whats wrong is in my selenium webdriver code:
driver.findElement(By.xpath("//input[#class='buttonGreen' and contains(#onclick, 'window.location.href='?mod=eA&act=00001')]")).click();
Try to search by value
driver.findElement(By.cssSelector("[value='TK']")).click();
And for what's wrong, you are searching for ?mod=eA&act=00001 when in the html its
?mod=eA&act=00001
Edit
Another solution is to insert the buttons to list and click by index:
List<WebElement> buttons = driver.findElements(By.className("buttonGreen"));
buttons.get(0).click();
You can also try using explicit wait
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[value='TK']")).click();
This will wait up to 10 seconds for the button to be visible before clicking on it. You can change the selector or time span.
Try using XPath instead of CSS
driver.find_element(By.XPATH, '//input[#onclick=\'window.location.href=\'?mod=eA&act=00001\';\']').click()
Edit
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
NOTE: After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();

InternetExplorer Webdriver with nativeEvents

I have a menu hidden in a button, when the button is clicked, then the menu is shown, the structure of hidden menu is follow:
<button id="buttonID"></button>
<ul class="ulClass">
<li>
<li>
<li>
<li>
</ul>
I want to click on second item, so I did:
webDriver.findElement(By.xpath("//ul[#class='ulClass']/li[2]")).click();
It works fine with FF and Chrome, but doesn't work with IE, the reason because I gave the nativeEvents to false to IE:
capabilities.setCapability("nativeEvents", false);
This capabilities is set for the whole test with IE, without it, the whole test won't work, and now I just need to click on this item but can't because of this capabilities.
Is there any workaround to click on this item but I still skip this capability, because apparently we can't toggle capabilities in run time. Thanks.
I'd try JavaScript instead native clicks
var element=webDriver.findElement(By.xpath("//ul[#class='ulClass']/li[2]"));
Driver.ExecuteJavaScript("arguments[0].click();",element);
Thanks for all suggestions, I solved this with JavascriptExecutor:
JavascriptExecutor js = (JavascriptExecutor)webDriver;
js.executeScript("arguments[0].click();", element);

How to interact with dropdown having html tag as input instead of select in selenium webdriver

I've dropdown control in the web application having following html code
<input class="dynamic-list-widget-input ui-widget ui-widget-content" title="" autocomplete="off" aria-invalid="false">
I've tried accessing using selenium webdriver through xpath(relative/absolute), cssSelector but no to avail i got following exception
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[#id='With-Attachment']/div[2]/div[2]/div[5]/div[1]/span/input"}
Command duration or timeout: 10.12 seconds
It looks very likely you get the wrong xpath for that input element.
if you are using Chrome, you can do as follows:
right click on the element in the webpage.
click "inspect element" in pop up menu
right click the highlighted html code on the right, the then click "copy xpath"
you will get the xpath of that element and compare what it is in your code.

How to create a li tag dynamically using selenium webdriver on java

Here is my ol tag
<ol>
<li class="dd-item" ><div class="dd-handle"><img alt="testing" src="test2.png" s><a name="tree" style="margin:5px;">page1</a></div></li>
<li class="dd-item" ><div class="dd-handle"><img alt="testing" src="test2.png" s><a name="tree" style="margin:5px;">page2</a></div></li>
</ol>
I want to insert this below tag to above ol tag as 3rd element using selenium webdriver in java
<li class="dd-item" ><div class="dd-handle"><img alt="testing" src="test2.png" s><a name="tree" style="margin:5px;">page3</a></div></li>
How can i do it?
Webdriver is designed for browser automation, not for changing server side code or HTML returned by the server. However, if you want to change the HTML temporarily on the client side, you'll have to do what everyone else does and run some JavaScript on the browser.
As the Selenium FAQ states, you can execute JavaScript with a WebDriver instance by casting it into a JavascriptExecutor:
WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.title");
Then you can use JavaScript to manipulate the DOM inside the page being shown on the browser that your WebDriver instance is currently driving.

Categories

Resources