Double click on table row and insert value - java

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?

Related

Select a textbox at the location where i have a match for the header data and data in first column

This is how my page looks like on which I need to work.
The DOM looks like this
Scenario :- I need to traverse the table and where the header data(BH001 etc) and first column data(ABC etc) matches the data input by the user, I need to click on the textbox corresponding to it.
I have written the below specified code but its not working :-
public static void getMarksBox(WebDriver driver, String user, String taskCode) {
UserData userNm = TestData.findUserById(user);
String userName = userNm.getName();
WebElement table = WaitUtils.waitForElement(driver, By.cssSelector("table.eds-o-table.cvr-c-table--marksbook"));
List<WebElement> tableCols = table.findElements(By.cssSelector("td.eds-o-table__cell"));
int columnIndex = -1;
for(int i=1; i<tableCols.size();i++)
{
if(userName.equals(tableCols.get(i).findElement(By.cssSelector(".v-label-cvr-c-data-nav-link")).getText()))
{
columnIndex = i;
break;
}
}
List<WebElement> tableRows = table.findElements(By.cssSelector("tr.eds-o-table__row"));
List<WebElement> tableHeaders = tableRows.get(1).findElements(By.cssSelector(".v-label-cvr-u-margin-right--sm"));
WebElement textBox = table.findElement(By.cssSelector(".v-textfield"));
for(WebElement header :tableHeaders)
{
if(taskCode.equals(header.getText()))
{
textBox = tableRows.get(columnIndex);
textBox.click();
WaitUtils.sleepInSeconds(5);
break;
}
}
}
As #supputuri suggested, you can find the matched row or cell directly via XPath/Css selector to avoid complex loop to reduce execution time.
public static void getMarksBox(WebDriver driver, String user, String taskCode) {
UserData userNm = TestData.findUserById(user);
String userName = userNm.getName();
WebElement table = WaitUtils.waitForElement(driver,
By.cssSelector("table.eds-o-table.cvr-c-table--marksbook"));
WebElement matchedRow = table.findElement(By.xpath(
String.format("./tobdy/tr[td[1][normalize-space(.)='%s']]", userName)))
WebElement matchedTextBox = matchedRow.findElement(
By.cssSelector("./td:nth-child(2) input.v-textfield-eds-c-input"))
matchedTextBox.click()
// or you can directly find the matchedTextBox in one findElement
String xpath = String.format(
"./tobdy/tr[td[1][normalize-space(.)='%s']]" +
"/td[2]//input[contains(#class,'v-textfield-eds-c-input')]", userName)
WebElement matchedTextBox = table.findElement(By.xpath(xpath))
matchedTextBox.click()
}
Use this below xpath to access the inputbox directly, rather doing the loops that you have written in the above method.
//td[position()=count(//th[contains(.,'First Name')]/preceding-sibling::th)+1 and normalize-space(.)='ABC']/ancestor::tr//td[position()=count(//th[contains(.,'BH001')]/preceding-sibling::th)+1]//input[contains(#class,'v-textfield-eds-c-input')]
Here is the general notation:
//td[position()=count(//th[contains(.,'your reference column name')]/preceding-sibling::th)+1 and normalize-space(.)='reference value']/ancestor::tr//td[position()=count(//th[contains(.,'target column name')]/preceding-sibling::th)+1]//input[contains(#class,'v-textfield-eds-c-input')]

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

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

Handle listbox values in Selenium Webdriver

I am new to Selenium. I just want to know if I am having some excel data. How can I compare it with any listbox value and select that listbox value while executing any script.
I have following code:
System.setProperty("webdriver.gecko.driver","E:/Bhavin/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
WebElement val = driver.findElement(By.id("month"));
List<WebElement> months = val.findElements(By.tagName("option"));
//System.out.println("values are "+months);
for (int i=0;i<=months.size();i++){
String value = months.get(i).getText();
//System.out.println("values for the months are "+value);
}
ArrayList<String> month = readExcel(5);
//System.out.println("months.... "+month);
Suppose I have stored an Excel column in an array list (month). How can I compare the arraylist value from Excel to the list value that I stored for f.b months and apply select command for the same.

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

StaleElementReferenceException in datepicker

I am able to select date, day and year from date picker, but after selecting, the page is refreshed and the Webdriver element is detached and getting StaleElementReferenceException. I am not understanding how to build the object instance as its a date picker, it cant be clicked again.
WebElement dateWidget = getDriver().findElement(DATEPICKER_WIDGET);
List<WebElement> rows = dateWidget.findElements(By.tagName("tr"));
for (WebElement row : rows) {
List<WebElement> columns = row.findElements(By.tagName("td"));
for (WebElement cell : columns) {
try{
if (cell.getText().equals(String.valueOf(calendar.get(Calendar.DATE)))) {
cell.findElement(By.linkText(String.valueOf(dayValue))).click();
boolean flag = commonpage.isAlertPresent();
if (flag == true) {
String text = commonpage.closeAlertAndGetItsText();
addScreenshot("Alert text: " + text);
}
break;
}}catch(StaleElementReferenceException e){
}
The problem is after picking the correct date, I got an alert box and that too handled perfectly. But after alert box, the page is refreshed, and selenium is having problem in identifying what is cell (WebElement) as the page is refreshed. Not understanding how to re-instantiate WebElement cell.

Categories

Resources