Unable to Click button in Selenium - java

Im having difficulties in locating an element which is a finish button within a page. I have used driver.findElementById("finish").click (); and it does not work.
Below are different examples I used but with no success:
for (WebElement Element : driver.findElement(By.id("finish")).findElements(By.tagName("a"))) {
if (Element.getAttribute ("class").contains("criteria-filter")) { Element.click();
break;
}
----------------------------------------------------------------
WebElement click1 = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"finish\"]")));
((JavascriptExecutor) driver).executeScript("arguments[0].click();",click1);
----------------------------------------------------------------
if(!driver.findElementById("finish").isEnabled())
{
driver.findElementById("finish").click();
}else{
System.out.println("False");
}
Element:
<a id="finish" access="" allof="PA.DEPLOYMENT_CONFIG" class="btn criteria-filter btn-success" href="" ng-click="verifyAllFields(sftpForm) && sftpForm.$valid && create()">Finish
</a>

Class selector is not the best idea as it will fail if there is more elements with the same class.
The best way is to open dev-tools, click ctrl + f, then try to cath your element by xpath like this:
//a[text()='Finish']
When you "cath" this browser will move to that element.

Related

Selenium / Java : Unable to locate element on a ion search popup

We are trying to automate a flow and it require a click + sign on a below popup window.
We have tried xpath locator but element is not getting located.
and below we get below error:
error : org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <ion-icon color="gray-900" name="ios-add-circle-outline" role="img" class="icon icon-ios icon-ios-gray-900 ion-ios-add-circle-outline" aria-label="add circle-outline"></ion-icon> is not clickable at point (1135, 195). Other element would receive the click: <span class="button-inner">...</span>
Element locator code is
We are using following code in our step definition for it:
Element locator:
#FindBy (xpath = "//*[#name='ios-add-circle-outline']")
private WebElement plusIcon;
To click below code is being used by us.
method ()
> String currentWindow = driver.getWindowHandle();
> driver.switchTo().window(currentWindow);
> Thread.sleep(3000);
> plusIcon.click();
Action class used to overcome above issue.
> Actions builder = new Actions(driver);
> builder.moveToElement(plusIcon);
> builder.click(); builder.perform();

Not Able to click in Radio Button - Selenium

Tried to select from Radio Button, Check on Console the Element is there and its not under Iframe:
$x("//*[#id='featuresCollapse1']/div[1]/label/span")
(2) [span.overlay, span.overlay]
HTML:
<div class="col-6 my-1 feature-input-wrapper text-left">
<input name="radio0" data-index="1" id="Personal Accident for Driver only" hidden="" type="radio"><label data-code="PA-DO" data-value="60" class="radio-inline custom-component featureInput p-0 radio-no form-group">
<span class="overlay" tabindex="0"></span>
<label data-value="60" class="feature-name">Personal Accident for Driver only<span class="option-price font-weight-bold ml-2"> 60 </span> </label></label></div>
the Xpath of the radio is : private final By PAforDriverOnly = By.xpath("//*[#id='featuresCollapse1']/div[1]/label/span");
Tried here to check if it Display or not .. but its not " Reading the Exception message
try {
driver.findElement(PAforDriverOnly).isDisplayed();
System.out.println("driver.findElement(PersonalAccindent).isDisplayed();");
String Persional = driver.findElement(PAforDriverOnly).getText();
System.out.println(Persional);
driver.findElement(PAforDriverOnly).click();
System.out.println("driver.findElement(PersonalAccindent).click();");
//getTextAct(ProceedToCheckoutBtn);
} catch (Exception e){
System.out.println("The Exception Message of Verify Button : "+e);
}
and here tried to use JavaScriptExcutor, but Failed on WebDriverWait,
once to remove it, its reading the JavaExcutor but not Give any Action :
WebElement myElement = driver.findElement(PAforDriverOnly);
new WebDriverWait(driver, 5).until(ExpectedConditions.visibilityOfElementLocated(PAforDriverOnly));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", myElement );
System.out.println("JavaScriptExcutor - Click on RadioButton");
Thread.sleep(2000);
Btnclick(PAforDriverOnly);
I hope your xpath is wrong,
because your xpath is pointing to label. try to point it to input tag.
use this //input[#id='Personal Accident for Driver only'] or use it on your way to find the element but you need to drill down to input tag.

