Click an element within section tag - java

The Element is inside tag but many ways tried couldn't click on it.
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#value='Login to Register']"))).click();
and
WebElement element = driver.findElement(By.xpath("//input[#value='Login to Register']"));
JavascriptExecutor jsEx= (JavascriptExecutor)driver;
jsEx.executeScript("arguments[0].click();", element);
Here is the html:
<div id="requestRegistrationWidget">
<a id="requestLocationLogin" href="/user/login?destination=%2Fsearch%2Flocations%2Fwidget%3Fparam1%3D006046-0619_1565278200_11000000%26param2%3D3" class="use-ajax login-popup-form" data-dialog-type="modal">
<input class="btn btn-primary" style="width: 100% !important;" type="button" value="Login to Register"></input>
</a>
<!-- registerSession == 3 and registerAnyActiveSession == 1 case (2) -->
</div>

Can you give a try with the below code.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.id("requestLocationLogin"))).click();

I suppose you are facing this issue on chrome.
Try this, it works for me.
element.sendKeys(Keys.RETURN);
Its just hitting the "Enter" button on the element.

To click() on the element with text as Login to Register as the element is AJAX enabled 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.use-ajax.login-popup-form#requestLocationLogin>input.btn.btn-primary[value='Login to Register']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='use-ajax login-popup-form' and #id='requestLocationLogin']/input[#class='btn btn-primary' and #value='Login to Register']"))).click();

Related

Selenium, build XPath in java, to click on the button

I need please your help to build XPath, click on the button :
HTML is :
<div class="offer col-md gtm-data gtm-impression slick-slide" data-gtm-id="5608" data-gtm-title="230 גיגה Rolling Package" data-gtm-price="33.00" data-gtm-category="חבילות" data-gtm-list="homepage" data-gtm-position="4" data-slick-index="3" aria-hidden="true" tabindex="-1" style="width: 370px;" xpath="1">
<div class="upper">
<div class="title"><spam class="threshold">230</spam><spam class="units">GB</spam></div>
<div class="subtitle"><p>Rolling Package</p>
</div>
<!--<div class="comment"><span>test</span></div>-->
</div>
<div class="bottom">
<div class="price-area">
<div class="title"><spam class="price-number"><span class="number">33</span></spam> </div>
<div class="subtitle">
<span></span>
</div>
</div>
<div class="link">
Join Now
</div>
</div>
</div>
When I try to click on the button,
by XPath :
//div[contains(#data-gtm-id, '5608')] //a[#class='button red full gtm-click']
I got this error :
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element להצטרפות is not clickable at point (951, 858). Other element would receive the click: <p>...</p>
(Session info: chrome=96.0.4664.45)
When I try :
(//a[contains(#class,'button red')])[2]
I can click on the button,
But I want the code to be more dynamic.
The data-gtm-id attribute is a Google Tag Manager attribute and the value of data-gtm-id attribute i.e. 5608 is dynamically generated. Everytime you access the application it would get changed. So you won't be able to locate the element using the attribute data-gtm-id
To click() on the element with text as Join Now you can use either of the following Locator Strategies:
linkText:
driver.findElement(By.linkText("Join Now")).click();
cssSelector:
driver.findElement(By.cssSelector("a.button.red.full.gtm-click[title='Join Now']")).click();
xpath:
driver.findElement(By.xpath("//a[#class='button red full gtm-click' and text()='Join Now']")).click();
However, as the element is a dynamic element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Google"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.button.red.full.gtm-click[title='Join Now']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='button red full gtm-click' and text()='Join Now']"))).click();
I hope your element is right but it is clicking before it is loaded, so please add wait condition.
By joinNowBtn = By.xpath("//a[contains(text(),'Join Now')]");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(joinNowBtn));
wait.until(ExpectedConditions.visibilityOfElementLocated(joinNowBtn));
driver.findElement(joinNowBtn).click();

Selenium: Click on tag by title using XPath

Need some help on Xpath for following code:
<span class-"metadata-row float-left" style="width: 1.9vw;"> &absp; </span>
<input placeholder="New Course Name" id="newCourseName" type="text” class="metadata-name metadata-name-edit font-12" autofocus>
<span class="fa fa-check metadata-action-icon" title="Save" onclick="addCourse(this)" style="display: block;"> ... </span>
I want to click on "Save" which is mentioned as title in the code but in ui it is showing as icon.
To click() on the element with title attribute as Save you need to use elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.fa.fa-check.metadata-action-icon[title='Save']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='fa fa-check metadata-action-icon' and #title='Save']"))).click();
try javascript executor
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

How to locate the element using Selenium and Java

<input class="chkbx-selection ng-pristine ng-untouched ng-valid" type="checkbox" value="test" id="isAgreeChkBox" ng-model="$root.isAgreement">
Please help me find what is the xpath/css selector here? I need to locate this using locators in selenide
The desired element is an Angular element so you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.chkbx-selection.ng-pristine.ng-untouched.ng-valid#isAgreeChkBox[value='test']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='chkbx-selection ng-pristine ng-untouched ng-valid' and #id='isAgreeChkBox'][#value='test']"))).click();

Selenium: Button in modal content is not clickable

I want to Click the "OK" button during a Selenium test but the Element is not visible.
driver.findElement(By.xpath("//*[#id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]")).click();
<div class="bootstrap-dialog-footer-buttons">
<button class="btn btn-default" id="5a4bb849-7a61-4603-9ef2-f9e0ecab4523">
<span class="glyphicon glyphicon-ban-circle"></span> Cancel
</button>
<button class="btn btn-warning" id="f7f4b18b-2ba2-4c1e-b541-a254c080f398">
<span class="glyphicon glyphicon-ok"></span> Ok
</button>
</div>
I think in your DOM, the button id is changing dynamically. Whenever page reload it will generating new id. There is different button id you used in your Selenium code and HTML. So, I suggest you go with className. Try below code and hope it works for you.
//If the Element is not visible then wait until that element is not visible
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.className("btn btn-warning")));
//If the element is visible but not Clickable then wait until that element get Clickable.
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.className("btn btn-warning")));
//Then simply click the button
driver.findElement(By.className("btn btn-warning")).click();
Use JavascriptExecutor to click the element,
Refer code,
WebElement element = driver.findElement(By.xpath("//*[#id=\"5f6e7b16-0fa1-4db6-869b-3a6ba6b0fafe\"]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
As per the HTML you have shared it seems that the desired element is within a Bootstrap Modal Dialog Box and the id attribute of the element is dynamic. So to invoke click() you have to induce WebDriverWait as follows :
cssSelector :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.bootstrap-dialog-footer-buttons button.btn.btn-warning"))).click();
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='bootstrap-dialog-footer-buttons']//button[#class='btn btn-warning']"))).click();

Toggle an element using selenium webdriver

I have a toggle element in a webpage.
Using selenium i have to toggle right .Am not sure how it can be done using selenium
Actualy I need to click the following element to toggle
<div class="right">
<input id="app_in" class="cmn-toggle cmn-toggle-round" type="checkbox" value="false">
<label class="preference" tabindex="2" data-preference="inFlag" data-guid="26865MS" for="app_in"></label>
</div>
i tried following code to click the checkbox but getting "Element is not currently visible and so may not be interacted with" error
driver.findElement(By.id("app_in")).click();
One possible solution here could be to wait for the element to become visible:
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement element wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("app_in")));
element.click();
If it does not help, try clicking the element through javascript:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);

Categories