How to select first 5 rows in a table using selenium webdriver? - java

I have writen below code to select first 5 rows using selenium webdriver. But it is not working.
public void testRowSelectionUsingControlKey() {
List tableRows = driver.findElements(By.xpath("//table[#class='iceDatTbl']/tbody/tr"));
Actions builder = new Actions(driver);
builder.click(tableRows.get(0)).keyDown(Keys.CONTROL).click(tableRows.get(1)).keyDown(Keys.CONTROL).click(tableRows.get(2)).keyDown(Keys.CONTROL).click(tableRows.get(3)).keyDown(Keys.CONTROL).click(tableRows.get(4)).keyUp(Keys.CONTROL).build().perform();
}

You don't need to call the method keyDown(Keys.CONTROL) every time you select a row from your table. Try calling keyDown(Keys.CONTROL) before you select all the rows and then call keyUp(Keys.CONTROL).
public void testRowSelectionUsingControlKey() {
List tableRows = driver.findElements(By.xpath("//table[#class='iceDatTbl']/tbody/tr"));
Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL)
.click(tableRows.get(0))
.click(tableRows.get(1))
.click(tableRows.get(2))
.click(tableRows.get(3))
.click(tableRows.get(4))
.keyUp(Keys.CONTROL).build().perform();
}

Related

Double click on table row and insert value

I use this code to double click on table row and insert a value into input filed:
WebElement element = driver.findElement(By.xpath("/......iv[3]"));
Actions builder = new Actions(driver);
builder.doubleClick(element).perform();
Thread.sleep(3000);
// Insert into Accessorial Tasks screen Actual value
insertInputFieldByXPath(driver, "...../input",
"3");
protected void insertInputFieldByXPath(WebDriver driver, String input_id, String value){
WebDriverWait webDriverWait = new WebDriverWait(driver, 15);
WebElement webElement = webDriverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(input_id)));
webElement.clear(); // First we delete the old value in case if this is a spinner
webElement.sendKeys(value);
}
When I double click with Selenium the table row becomes input field. But when I try to enter some value the id is not found. Maybe it's not found because the input field ID is dynamically added when I double click.
Do you know how this issue can be solved?

Shadow Root - click in a href under several shadow roots

