Toggle an element using selenium webdriver - java

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

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

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

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

how to select drop down option and should click on the following values within drop down list in selenium webdriver java code

here is code html code. here using java selenium code i need to click on logout option to close the session. when clicked on drop down button, logout option will appear and i need to click on that link
<div class="sp-info">
abc
<i class="fa fa-angle-down" aria-hidden="true"></i>
</div>
</a>
<ul class="main-menu" style="display: block;">
<li>
<a href="profile.php">
</li>
<li>
<a href="change_password.php">
</li>
<li>
<a href="logout.php">
here is java selenium code.
driver.get(baseUrl + "owner/login.php");
driver.findElement(By.xpath("//input[#name='admin_user_name']")).sendKeys("qwerty");
driver.findElement(By.name("admin_password")).clear();
driver.findElement(By.xpath("//input[#name='admin_password']")).sendKeys("12345678");
driver.findElement(By.xpath("//button[#type='submit']")).click();
driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
WebElement wb = driver.findElement(By.xpath("//div[contains(#class,'sp-info')]"));
Actions mouse = new Actions(driver);
mouse.moveToElement(wb).click();
WebElement wb1 = driver.findElement(By.xpath("//a[contains(#href,'logout.php')]"));
mouse.moveToElement(wb1).click();
Use XPath for getting the path for the element.
you can get the XPath using browser.
For Example:
element = findElement(By.xpath("//*[#test-id='logout.php']");
You can try using actions to click on element in the drop dow.
driver.manage().timeouts().implicitlyWait(10 , TimeUnit.SECONDS);
wb = driver.findElement(<xpath_to_go_to_menu_dropdown>);
Actions mouse = new Actions(driver);
mouse.moveToElement(wb).click();
WebElement wb1 = driver.findElement(<xpath_to_go_value_in_dropdown>);
mouse.moveToElement(wb1).click();
mouse.build();
mouse.perform();
Based on your comments it seems you're being told the element is not visible.
Have you tried using explicit wait? So that when you open the menu that contains the Logout option you give it a chance to appear, e.g.:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(#href,'logout.php')]")));
Hopefully then it should find it, and you'll be ok to interact with it.

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

Categories