Button not working as expected in headless mode - java

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

Related

window.showModalDialog not appear when running with selenium

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

how to find xpath in selenium webdriver

Following is the HTML of the page, How do i get the Xpath or is there anyother way to automate using java?In fact, we shud click on this "continue" button.
<regform-button>
<button ng-disabled="activityIndicator" ng-click="validate()" type="button">
<div template="api-loader" ng-http-loader="">
<div class="http-loader__wrapper ng-hide" ng-show="showLoader" ng-include="template">
<span class="api-loader"></span>
</div>
</div>
<ng-transclude>
</button>
</regform-button>
If you are using Google Chrome, right click on the button, and select Inspect in the popup. The html will open in a developer tools frame. Right click on the element in the developer tools frame, hover over copy, select copy xpath.
Here is the XPath to the form on the URL.
//*[#id="main-frame"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button
WebDriver driver = new ChromeDriver();
driver.get("https://uk.match.com/unlogged/landing/2016/06/02/hpv-belowthefold-3steps-geo-psc-bowling?klid=6740");
//fill in fields
WebElement element = driver.findElement(By.xpath("//*[#id=\"main-frame\"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button"));
element.click();
driver.findElement(By.xpath("//regform-button/button")).click();
Try opening the console for Google Chrome and typing the following:
document.evaluate("//regform-button/button", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click()
It works...that's if you've specified the required fields. Also be sure to wait for a while (2 secs to be sure) after specifying the required fields as there might be an additional load after setting the required fields.
Edit: The code is javascript..I just took the xpath given by the previous answers and used that to locate the element through js. I'm saying that "//regform-button/button" should work..
Once you have selected the required values from the drop-downs appearing before Continue button, you can click on Continue button using the following statement:
driver.findElement(By.xpath("//span[text()='Continue']")).click();
The following xpath worked for me.But I dont know, why is it different for each different browser.
For Firefox :
`driver.findElement(By.xpath("//*[#id='main-frame']/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button")).click()`;
For chrome: driver.findElement(By.xpath("//regform-button/button")).click();

How to use Button class In Selenium Webdriver by the help of Java

Here is my code:
<div class="meal_buton_cont">
<button class="pink_button" data-target="#myModal" data-toggle="modal">Add Selected to Shopping List</button>
<button id="add_meal" class="gray_button">+ Add Meal</button>
</div>
I am using this:
driver.findElement(By.className("pink_button")).click();
But no popup open but some time open but not item take, it show blank.
How to create script for clicking Pink_button then my popup will be open?
Please help me
Few things to consider..Wait before the element appears using explicit waits like WebDriverWait API. Once the element is clickable using ExpectedConditions.elementToBeclickable(WebElement) then click on it and again wait till the pop-up appears. if its alert then use ExpectedConditions.alsertIsPresent() with WebDriverWait

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

Click on element does not produce expected outcome using selenium webdriver

First the specs, I am writing Selenium Webdriver tests in java for an application written largely in ExtJS and running on the firefox browser v14. The interesting thing is that Selenium can find the element that I want to click, but the click does not seem to be executed or if it is getting executed the desired outcome (a popup appearing) does not happen. I have also verified in Selenium IDE that the element I am looking for (a span element that I locate via Xpath) exists, but in Selenium IDE I run into the same issue of not being able to click on it.
If I manually click on the button the popup window appears asking for what file I want to upload. I have also tried other elements such as the span's parent 'button' element and parent 'em' element and parent 'div' element all with no luck.
What kills me is that I have been writing Selenium Tests for this application for weeks now and always been able to use this method to click on buttons and for this particular button it no longer works.
WebElement uploadButton = driver.findElement(By.xpath("//span[contains(text(), 'Upload Popup')]"));
uploadButton.click();
Edit 1: Thought that the code of the button itself might help
<button id="filefield-1158-buttonEl-btnEl" class="x-btn-center" autocomplete="off" role="button" hidefocus="true" type="button" style="background-color: transparent;">
Note the id is the dynamic id created by ExtJS
It can be because of lots of reasons. I would suggest that you debug while your test is running.
It is possible to install FireBug in your Selenium Firefox instance:
File file = new File("firebug-1.8.1.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); // Avoid startup screen
WebDriver driver = new FirefoxDriver(firefoxProfile);
I assume you can launch your test via JUnit (in an IDE like Eclipse). Launch your test in debug mode and set a breakpoint just before clicking on the button. Inspect the html code via FireBug then. It might give you a start.
Another posibility is to select the button by css class (By.className) or selector (By.cssSelector):
WebElement uploadButton = driver.findElement(By.className("x-btn-center"));
uploadButton.click();
If there are multiple buttons on the page you would have to use
List<WebElement> buttons = driver.findElements(By.className("x-btn-center"));
WebElement uploadButton = buttons.get(index);
uploadButton.click();
Try this out:
driver.findElements(By.xpath("//button[#class='x-btn-center']").click()
or
driver.findElements(By.xpath("//*[#class='x-btn-center']").click()

Categories

Resources