How to locate the element using Selenium and Java - 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();

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

Click an element within section tag

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

I am trying to select a checkbox in selenium but I am unable to, Why?

I have a check box in my code which I am trying to select. I have also tried the absolute xpath but it does not get selected.
The code is pasted below. Can someone please help me with it?
<label for="brandFilter*">ALL</label>
<div class="dijit dijitReset dijitInline dijitCheckBox dijitCheckBoxHover dijitHover dijitCheckBoxFocused dijitCheckBoxHoverFocused dijitHoverFocused dijitFocused" role="presentation" widgetid="brandFilter1">
<input id="brandFilter1" class="dijitReset dijitCheckBoxInput" type="checkbox" dojoattachevent="onclick:_onClick" dojoattachpoint="focusNode" name="brandFilter" style="-moz-user-select: none;" tabindex="0" aria-pressed="false"/>
</div>
<label class="dijitFocusedLabel" for="brandFilter1">Power Systems</label
The error that I get is:
org.openqa.selenium.WebDriverException: unknown error: Element is not
clickable at point (465, 669).
Other element would receive the click:
<div id="results-loading" class="dealreg-load" style="filter:alpha(opacity=90); opacity:0.9;">...</div>
It looks like there is a loading indicator that is on top of the desired element when you search for it. Wait for your desired element to become clickable:
WebDriverWait wait = WebDriverWait(driver, 10);
WebElement brandFilter = wait.until(ExpectedConditions.elementToBeClickable(By.ID("brandFilter1")));
brandFilter.click();
Or, wait for the loading indicator to disappear:
WebDriverWait wait = WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.ID("results-loading")));
WebElement brandFilter = driver.findElement(By.ID("brandFilter1"));
brandFilter.click();
What can also help:
maximizing the browser window:
driver.manage().window().maximize();
scrolling into view of the element:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", brandFilter);

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