Unable to click on button available on the page - java

<button class="md-trigger btn btn-primary mrg-b-lg" data-toggle="modal" data-target="CaseProcessmodal" id="AddCaseButton" onclick="return validateForm('#CaseProcessmodal');">Add Case</button>
I have an element location like above, trying to click on the button in selenium, its not working. I am using page factory, element id is correctly matching though.
Verified with other matching element id.

The desired element is within a Modal Dialog so to click() on the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.md-trigger.btn.btn-primary.mrg-b-lg#AddCaseButton[data-target='CaseProcessmodal']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='md-trigger btn btn-primary mrg-b-lg' and #id='AddCaseButton'][text()='Add Case']"))).click();

I found solution to this putting if condition, agian applied click action. it worked.

Related

How to click on the button with Java Selenium

I am trying to click the button first which I did and then the dropdown menu element which is Tüm Soru Tipleri automatically in Selenium Java.
This one is didn't work:
driver.findElement(By.id("select2-question_types-sq-result-xih0--1")).click();
Could you help?
HTML snapshot:
Element snapshot:
Try adding a wait of 2 ms between button and drop-down.
Once you click and expand the dropdown further to click() on the desired <li> element with text as Tüm Soru Tipleri you need to induce WebDriverWait for the elementToBeClickable() and you can use the following locator strategy:
Using xpath and text():
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='select2-results']/ul[#id='select2-question_types-sq-results']//li[text()='Tüm Soru Tipleri']"))).click();
Using xpath and contains():
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='select2-results']/ul[#id='select2-question_types-sq-results']//li[contains(., 'Tüm Soru Tipleri')]"))).click();

How to click on a anchor Tag using selenium webDriver

I am not able to click on the button having Some visibility issues. I need to hover around this first to get the link then i need to click on the same.
<a tabindex="0"
class="cardPreviewLink expand-icon"
aria-label="card opens in new tab"
target="_blank"
id="card-preview-link-19479"
href="/card/19479?$filters#$pattern=10532372&type===&dimension=chargeback_id">
<button class="MuiButtonBase-root MuiIconButton-root" tabindex="-1" type="button">
<span class="MuiIconButton-label">
<svg class="MuiSvgIcon-root open-icon"
focusable="false"
viewBox="0 0 24 24"
aria-hidden="true"
role="presentation">
<path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z"/>
</svg>
</span>
</button>
</a>
Code trials:
WebDriverWait wait4 = new WebDriverWait(driver, 60);
wait4.until(ExpectedConditions.visibilityOfElementLocated(By.className("cardPreviewLink expand-icon")));
driver.findElement(By.className("cardPreviewLink expand-icon")).click();
Error:
Timeout Exception because of No such Element Exception
The desired element is a dynamic element so to click() on the element you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.cardPreviewLink.expand-icon > button.MuiButtonBase-root.MuiIconButton-root > span.MuiIconButton-label"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='cardPreviewLink expand-icon']/button[#class='MuiButtonBase-root MuiIconButton-root']/span[#class='MuiIconButton-label']"))).click();
You can try to click with webdriver wait for element to receive click.
By buttonBy = By.cssSelector("a.cardPreviewLink.expand-icon > button"));
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until(ExpectedConditions.elementToBeClickable(buttonBy);
If above approach will not work you can try with click using JS. Here I am just waiting for visibility of element as If element can receive click then First approach should work.
wait.until(ExpectedConditions.visibilityOfElementLocated(buttonBy);
WebElement button=driver.findElement(buttonBy);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", button);
By.className() won't work with names having spaces - cardPreviewLink expand-icon. Instead try to use cssSelector or xpath.
Xpath Example:
WebDriverWait wait4 = new WebDriverWait(driver, 60);
WebElement element = wait4.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(#class,'cardPreviewLink'][contains(#class,'expand-icon']")));
element.click();
'visibilityOfElementLocated' should work. If it didn't, as mentioned by Debanjan, try with 'elementToBeClickable'.
Also, wait.until will itself return WebElement object. You can use the same to click on it.

How to click on Log In button/link data-testid using Selenium and Java

I am attempting to click on 'Log in' but my Selenium code isn't working. Below is the HTML code.
<span class="css-14krylx-text-text-fullPageText-FormFooter">Already have a Times account?
<span tabindex="0" data-testid="switch-to-login" class="css-dip6gw-link-link-FormFooter">Log in</span></span>
What I attempted so far and did not work?
driver.findElement(By.cssSelector("[data-testid='switch-to-login'")).click();
driver.findElement(By.xpath("//span[#data-testid='switch-to-login'")).click();
driver.findElement(By.cssSelector("/.css-dip6gw-link-link-FormFooter'")).click();
Is there any other approach?
The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span[class$='text-text-fullPageText-FormFooter'] span[class$='link-link-FormFooter'][data-testid='switch-to-login']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#class, 'text-text-fullPageText-FormFooter')]//span[contains(#class, 'link-link-FormFooter') and text()='Log in']"))).click();

Unique name for the button with plus sign - want to click this button

The code is:
<button class="btn btn-default btn-xs" style="padding:0 5px" id="addPiidRelatedNumberBtn_332741000" ng-click="AddPiidRelatedNumber($event)" ng-show="EnableEditable && CanEdit">
<span class="glyphicon glyphicon-plus" title="Add New Related Number"></span>
I wrote this selenium code in STS bundle:
WaitUtility.ThreadSleep(1000);
driver.findElement(By.xpath("//button[#id='addPiidRelatedNumberBtn_332741000']")).click();
But I know the id can be changed anytime as it has some digits in it. Is there a better way to code this to click this button?
You can try something like this:
driver.findElement(By.xpath("//button[starts-with(#id,'addPiidRelatedNumberBtn_')]")).click();
You can find more info on XPath use for testing in this article:
XPath tutorial on guru99
The desired element is an Angular element so you need to induce WebDriverWait for the element to be clickable and you can use either of the following solution:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-default.btn-xs>span.glyphicon.glyphicon-plus[title='Add New Related Number']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn-default btn-xs']/span[#class='glyphicon glyphicon-plus' and #title='Add New Related Number']"))).click();
You can use angular attribute,
driver.findElement(By.xpath("//button[#ng-click='AddPiidRelatedNumber($event)']"));

How to find the button element through Selenium webdriver and xpath

snapshot
<button class="add-school btnEditDashboard btn btn-primary pull-right" ng-click="CreateNewSchool()" name="AddSchool" type="submit">Add School</button>
Tried xpath also. (\\html/body/div[2]/div/div[2]/div/div/div[1]/div/div[2]/button)
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"tag name","selector":"AddSchool"}
try with this code :
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Add School' and #name='AddSchool' and #ng-click='CreateNewSchool()']"))).click();
If it is in iframe , please switch focus of driver to respective iframe and then try to interact with it.
AddSchool is the name attribute, not the tag name (which will be button in your case). Try
findElement(By.name("AddSchool"))
To locate the element instead of using an absolute xpath it would be optimum to use a relative xpath. Now, as per the HTML you have shared the element is an Angular element so you have to induce WebDriverWait for the element to be clickable as follows:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='add-school btnEditDashboard btn btn-primary pull-right' and #name='AddSchool']"))).click();
Since the HTML is not shared, it could be 2 things
Iframe: You need to change your drivers focus to the iframe the element is in.
https://www.guru99.com/handling-iframes-selenium.html
Selenium is executing before the element is visible. Use Explicit Wait to wait for the element to be interactable before clicking it. Understand all the type of wait conditions through http://www.seleniumeasy.com/selenium-tutorials/webdriver-wait-examples
Try this xpath:
//button[#ng-click='CreateNewSchool()']

Categories

Resources