waitForElementToDisappear for all the panels of the page - selenium

By panelCollapseSelector = By.cssSelector(".panel-heading+.panel-collapse");
waitForElementToDisappear(panelCollapseSelector);
The above code waits for the 1st panel of the page. I would like to wait for all the panels to collapse. How can I run loop around this.to wait for all the panels of the page.
code before collapse
<div class="panel-collapse collapse in" style="height: auto;">
code after collapse
<div class="panel-collapse collapse" style="height: 0px;">

A simple solution would be to wait for 0 elements matching the non collapsed panels.
One possible CSS selector to get all the non collapsed panels:
".panel-heading:not(.in) + .panel-collapse:not(.in)"
Then to wait with a lambda expression :
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until((WebDriver drv) -> drv.findElements(By.cssSelector(
".panel-heading:not(.in) + .panel-collapse:not(.in)")).size() == 0);
Or using the expected conditions:
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.not(ExpectedConditions.presenceOfAllElementsLocatedBy(
By.cssSelector(".panel-heading:not(.in) + .panel-collapse:not(.in)"))));

You can try this java method for waiting 60 seconds for all panels to get collapse
int timeout=60;
while(driver.findElement(By.cssSelector(".panel-heading+.panel-collapse")).getAttribute("style").contains("height: auto;") && timeout > 0) {
try {
if(timeout>1)
{
timeout--;
System.out.println("====== Waiting for Collapsing Panels ======");
Thread.sleep(1000);
}
}
catch(Exception e) {
}

Related

Perform continuous mouse hover with Canvas?

[https://i.stack.imgur.com/yKOOF.png]I have to click one of options from left widget (See the Image1.PNG) will display single row as shown in ( See the [https://i.stack.imgur.com/hBNTN.png]Image.PNG) and then mousehover on each and every value and have to capture the tooltip for each values of that particular row..
xpath for the left and right widgets are:
#FindBy(xpath="//*[#id='tabZoneId21']/div/div/div/div[1]/div[5]/div[1]/canvas")
WebElement wbcanvasWidgetLeft;
#FindBy(xpath="//*[#id=\"view5159778620503418402_17231458509407196328\"]/div[1]/div[2]/canvas[2]")
WebElement wbcanvasWidgetRight;
And when I mouse hover on each and every value the following code gets highlights and only the value of ‘left’ gets change otherwise its static for all options.
[![<div class = “ tab-tooltip tab-widget tab-tooltipAR” style=”user-select: none; -webkit-tap-highlight-color: transparent; top: 1px; **left:119px**;”>… </div>
<div class=”tab-tooltipContainer”>
<div class =”tab-tooltipContent” wairole=”alert” style>
<div class=”tab-ubertip tab-widget” style=”min-width: 368px;”>.
<div class =”tab-ubertipContent”>
<div class=”tab-ubertipTooltip”>
<span style =”white-space:pre-wrap;tab-size:10;-moz-tab-size:10”>
<div style=”text-align:left;”>
<span style=””>Health Group </span>
</div>
<table style=”vertical-align:top;padding:0px” cellspacing=”0”>
<tbody>
<tr>
<td style =”white-space:pre”>
span style=”font-family” >97</span>
</td>
<td >
<span style=”font-” >Minimum</span>
</td>][1]][1]
Only the value of ‘left’ gets change for each options with some specific margin.
I have tried with following code which mousehover either 87.7% or 30.2% and displays the tooltip. I am not able mousehover on the first option that is 19.9% and so on in continuous manner.
public void sikuli() throws FindFailed, Exception {
driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='data visualization']")));
try{
//Captured image of first option(Health) and clicked on in it
Screen s = new Screen();
Pattern target = new Pattern("Health.PNG");
s.wait(target,20);
s.mouseMove(target);
s.click();
Actions act = new Actions(driver);
act.moveToElement(wbcanvasWidgetLeft).moveByOffset(-50,0).moveToElement(wbcanvasWidgetRight).build().perform();
//Capturing Tooltip
System.out.println(driver.findElement(By.xpath("//div[#class='tab-ubertipTooltip']/span/div[3]/span[1]")).getText());
System.out.println(driver.findElement(By.xpath("//div[#class='tab-ubertipTooltip']/span/div[4]/span")).getText());
}
catch(Exception e)
{
e.printStackTrace();
}
driver.switchTo().defaultContent();
}
Can I use javascript executor with help of style attribute where I set the margin values for ‘left’ but i don't know how to set margin for style attribute while writing xpath.
Any help will be appreciated.

How to move down and move left the scroll bar that exists within the webpage?

I want to move the vertical and horizontal scrollbar that exists within the webpage itself, I couldn't do that with the below method,
When I inspected an element I got the below one,
<div id="DashboardPageContentDiv" class="DashboardPageContentDiv" style="height: 521px; overflow: auto;">
here is the method I created,
public void scrollbardown(RemoteWebDriver remoteWebDriver) throws Exception {
try
{
JavascriptExecutor jse = (JavascriptExecutor)remoteWebDriver;
jse.executeScript("window.scrollBy(0,2500)", "");
}
catch (Exception exc)
{
}
}
This scrollbars exists with in the frame, here is the frame source code:
<iframe allowtransparency="true" id="symbUrlIFrame2" src="/cos/start.swe?SWECmd=GetCachedFrame&SWEC=13&SWEFrame=symbUrlIFrame2&SRN=xaU5eD1S1IOkspAeHu524NMsHC5h5jzSUipwpmEq8bYb " height="800" width="100%" style="height: 607px; position: relative;"></iframe>
Find the nearest element and use scrollintoview function to move there, the below code works,
JavascriptExecutor js = (JavascriptExecutor) remoteDriver;
WebElement
a=remoteDriver.findElement(By.xpath(".//td[#id='titleView!1Subtitle' and
contains(text(),'Time run: ')]"));
js.executeScript("arguments[0].scrollIntoView();",a );

Issues in value selection from dropdown in Selenium webdriver

I am trying to select the value from the drop down. Below is the HTML of that dropdown on the login screen.
<div class="form-group">
<label for="j_companyname">Company</label>
<select id="j_companyname" name="companyname" size="1" onclick="onAccountLostFocus();" style="cursor: pointer;"><option value="AUTO_DEPLOYER">AUTO_DEPLOYER</option><option value="ES188CLIENT1">ES188CLIENT1</option><option value="ES188CLIENT10">ES188CLIENT10</option><option value="ES188CLIENT11">ES188CLIENT11</option><option value="ES188CLIENT12">ES188CLIENT12</option><option value="ES188CLIENT13">ES188CLIENT13</option><option value="ES188CLIENT14">ES188CLIENT14</option><option value="ES188CLIENT15">ES188CLIENT15</option><option value="ES188CLIENT16">ES188CLIENT16</option><option value="ES188CLIENT17">ES188CLIENT17</option><option value="ES188CLIENT18">ES188CLIENT18</option><option value="ES188CLIENT19">ES188CLIENT19</option><option value="Primatics">Primatics</option></select>
<img src="ui/img/ajax-loader.gif" style="margin: 3px; position: absolute; display: none;">
"
I tried to locate element via different locators but unable to pick any value. My script is:
if( driver.findElement(By.id("j_companyname")).isEnabled()){
System.out.println("Element is Enable");
WebElement select = driver.findElement(By.id("j_companyname"));
List<WebElement> options = select.findElements(By.tagName("option"));
for (WebElement option : options)
{
if("ES188CLIENT16".equals(option.getText()))
// option.click();
option.submit();
}
}
else{
System.out.println("Element is Disabled");
}
Please help.
Pleas look below code
Select drpCountry = new Select(driver.findElement(By.id("j_companyname")));
drpCountry.selectByVisibleText("ES188CLIENT16");
// these also can help you
//drpCountry.selectByIndex("givenindex");
//drpCountry.selectByValue("ES188CLIENT16");
As you are trying to select the value from the drop down, lets assume we have to select AUTO_DEPLOYER from the dropdown. To select AUTO_DEPLOYER from the dropdown you can use the following code block :
WebElement my_select = driver.findElement(By.id("j_companyname"));
Select select = new Select(my_select);
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeSelected(By.xpath("//select[#id='j_companyname']//option[contains(.,'AUTO_DEPLOYER')]")));
select.selectByVisibleText("AUTO_DEPLOYER");

Stale element error while getting text of element

I want to get the text of li (second) element as below. Code is able to find the element but it is throwing the error while fetching the gettext. Please help.
WebElement element = driver.findElement(By.xpath("//div[contains(#id,'location_group')]/div/ul/li[2]"));
element.getText();
Error
Stale element reference: element is not attached to the page document
HTML
<div id=location_group>
<div>
<ul class="og-tooltip js-og-tooltip" style="display: none; opacity: 100;">
<li class="title_text">Organization Group</li>
<li class="value_text" style="background: rgb(204, 136, 136); border: 2px solid red;">Global / Aricent / gemsecure </li>
<li> </li>
<li class="title_text">Group ID</li>
<li class="value_text">gemsecure</li>
</ul>
</div>
</div>
May be you have to wait for the parent element to be visible then interact with it
WebElement elementUL = driver.findElement(By.xpath("//div[contains(#id,'location_group')]/div/ul"));
WebDriverWait wait=new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(elementUL));
WebElement element = driver.findElement(By.xpath("//div[contains(#id,'location_group')]/div/ul/li[2]"));
element.getText();
Because this element is wrapped with a ul that has display: none selenium can not interact with it.
Options to try:
element.getAttribute("innerHTML");
or you can use the JavascriptExecutor:
JavascriptExecutor executor = (JavascriptExecutor)driver;
String text= executor.executeScript("document.document.getElementsByClassName('value_text')[0].innerHTML");
Another option is to use querySelector/querySelectorAll which has broader support than getElementsByClassName
Place a explicit waitWebDriverWait and wait till element to be visible
WebElement ele = driver.findElement(By.xpath("Your locator "));
WebDriverWait wait=new WebDriverWait(driver, 25);
wait.until(ExpectedConditions.visibilityOf(ele));
and then getText()
What I recommend is one use the xPath. I think I've tried every solution in java's selenium library, thus my website had tricky selects - once user picks an option, the options are reloaded (ex. selected value sometimes disappears, sometimes different options where loaded depends on particular select).
Lot of stale element exceptions.
What I've used:
private WebElement getDesiredOptionByVisibleText(WebDriver driver, String selectId, String text) {
List<WebElement> options = driver.findElements(By.xpath("//select[#id='" + selectId
+ "']/option[contains(text(),'" + text + "')]"));
if (options == null || options.size() == 0) {
return null;
} else {
return options.get(0);
}
}
especially use this method with waits, for example my wait:
private void waitUntilDesiredOptionVisible(WebDriver driver, String selectId) {
getFluentWait(driver).until(d -> (getDesiredOptionByVisibleText(driver, selectId, value) != null));
}
public static FluentWait<WebDriver> getFluentWait(WebDriver driver) {
return new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(60))
.pollingEvery(Duration.ofMillis(100));
}
Last but not least clicking that option.
private void selectByVisibleTextRegex(WebDriver driver, String selectId) {
WebElement option = getDesiredOptionByVisibleText(driver, selectId, value);
if (option == null) {
throw new NoSuchElementException(String.format("Cannot locate element with text like: %s.", value));
}
option.click();
}