java selenium cant find iframe path

i'm trying to get into iframe, but i get error like this:
Find element :By.id: fullscreen-app-host
Find element :By.id: fullscreen-app-host
Get Exception: no such element: Unable to locate element: {"method":"css selector","selector":"#fullscreen\-app\-host"}
and here is iframe css:
<iframe class="publishedAppIframe" tabindex="0" aria-hidden="false" id="fullscreen-app-host" scrolling="no" frameborder="0" name="fullscreen-app-host" allow="geolocation; microphone; camera; fullscreen" style="" title="holidays" cd_frame_id_="6a1f85c52ae"></iframe>
i have tryied many different ways, but result always almost the same - can't find element into iframe.
here i paste link to code of methods i use:
https://www.codepile.net/pile/7eygVLY3
your code:
public void selectIframe() {
String parent = driver.getWindowHandle();
for (String childHandle : driver.getWindowHandles()) {
if (!childHandle.equals(parent)) {
driver.switchTo().frame(childHandle);
}
}
}
Problem:
Iframes are not window , its an element you cannot use :
driver.switchTo().frame(childHandle);
if you want to switch to window then use:
driver.switchTo().window(childHandle);
if you want to switch iframe , then find that iframe and then switch to it
eg :
frame = driver.findElement(By.TAGNAME("iframe"));
driver.switchTo().frame(frame);

Can't click the button - element not visible