I have a list of links inside of several shadowRoots. Already solved this problem.
public WebElement expandRootElement(WebElement element) {
WebElement ele = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot",element);
return ele;
}
WebElement root5_adminPanel = shadowRoot4_MduiContainerChild2.findElement(By.cssSelector("#layout > border-layout > ng-view > admin-panel"));
WebElement shadowRoot5_AdminPanel= expandRootElement(root5_adminPanel);
WebElement root6_breadCrumb = shadowRoot5_AdminPanel.findElement(By.cssSelector("#layout > border-layout > breadcrumb"));
WebElement shadowRoot6_breadCrumb = expandRootElement(root6_breadCrumb);
WebElement root6_domainPanel = shadowRoot5_AdminPanel.findElement(By.cssSelector("#layout > border-layout > ng-view > gdsr-domain-panel"));
WebElement shadowRoot6_domainPanel = expandRootElement(root6_domainPanel);
WebElement root7_selectDomain = shadowRoot6_domainPanel.findElement(By.cssSelector("#domainContainer > domain-panel-item.ng-binding.last"));
WebElement shadowRoot7_selectDomain = expandRootElement(root7_selectDomain);
When I reach this shadowRoot7, I have a list of items with the same name, which I already created a List to fix it.
List<WebElement> rows_table = shadowRoot6_domainPanel.findElements(By.cssSelector("#domainContainer > domain-panel-item:nth-child(n)"));
(They are around 45 items)
This will select all of them, in this case all the domain-panel-item rows.
My problem is that each domain-panel-item still contain another shadowRoot (the same path for all of them) an i would like to select a random item, not the first or last one, for example, the item number 43.
enter image description here
My solution was this one but it doesn't work because it doesnt access to the link that i want:
public void clickSelectedDomain(String domain) {
List<WebElement> rows_table = shadowRoot6_domainPanel.findElements(By.cssSelector("#domainContainer > gdsr-domain-panel-item:nth-child(n)"));
int rows_count = rows_table.size();
for (int row=0; row<rows_count; row++) {
if(rows_table.get(row).getAttribute("href").contains(domain)) {
rows_table.get(row).click();
}
}
}
Some have an idea how to fix this?
You solved the problem by calling recursively executeScript() in order to get the imbricated Shadow DOMs but actually you could have just called executeScript() once, and inside got the Shadow DOMs successively.
driver.executeScript( function ()
{
var root1 = document.querySelector( 'selector string 1' ).shadowRoot
var root2 = root1.querySelector( 'selector string 2' ).shadowRoot
var root3 = root2.querySelector( 'selector string 3' ).shadowRoot
...
return foundElement
}
Anyways, in the for() {} loop, you should extract the ultimate Shadow DOM one last time, and then select the <a> element to check its content.

Getting issue while trying to select rows in sequence via Selenium Webdriver

I have table in website. Table allows to select multiple rows by pressing Shift key + Down arrow keys.
I am trying to perform same using selenium webdriver but it's not selecting rows one by one, it select row then unselect it and goes to next....
My Code :
List<WebElement> TRcount = driver.findElements(By.tagName("tr"));
int x;
for(x=0;x<TRcount.size();x++)
{
Actions rows = new Actions(Base.getdriver());
rows.keyDown(TRcount.get(x),Keys.SHIFT).keyUp(TRcount.get(x+1), Keys.SHIFT).build();
rows.build().perform();
TRcount.get(x).click();
}
You pressing keyDown and keyUp. Try
Actions rows = new Actions(Base.getdriver());
rows.keyDown(Keys.SHIFT).perform();
for(x = 0 ; x < TRcount.size() ; x++)
{
TRcount.get(x).click();
}
rows.keyUp(Keys.SHIFT).perform();
By the way, perform() is doing build(), no need to call them both.
I believe this should be:
List<WebElement> TRcount = driver.findElements(By.tagName("tr"));
int x;
Actions rows = new Actions(Base.getdriver());
rows = rows.keyDown(Keys.SHIFT).build();
for(x=0;x<TRcount.size();x++)
{
rows = rows.sendKeys(TRcount.get(x),Keys.DOWN).build();
}
rows = rows.keyUp(Keys.SHIFT).build();
rows.build().perform();
If you have public URL to replicate this then We could try it more easily.

I am trying to click on an option in the dropdown with the help of the li tag but not getting the output neither am i getting any error

public class CssSelector3 {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
driver.get("http://qa.letslearnindia.com");
driver.manage().window().maximize();
driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(5000);
driver.findElement(By.cssSelector("input[id='inputSuccess2']")).sendKeys("tester42#gmail.com");
driver.findElement(By.cssSelector("input[id='inputSuccess3']")).sendKeys("123456");
driver.findElement(By.cssSelector("input[id='btn_login']")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[#id='navbar']/ul/li[2]/a")).click();
driver.findElement(By.xpath("//*[#id='horizontalTab']/div/div[1]/div[1]/div[2]/a/input")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//*[#id='full_height_base']/div/div[3]/div[3]/div[2]/div/ul[2]/li[1]/a")).click();
driver.findElement(By.xpath("//*[#id='courseTitle']")).sendKeys("Automation Test");
driver.findElement(By.xpath("//*[#id='courseSubtitle']")).sendKeys("Automating the test cases");
Thread.sleep(5000);
WebElement dropdown = driver.findElement(By.xpath("//*[#id='validate-me-plz']/div[1]/div[2]/div/p/span"));
List<WebElement> li = dropdown.findElements(By.tagName("li"));
System.out.println(li.size());
String element;
for(int i =0; i<li.size();i++){
element = li.get(i).getAttribute("data-val");
if(element.equals("English")){
li.get(i). click();
When choosing from <select> tag you should use Select class
WebElement dropdown = driver.findElement(By.id("courseLanguage")); // locate the dropdown
Select select = new Select(dropdown); // initialize select
select.selectByVisibleText("English"); // choose the option with "English" as text
// select.selectByValue("English"); // choose the option with "English" as value
Its still giving an error as "Element is not currently visible and so may not be interacted with"
To make sure the element is visible before interaction use explicit wait
// this will wait up to 10 seconds for the dropdown to be visible and will return the dropdown element
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement dropdown = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("courseLanguage")));
Select select = new Select(dropdown);
select.selectByVisibleText("English");
try by selecting all the elements with a select tag by using findElements method
and then pass the desired element to the Select class as below :
List<WebElement> AllselectTags= driver.findElements(By.tagName("select"));
WebElement selectedElement = AllselectTags.get(0);
Select s = new Select(selectedElement);
s.selectByValue("English");

Selenium webdriver to select date

I am newbie to Selenium and not able to select date website http://www.redbus.in.
Can some one help me out? I tried to pass value to the read only text box, but it was in vain.
public static void setup() throws Exception {
System.out.println("Browser Set up start.. ");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.get("http://www.redbus.in/");
System.out.println("Browser Set up Completed ");
}
#Test
public static void SelectSrcDest() throws Exception {
System.out.println("Constructing Url to open");
driver.findElement(By.id("txtSource")).sendKeys("Bangalore");
driver.findElement(By.id("txtDestination")).sendKeys("Chennai");
driver.findElement(By.xpath("html/body/div[2]/div/section/div[1]/img")).click();
driver.findElement(By.xpath("html/body/div[2]/div/section/div[1]/img")).click();
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("window.document.getElementById('txtOnwardCalendar').setAttribute('value','27-Mar-2014')");
driver.findElement(By.xpath("html/body/div[2]/div/section/div[4]/button")).click();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
setup();
SelectSrcDest();
}
You can use xPath to find the element by text and then use the div id & table class to choose which trip & month to pick.
For instance in order to select the date of journey:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.redbus.in");
driver.findElement(By.id("txtOnwardCalendar")).click();
By locator = By.xpath("//div[#id='rbcal_txtOnwardCalendar']" +
"/table[#class='monthTable first']" +
"//td[contains(text(), '10')]");
driver.findElement(locator).click();
would select the 10th day of the first month. If you use monthTable last as the class instead, you get 10th day of the second month. And if you change the div id into rbcal_txtReturnCalendar, you can pick a date for the return trip.
You can try the following piece of code. It works perfect
driver.findElement(By.id("DDLSource")).sendKeys("C");
driver.findElement(By.xpath("//dl[#id = 'lis']//dt[text()='Chennai']")).click();
driver.findElement(By.id("DDLDestination")).sendKeys("t");
driver.findElement(By.xpath("//dl[#id = 'dis']//dt[text()='Theni']")).click();
driver.findElement(By.className("calenImg")).click();
driver.findElement(By.xpath("//td[text()='Feb']/../..//a[text()='17']")).click();
driver.findElement(By.id("calendar1")).click();
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
driver.findElement(By.xpath("//td[text()='Feb']/../..//a[text()='20']")).click();
Try this to select the date:
driver.findElement(By.id("give id")).click();
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-year")));
Select select = new Select(selectElement);
select.selectByValue("2012");
Thread.sleep(6000);
WebElement dateWidget = driver.findElement(By.id("ui-monthpicker-div"));
List<WebElement> columns011=dateWidget011.findElements(By.tagName("td"));
for (WebElement cell: columns011){
//Select Month
if (cell.getText().equals("Feb")){
cell.findElement(By.linkText("Feb")).click();
break;
}
}
Try to figure out the element through ID.. Its very easy to target exactly by the ID.
driver.findElement(By.id("txtOnwardCalendar")).click();
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("monthTable first")));
Select select = new Select(selectElement);
select.selectByValue("2012");
Thread.sleep(6000);
WebElement dateWidget = driver.findElement(By.id("monthTitle"));
List<WebElement> columns011=dateWidget011.findElements(By.tagName("td"));
for (WebElement cell: columns011){
//Select Month
if (cell.getText().equals("Feb")){
cell.findElement(By.linkText("Feb")).click();
break;
}
}

Categories

Resources