How to list all the checkboxes available in a webpage by displaying their Visible Text in selenium using Java?
For that website this should do
List<WebElement> checkboxes = driver.findElements(By.cssSelector("input[type=checkbox]"));
JavascriptExecutor js = (JavascriptExecutor) driver;
if (checkboxes.isEmpty()) {
System.out.println("No Checkbox present in the page");
} else {
for (WebElement checkbox : checkboxes) {
if (checkbox.isDisplayed()) {
String text=(String) js.executeScript("return arguments[0].nextSibling.textContent.trim();", checkbox);
System.out.println(text);
}
}
}
Related
I have one button which sometimes does not do the actual click, due to that i wrote a code to handle it with a while loop where it will click until it finds out a new assignment title of the page. But this is not working as expected. Please help me, thanks in advance.
String titleWhenLoad = driver.findElement(By.xpath("//span[#class='assignment_title']")).getText();
String titleNext = titleWhenLoad;
while ((titleWhenLoad.equals(titleNext))
&& (driver.findElement(By.xpath("//span[#class='assignment_title']")).isDisplayed())) {
WebElement btn = driver.findElement(By.xpath("//button[contains(text(),'Submit')]"))
btn.click();
if (driver.findElement(By.xpath("//span[#class='assignment_title']")).isDisplayed()) {
titleNext = driver.findElement(By.xpath("//span[#class='assignment_title']")).getText();
} else {
break;
}
}
You can use WebDriverWait utilizing .invisibilityOfElementWithText to wait a element with specific text disappear.
To click the button target, I suggest to use Actions class.
Check it out:
String titleWhenLoad = driver.findElement(By.xpath("//span[#class='assignment_title']")).getText();
boolean findNewTitle = false;
while (!findNewTitle) {
try {
new WebDriverWait(driver, 3).until(ExpectedConditions.invisibilityOfElementWithText(By.xpath("//span[#class='assignment_title']"), titleWhenLoad));
findNewTitle = true;
} catch (Exception e) {
WebElement btn = driver.findElement(By.xpath("//button[contains(text(),'Submit')]"));
Actions builder = new Actions(driver);
builder.moveToElement(btn).click(btn).build().perform();
}
}
Import the following:
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.interactions.Actions;
Reference:
https://www.selenium.dev/documentation/en/webdriver/waits/#expected-conditions
https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html
I made a loop for an iframe locator with java code
but its not working. Can anyone see the problem?
invoking the class with:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver,driver.findElement(By.cssSelector("[href*='Search.mvc'][class*='magnify']")));
and use this after:
searchButton.click();
public class IFrameLocator {
public static WebElement switchToIFrameWithElement(WebDriver driver, WebElement element) {
try {
driver.switchTo().defaultContent();
element.isDisplayed();
} catch (Exception continueFlow) {
WebDriverWait wait = new WebDriverWait(driver, 20);
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame));
if (element.isDisplayed()) {
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} return element;
}
}
Because i was passing the whole Webelement By cssSelector it was not even going in the loop. So i pass the element like this:
WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver, By.cssSelector("[href*='Search.mvc'][class*='magnify']"));
So i changed the code and removed the By.cssSelector in 2 spots and all working fine now:
public class IFrameLocator {
public static WebElement switchToIFrameWithElement(WebDriver driver, By element) {
driver.switchTo().defaultContent();
try {
if (driver.findElement(element).isDisplayed()) ;
{
System.out.println("Element is displayed on main page");
}
} catch (Exception continueFlow) {
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
System.out.println("going back to main page");
try {
driver.switchTo().frame(frame);
System.out.println("switched to next frame: " + frame);
if (driver.findElement(element).isDisplayed()) {
System.out.println("element is found in frame: " + frame);
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} System.out.println("returned element succesfully");
return driver.findElement(element);
}
}
public void openNewEbay() throws InterruptedException {
// to click on open link on new tab
Actions rightclick = new Actions(driver);
WebElement elementlocator = driver
.findElement(By.xpath("//li[#class='hl-cat-nav__js-tab']//a[contains(text(),'Electronics')]"));
//rightclick.contextClick(elementlocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Thread.sleep(3000);
rightclick.contextClick(elementlocator).build().perform();
Thread.sleep(5000);
rightclick.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
rightclick.sendKeys(Keys.RETURN).build().perform();
/*ArrayList tab = new ArrayList(driver.getWindowHandles());
System.out.println(tab.size());*/
}
public void openNewEbay() throws InterruptedException {
// to click on open link on new tab
Actions rightclick = new Actions(driver);
WebElement elementlocator = driver
.findElement(By.xpath("//li[#class='hl-cat-nav__js-tab']//a[contains(text(),'Electronics')]"));
//rightclick.contextClick(elementlocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Thread.sleep(3000);
rightclick.contextClick(elementlocator).build().perform();
Thread.sleep(5000);
rightclick.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
rightclick.sendKeys(Keys.RETURN).build().perform();
/*ArrayList tab = new ArrayList(driver.getWindowHandles());
System.out.println(tab.size());*/
}
Selenium-code issue: trying to automate right click but when I right click, it does not move to right-click options instead it clicks on elements. I want to select the option "Open the link on new tab"
Try below code:
public void openNewEbay() throws InterruptedException {
// to click on open link on new tab
Actions rightclick = new Actions(driver);
WebElement elementlocator = driver
.findElement(By.xpath("//li[#class='hl-cat-nav__js-tab']//a[contains(text(),'Electronics')]"));
//rightclick.contextClick(elementlocator).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Thread.sleep(3000);
rightclick.contextClick(elementlocator).build().perform();
Thread.sleep(5000);
rightclick.sendKeys("t").build().perform();
// here "t" performs new tab operation. If you use Keys.ARROW_DOWN of actions class will
// not move mouse on to the context menu and also, we can't inspect options available on // context menu
}
One alerts popup is appearing after saving data. How to check popup visibility using Selenium with java
I would suggest you to use ExpectedConditions like below to check whether the alert is present or not like below:
WebDriverWait wait = new WebDriverWait(driver, 30);
if(wait.until(ExpectedConditions.alertIsPresent())==null)
{
System.out.println("No alert");
else{
System.out.println("Alert present");
}
}
To accept that alert you can use:
driver.switchTo().alert().accept();
To close that alert you can use:
driver.switchTo().alert().dismiss();
To send some values to the alert box you can use:
driver.switchTo().alert().sendKeys("Text");
We can also use try-catch block like below:
public boolean alertPresent()
{
try
{
driver.switchTo().alert();
return true;
}
catch (NoAlertPresentException Ex)
{
return false;
}
}
driver.switchTo().alert();
This will help you to switch to the alert popup window and you can check for the visiblility.
Ok, Find the xpath or id of the popup let's take one example
WebElement popUp = driver.findElement(By.xpath("xpath of the popup"));
if (popUp.isDisplayed())
{
WebElement btnOk = driver.findElement(By.xpath("xpath of the button in alert"));
btnOk.click();
System.out.println("Pop up displayed and button clicked");
}
else
{
System.out.println("Pop up Not found");
}
their is second way you can handle alerts and check if that presents
try
{
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText()+" Alert is Displayed");
}
catch(NoAlertPresentException ex)
{
System.out.println("Alert is NOT Displayed");
}
public void testSuccess() throws InterruptedException {
System.out.println("test arribute popup");
Thread.sleep(3000);
WebElement searchBtn = driver.findElement(By.xpath(".//*[#id='investedList']/span/span[2]/span"));
Actions action = new Actions(driver);
action.moveToElement(searchBtn).perform();
Thread.sleep(3000);
}
when mouse move into button there is popup text display as "invested list"
how to test this using selenium
try this code:
List<WebElement> overlaysTooltips = driver.findElements(By.xpath(".//*[#id='investedList']/span/span[2]/span"));
for(int i = 0; i < overlaysTooltips.size(); i++){
Actions builder = new Actions(driver);
builder.moveToElement(overlaysTooltips.get(i)).perform();
//get button text after hover
if((overlaysTooltips.get(i).getAttribute("value").equals("//ur button hover string")){
//ur condition
Thread.sleep(3000);
}
}
you can use other button attribute what is in ur html code as i dont know what kind of attribute in ur html code there.
like this:
overlaysTooltips.get(i).getAttribute("id")
or
overlaysTooltips.get(i).getAttribute("class")
or
overlaysTooltips.get(i).getAttribute("name")