I'm using selenium and chrome drive and i can't click the button,
I tried different approaches but nothing:
link = driver.findElement(By.xpath("//button[#class='btn buttonContainer arrow']"));
link = driver.findElement(By.xpath("//input[#class='btn buttonContainer arrow']"));
link = driver.findElement(By.cssSelector("input[type='button']"));
link = driver.findElement(By.cssSelector("input[type='button'][value='btn buttonContainer arrow']"));
error massage:
INFO: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible
(Session info: chrome=58.0.3029.96)
(Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.10240 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 15 milliseconds
code:
<a class="ng-scope" ng-if="!loginCtrl.pageObject.isDebugMode" ng-click="rootCtrl.redirectToRegistration()">
<button class="btn buttonContainer arrow">
<span>Enter</span>
</button>
</a>
thanks Amir
Incase you want to click on Enter button try: driver.findElement(By.xpath("//button[#class='btn buttonContainer arrow']/span"));
I didnt reproduce it but you should be able to get it to work like this:
firefoxDriver.findElement(By.className("buttonContainer")).click();
This approach is working for me using Selenium V2.52.0
Use some implicit wait default in your script to locate the element by selenium before throwing the exception
Like
driver.get("your _site_url");
driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
Use explicit wait until your element get visible and then perform click
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[#class='btn buttonContainer arrow']")));
driver.findElement(By.xpath("//button[#class='btn buttonContainer arrow']")).click();
You are automating a web-based application built using AngularJS.
In such a case, your click() events should be targeted to the web elements which have ng-click attribute.
Try clicking on the anchor link instead of the button.
link = driver.findElement(By.xpath("//button[#class='btn buttonContainer arrow']/.."));
link.click();
Looks like there are multiple web elements in the DOM with same xpath and the first element is not visible although Firebug would highlight visible element (It typically happens with Responsive websites)
You can try following:
public WebElement getDisplayedElement( WebDriver driver ) {
try {
WebElement visibleElement = null;
List<WebElement> elements = driver.findElements(locator);
for (WebElement element : elements) {
boolean isDisplayed = false;
boolean isEnabled = false;
System.out.println("Count of element(s) from locator " +locator+ " is " +elements.size());
isDisplayed = element.isDisplayed();
isEnabled = element.isEnabled();
if(isDisplayed && isEnabled ) {
System.out.println("Found enabled and displayed web element");
visibleElement = element;
break;
}
}
return visibleElement;
}

navigate between pages with selenium - Java

I walk a list of links , I click on them one by one , I go to the page the link and realize the actions that need to perform and then return to the list to click on the next link, it is working perfectly.
What I need now is to come to the end of the links , where the loop ends , the selenium click the forward button to go to the next page and be done again the link count of this page and start the cycle again.
I can not make the selenium click the move because it says that the click(); command You can not be using in a webelento .
The method click () is undefined for the type List < WebElement >
This is the HTML structure:
<div id="results-pagination">
<h2 id="pagination-heading">Pagination</h2>
<ul class="pagination">
<li class="prev">
<a class="page-link" href="url" title="back" data-li-page="1">< back</a>
</li>
<li class="link">
<a class="page-link" href="url" title="page 2" data-li-page="2">2</a>
</li>
<li class="next">
<a class="page-link" href="next" title="next" data-li-page="next"></a>
</li>
</ul>
</div>
selenium code:
List<org.openqa.selenium.WebElement> numberpages= driver.findElements(By.className("page-link"));
System.out.println("numberpages : " + numerospaginas.size());
List<org.openqa.selenium.WebElement> links= driver.findElements(By.linkText("to connect"));
System.out.println("Count to connect : " + links.size());
Thread.sleep(2000);
for(int i=0;i<5;i++){
links= driver.findElements(By.linkText("to connect"));
links.get(i).click();
Thread.sleep(2000);
boolean convite = driver.getPageSource().contains("iweReconnectSubmit");
if(invite == true){
Thread.sleep(2000);
boolean error = driver.getPageSource().contains("message:");
do{
//action
By tipoPlano = By.cssSelector("[name='reason'][value='IF'][type='radio']");
driver.findElement(tipoPlano).click();
}while(error == true);
//submit
driver.findElement(By.name("iweReconnectSubmit")).click();
Thread.sleep(2000);
WebDriverWait confirmacaoadicao = new WebDriverWait(driver, 10);
confirmacaoadicao.until(ExpectedConditions.textToBePresentInElement(By.id("control_gen_3"), "invite for: "));
String pessoa = driver.findElement(By.xpath("//div[#id='control_gen_3']//a")).getText();
System.out.println(pessoa + " add" );
driver.navigate().to(list_of_links);
WebDriverWait retorno = new WebDriverWait(driver, 10);
retorno.until(ExpectedConditions.elementToBeClickable(By.linkText("To connect")));
}
}
//does not work
driver.findElements(By.linkText("next")).click();
//does not work
((org.openqa.selenium.WebElement)driver.findElements(By.linkText("next"))).click();
your click function is not coming because driver.findElements(By.linkText("next")) returns a list List<WebElement> and click() cant be called on a list object .
you can call click method my iterating over the list :
List<WebElement> WebElementList = driver.findElements(By.linkText("next"));
for(WebElement element : WebElementList){
element.click(); // click can be called on object of WebElement
}
I modified the code to iterate through google search results pages and to get the results' URLs.
public static void searchGoogle(String query) throws InterruptedException {
try {
System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.co.uk");
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("\"" + query + "\" filetype:pdf\n");
element.submit();
// wait until the google page shows the result
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));
getResults(driver);
Thread.sleep(1000);
for (int i = 0; i < 10; i++) {
driver.findElement(By.linkText("Next")).click();
Thread.sleep(1000);
getResults(driver);
}
} catch (Exception e) {
System.err.println("Error caught - " + e);
}
}
public static void getResults(WebDriver driver) {
List<WebElement> findElements = null;
findElements = driver.findElements(By.xpath("//*[#id='rso']//h3/a"));
for (WebElement webElement : findElements) {
System.out.println(webElement.getAttribute("href"));
}
}
It should be driver.findElement(By.linkText("next")).click();. driver.findElements returns List<WebElement> while driver.findElement returns single WebElement.
Also, it seems the button doesn't have next text. Try looking by class
driver.findElement(By.className("next")).click();
next text will look like
<a class="page-link" href="next" title="next" data-li-page="next">"next"</a>
with next before the <a> closing tag.

Categories

Resources