package com.examples;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Dropdown_04 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("Webdriver.chrome.driver", "chromedriver");
WebDriver wd = new ChromeDriver();
int i = 0;
if (i == 0) {
System.out.println("To understand selectByIndex and .getFirstSelectionOption");
wd.get("https://www.computerhope.com/jargon/h/html-select-tag.htm");
WebElement we = wd.findElement(By.xpath(".//*[@id='main-content']/article/select"));
JavascriptExecutor js = ((JavascriptExecutor) wd);
String color = "rgb(0,200,0)";
js.executeScript("arguments[0].style.backgroundColor = '" + color + "'", we);
Thread.sleep(5000);
// Going to select value that falls under index as per
new Select(we).selectByIndex(4);
WebElement we1 = new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getFirstSelectedOption();
// Going to print latest value that is selected under drop down
System.out.println(we1.getText());
System.out.println("***End***");
}
System.out.println("To fetch all the values of options only not from optgroup");
List<WebElement> we2 = new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getOptions();
for (WebElement w : we2) {
System.out.println(w.getText());
}
System.out.println("***End***");
System.out.println("To set value in drop down \"Tyrannosaurus\"");
wd.findElement(By.xpath(".//*[@id='main-content']/article/select")).sendKeys("Tyrannosaurus");
System.out.println("***End***");
System.out.println("To know total number values available in dropdown");
int k = new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getOptions().size();
System.out.println(new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getOptions().size());
System.out.println("***End***");
System.out.println(wd.getTitle());
Thread.sleep(5000);
wd.quit();
}
}
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Dropdown_04 {
public static void main(String[] args) throws InterruptedException {
System.setProperty("Webdriver.chrome.driver", "chromedriver");
WebDriver wd = new ChromeDriver();
int i = 0;
if (i == 0) {
System.out.println("To understand selectByIndex and .getFirstSelectionOption");
wd.get("https://www.computerhope.com/jargon/h/html-select-tag.htm");
WebElement we = wd.findElement(By.xpath(".//*[@id='main-content']/article/select"));
JavascriptExecutor js = ((JavascriptExecutor) wd);
String color = "rgb(0,200,0)";
js.executeScript("arguments[0].style.backgroundColor = '" + color + "'", we);
Thread.sleep(5000);
// Going to select value that falls under index as per
new Select(we).selectByIndex(4);
WebElement we1 = new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getFirstSelectedOption();
// Going to print latest value that is selected under drop down
System.out.println(we1.getText());
System.out.println("***End***");
}
System.out.println("To fetch all the values of options only not from optgroup");
List<WebElement> we2 = new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getOptions();
for (WebElement w : we2) {
System.out.println(w.getText());
}
System.out.println("***End***");
System.out.println("To set value in drop down \"Tyrannosaurus\"");
wd.findElement(By.xpath(".//*[@id='main-content']/article/select")).sendKeys("Tyrannosaurus");
System.out.println("***End***");
System.out.println("To know total number values available in dropdown");
int k = new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getOptions().size();
System.out.println(new Select(wd.findElement(By.xpath(".//*[@id='main-content']/article/select")))
.getOptions().size());
System.out.println("***End***");
System.out.println(wd.getTitle());
Thread.sleep(5000);
wd.quit();
}
}
No comments:
Post a Comment