How do I click depending on state of toggle switch?

I have a toggle switch I want to click depending on what the switch state is.
If it is "span.switch-right" I want to do:
findElement(By.cssSelector("span.switch-left")).click();
If it is "span.switch-left" I want to do:
findElement(By.cssSelector("span.switch-right")).click();
HTML:
<div tabindex="0" class="has-switch switch-on switch-animate">
<div>
<span class="switch-left">ON</span>
<label for="profile_isProfileSharedWithNearby"> </label>
<span class="switch-right">OFF</span>
<input id="profile_isProfileSharedWithNearby" name="profile[isProfileSharedWithNearby]" class="form-control-borderless hide" value="1" checked="checked" type="checkbox">
</div>
</div>
Assuming the text is reliable, you could use an xpath selector for this:
findElement(By.xpath("//span[contains(#class, 'switch-') and contains(text(), 'OFF')]")).click();
This will always click the OFF switch.
You probably want to give Fluent Wait a try. It gives you the ability to wait for an element bypassing some exception types and poll DOM after a certain period of time and make sure if that exists or not.
By by = By.cssSelector("span.switch-left");
// Waiting 30 seconds for an element to be present on the page, checking
// for its presence once every 5 seconds.
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, SECONDS)
.pollingEvery(5, SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(by);
}
});
foo.click();
Taken from here
Edit
Provided solution after confusing(;-)) discussion
//*[#tabindex='0'][contains(#class,'switch-off')]
You can check the switch state by following code and can switch it off its on
List<WebElement> switchElement = driver.findElements(By
.cssSelector("div.has-switch.switch-on.switch-animate"));
System.out.println(switchElement.size() + " : Switch Size");
// Check its on, if its on then switch it off
if (switchElement.size() != 0) {
switchElement.get(0)
.findElement(By.cssSelector("span.switch-left")).click();
} else
System.out.println("Switch is already off");