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);
Related
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();
I am having an issue in regards to a md-dialog pop-up in an angularJS app that I am unable to click in selenium. When i click a button the dialog box appears and becomes the active element on the screen, darkening the background. I have tried switchTo with active element, frame, alert and none of these seem to work. My most recent attempt was trying to swap windows using the below code:
winHandleBefore = driver.getWindowHandle();
Set<String> numOfWindows = driver.getWindowHandles();
System.out.print(numOfWindows.size());
for(String winhandle : driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
report.updateTestLog("Switched to window", "", Status.PASS);
}
the S.o.p for the size is always outputted as 1. There is a wait in after the initial button is clicked before the popup appears to assure it has the proper time to appear. Not sure what else there is to do, I have been scouring the internet for an answer and I haven't come across anything that will let me click the elements in that popup
Edit: This is the html for the md-dialog
<md-dialog class="quote _md md-transition-in" aria-label="Summary" role="dialog" tabindex="-1" id="dialogContent_78" aria-describedby="dialogContent_78" style="">
Edit 2: Forgot to say, I am trying to click a button inside the md-dialog popup
Edit 3: After reviewing the code a bit more I noticed that the md-dialog popup has a container div that is taking up the entire screen and that it is also calling in html from another file. The container div html:<div class="md-dialog-container ng-scope" tabindex="-1" style="top: 972px; height: 769px;">
In case someone has same issue the pop-up was registering as hidden even though it had appeared on screen, so any attempt to click a button was futile as the button was seen as hidden. The workaround was to find the button using the findElement() method, assign it to a variable and then use a javascriptExectutor to click it even though it was hidden. This is not ideal if you are trying to reproduce user input but it is a workaround. Code is below.
WebElement hiddenButton = driver.findElement(uniqueIdentifier);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", hiddenButton);
report.updateTestLog(flowName, uniqueIdentifier + " Hidden Button Pressed", Status.DONE);
Hope this helps anyone who finds it.
Following is the HTML for span:
<td class="header-logout-btn">
<a href="logout.htm" class="btn switch-btn">
<i class="fa fa-times"></i><span class="hidden-xs">Home</span>
</a>
</td>
Tried with:
driver.findElement(By.linkText("HOME"));
driver.findElement(By.xpath("//div[#class="header-logout-btn"]/span[#class="hidden-xs"]));
driver.findElement(By.xpath("//span"));
driver.findElement(By.className("hidden-xs"));
Try following solution :
Xpath= //*[contains(text(),'Home')]
Hope it will help you.
Normally the logout button will be submenu or it will be visible only if you click or mouse over on main menu usually the main menu is profile icon. I assume this logout button visible only after clicking on main/profile button. This may be the reason the button is hidden or not clickable. The solutions are given below.
1. First click or mouse over on main/profile menu then click on logout button.
2. Still you want to click on hidden button. You may be try with JavaScript executor as given below.
Webelement eleLogout=driver.find element(By.class name("switch-btn"));
JavaScriptExecutor js=(JavaScriptExecutor)driver;
js.executeScript("arguments [0].click()",eleLogout);`
Try clicking on the link.
Use selectors like:
xpath:
//a[contains(#href, 'logout')] or
//*[#class='header-logout-btn']/a[contains(#href, 'logout')]
css:
a[href*=logout] or
.header-logout-btn a[href*=logout]
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();
I have to click on customised checkbox, it's inside a div like the code following:
<div class="checkbox input-group">
<input id="checkboxH" class="checkboxClass" type="checkbox" name="checkboxH">
<label class="required ng-scope" for="checkboxH">
Text of checkbox
</label>
</div>
I use label as element to be clicked, and it works on FF as code below
webDriver.findElement(By.xpath("//label[#for='checkboxH']")).click();
but it doesn't work on Chrome (latest version) and IE11, the error is:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (634, 498). Other element would receive the click:
Note that the checkbox is not visible on the screen, it has to be scrolled into view. But with the same code, it works with FF. FF can click on checkbox even when it is not in the view.
I tried another way, like this:
WebElement a = webDriver.findElement(By.xpath("//label[#for='checkboxH']"));
a.sendKeys(Keys.ENTER);
a.click();
And it also works with FF, but not Chrome and IE. It seems, selenium with FF automatically scrolls element into view and performs click on it
I am stuck here with Chrome and IE. Any help much appreciated. Thanks
EDIT
Apparently, I fixed issue with IE by setting this capability, and it works for IE:
capability.setCapability("nativeEvents", false);
With Chrome, I don't know which capacities could fix this issue
I have a work around: I use Actions to move to element, then I use sleep for 2 second, then it works.