I need to get the Select Tag and Input Tag in web table for this I created the below code to get the Tag name in web table.
For this
Created a List of Elements to get the Number of rows in table.
For declare the variable as "i" to looping.
Find the Select tag in each row to sent the inputs in table.if the select tag presence in row pass the input value else pass different value in web table.
// web Table
WebElement table =d.findElement(By.xpath("//*[#id='ui-grid']/div/div/div/div[2]/table/tbody"));
List<WebElement> trcount = table.findElements(By.tagName("tr"));
int size = trcount.size();
System.out.println(size);
//Using size created the for loop to find each row available in table.
for(int i=1;i<size;i++) {
//Declare the Xpath to find the particular row
By tag = By.xpath("(//*[#id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/select)["+i+"]");
By Input_tag = By.xpath("(//*[#id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/input)["+i+"]");
List<WebElement> tdcount = trcount.get(i).findElements(tag);
String tag1 = tdcount.get(i).getTagName();
System.out.println(tag1);
if(tag1.equals("select")){
d.findElement(By.xpath(tag))Select level = new Select(d.FindElement(tag));
level.selectByVisibleText("YES");
}else {
d.findElement(Input_tag).sendKeys("12");
}
}
Expected Result:
If Web table presence Select tab then value selected from dropdown else Input Tag presence value should be passed.
Actual Result:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
//Get the number of count in web table.
WebElement table =d.findElement(By.xpath("//*[#id='ui-grid']/div/div/div/div[2]/table/tbody"));
List<WebElement> trcount = table.findElements(By.tagName("tr"));
int size = trcount.size();
System.out.println(size);
//Select particular tag(Select tag)
List<WebElement> Select = table.findElements(By.xpath("//*[#id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/select"));
int select_size = Select.size();
System.out.println(select_size);
//If web table have select tag perfrom below else to catch no search elements exceptions.
try{
for(int j=1;j<=select_size;j++) {
By tag = By.xpath("(//*[#class='ng-star-inserted']/span/select)["+j+"]");
System.out.println("Test");
}
}catch(Exception e){
System.out.println((e.getMessage()));
}
//Select particular tag(Input tag)
List<WebElement> Select1 = table.findElements(By.xpath("//*[#id='ui-grid']/div/div/div/div[2]/table/tbody/tr/td/span/input"));
int select_input = Select1.size();
System.out.println(select_input);
try{
for(int i=1;i<=select_input;i++) {
By tag1 = By.xpath("(//*[#class='ng-star-inserted']/span/input)["+i+"]");
System.out.println("Test");
d.findElement(tag1).sendKeys("12345");
Thread.sleep(3000);
//d.findElement(Accept_button).click();
}
}catch(Exception e){
System.out.println((e.getMessage()));
}
Related
Scenario:
I select Two Ids and gets added in the Table which can been seen in the below screen shot.
Over Here i need to Store the value in a Method and then call that method in other Place.
The Problem is iam not able to get the Values .
Code:
public boolean checkselectedDemandId()
{
String cDemand = "";
cDemand = "//table//tr//td[contains(#class,'mat-column-demandId')]";
List<WebElement> rows = driver.findElements(By.xpath(cDemand));`
for(WebElement row:rows)`
{if(cDemand.length()>0)
{
selectedDemandId = cDemand;
return true;
}`else
{
return false;
}
public String getselectedCreatedDemandId()
{
return selectedDemandId;
}
HTML of the Table:
4384SELTESTTECHMTESSINGAPOREHONGKONG2030-04-092040-06-152055-12-30delete 4383SELTESTTECHMTESSINGAPOREHONGKONG2030-04-092040-06-152055-12-30delete
If you are sure about number of rows, use the following code
List<WebElement> rows = driver.findElements(By.xpath("//table//tr//td[contains(#class,'mat-column-demandId')]"));
String rowOne = rows.get(0).getText();
String rowTwo = rows.get(1).getText();
I have a webtable which has multiple rows. I want to create a loop such that it traverses all the rows of this table and click on column 1 elements of first row, then check inside that an Edit button exists or not. Then come back and click on next element and check the edit button. Then come back and repeat till all rows are traversed.
But in current code implemented by me, it is just traversing the first row and then exiting. Could someone help me with same.
viewDiscussionScope(driver, scope);
WebElement paginationLabel = WaitUtils.waitForElement(driver, By.cssSelector(".v-csslayout-cvr-c-pagination__header"));
if(paginationLabel.isDisplayed())
{
WebElement table = WaitUtils.waitForElement(driver, By.cssSelector("table.eds-o-table.cvr-c-table--list tbody"));
List<WebElement> rows = table.findElements(By.cssSelector("tr.eds-o-table__row"));
for(WebElement row: rows)
{
List<WebElement> tableCols = row.findElements(By.cssSelector("td.eds-o-table__cell:nth-of-type(1)"));
for(WebElement col : tableCols)
{
col.findElement(By.cssSelector(".v-label-eds-c-text--bold")).click();
WebElement messageField = WaitUtils.waitForElement(driver, By.cssSelector(".eds-o-media__body-eds-o-media__body--top .v-label-eds-u-flexitext.v-label-undef-w:nth-of-type(1)"));
String messageText = messageField.getText();
boolean editLabel = (driver.findElement(By.cssSelector(".eds-c-button-set-eds-c-button-set--align-right .v-button-eds-s-is-first")).getText()).equals("Edit");
if(!(editLabel))
{
LOG.info(messageText+" is not editable by the logged in user");
}
else
{
LOG.info(messageText+" is editable by the logged in user");
}
break;
}
break;
}
}
}
Remove break statement from your parent for loop!
for(WebElement row: rows)
{
List<WebElement> tableCols = row.findElements(By.cssSelector("td.eds-o-table__cell:nth-of-type(1)"));
for(WebElement col : tableCols)
{
// your code
}
// REMOVE => break;
}
}
You have 2 break that need to be removed and you don't need to loop through the td if you want to click only first td
if (paginationLabel.isDisplayed())
{
WebElement table = WaitUtils.waitForElement(driver, By.cssSelector("table.eds-o-table.cvr-c-table--list tbody"));
List<WebElement> rows = table.findElements(By.cssSelector("tr.eds-o-table__row"));
for (WebElement row : rows)
{
// get first td
WebElement tableCol = row.findElement(By.cssSelector("td.eds-o-table__cell:nth-of-type(1)"));
tableCol.findElement(By.cssSelector(".v-label-eds-c-text--bold")).click();
WebElement messageField = WaitUtils.waitForElement(driver, By.cssSelector(".eds-o-media__body-eds-o-media__body--top .v-label-eds-u-flexitext.v-label-undef-w:nth-of-type(1)"));
String messageText = messageField.getText();
boolean editLabel = (driver.findElement(By.cssSelector(".eds-c-button-set-eds-c-button-set--align-right .v-button-eds-s-is-first")).getText()).equals("Edit");
if (!(editLabel))
{
LOG.info(messageText + " is not editable by the logged in user");
}
else
{
LOG.info(messageText + " is editable by the logged in user");
}
}
}
first you need to find where is edit button located in table you can find its xpath, does of all edit buttons having same xpath if yes then your problem will be solved in you just need to find by findElements() method and store it into List and to get value of edit buttons or to check if is available or not you can call .get() method which will get all respected buttons of same xpath and by specifying index as .get(0),.get(1) or pass it in loop you get his value by .getText() method and check or perform actions on that
If you could provide the DOM of the table you trying to work on, i can help you better.
If i understood correctly your problem here is the solution, also, i strongly advice to use methods for small tasks, which will help you in the long run. Also, the selectors needs a better wraping, also the explicit wait needs a wrapper with his own methods.
try this:
if (paginationLabel.isDisplayed())
{
WebElement table = WaitUtils.waitForElement(driver, By.cssSelector("table.eds-o-table.cvr-c-table--list tbody"));
List<WebElement> rows = table.findElements(By.cssSelector("tr.eds-o-table__row"));
for (int i=1; i<=rows.size; i++)
{
// get first td
clickColumn(int i);
getMessageText(int i);
boolean editLabel = (driver.findElement(By.cssSelector(".eds-c-button-set-eds-c-button-set--align-right .v-button-eds-s-is-first")).getText()).equals("Edit");
if (!(editLabel))
{
LOG.info(messageText + " is not editable by the logged in user");
}
else
{
LOG.info(messageText + " is editable by the logged in user");
}
}
}
private clickColumn(int position) {
WebElement tableCol = row.findElement(By.cssSelector("td.eds-o-table__cell:nth-of-type(" + position + ")"));
tableCol.findElement(By.cssSelector(".v-label-eds-c-text--bold")).click();
}
private getMessageText(int position) {
WebElement messageField = WaitUtils.waitForElement(driver, By.cssSelector(".eds-o-media__body-eds-o-media__body--top .v-label-eds-u-flexitext.v-label-undef-w:nth-of-type(" + position + ")"));
String messageText = messageField.getText();
}
I want to know how to nest dropdowns using selenium webdriver using java,i.e., I have 2 dropdowns and can these dropdowns be nested one after the other?
After looping 2 times for a dropdown it is showing stale element reference error
I have written the following code:
Select drpdwns6 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlBillable\"]")));
List <WebElement> sels6 = drpdwns6.getOptions();
sels6.size();
for(int s6=0;s6<sels6.size();s6++) {
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);
Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlofflinestatus\"]")));
List <WebElement> sels7 = drpdwns7.getOptions();
sels7.size();
for(int s7=0;s7<sels7.size();s7++) {
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}
My guess is selecting the option from the dropdown refreshes the DOM, so the exception is thrown. You need to relocate the dropdown in each itreation
Select drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
int drpdwns6Size = drpdwns6.getOptions().size();
for(int s6 = 0 ; s6 < drpdwns6Size ; s6++) {
drpdwns6 = new Select(driver.findElement(By.id("MainContent_ddlBillable")));
drpdwns6.selectByIndex(s6);
System.out.println("selected value"+s6);
Select drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
int drpdwns7Size = drpdwns7.getOptions().size();
for(int s7 = 0 ; drpdwns7Size ; s7++) {
drpdwns7 = new Select(driver.findElement(By.id("MainContent_ddlofflinestatus")));
drpdwns7.selectByIndex(s7);
System.out.println("selected value"+s7);
}
}
As a side note, if you have an id use By.id it instead of By.xpath
You get the Stale element exception whenever the element present in the DOM is deleted or removed or is not available.
The above answer (ie) relocating the element once the DOM is refreshed or you could use Webdriver wait, If an element is not attached to DOM then you could try using ‘try-catch block’ within ‘for loop’ like below
driver.manage().timeouts().implicitlywait(30,TimeUnit.SECONDS);
try{
Select drpdwns6 = new
Select(driver.findElementByXpath("//[#id=\"MainContent_ddlBillable\"]")));
List <WebElement> sels6AllOptions = drpdwns6.getOptions();
int count1=sels6AllOptions.size();
for(int s6=0;s6<count1;s6++)
{
drpdwns6.selectByIndex(s6);
}
}
catch(StaleElementException e1){
System.out.println("selected value"+s6);
}
try{
Select drpdwns7 = new Select(driver.findElement(By.xpath("//*[#id=\"MainContent_ddlofflinestatus\"]")));
List <WebElement> sels7AllOptions = drpdwns7.getOptions();
int count2=sels7AllOptions.size();
for(int s7=0;s7<count2;s7++) {
drpdwns7.selectByIndex(s7);
catch(StaleElementException e2){
System.out.println("selected value"+s7);
}
}
In my application on users profile page, user has:
Name: XYZ
Age: ##
Address: st.XYZ
and so on...
When an element is missing (example age) other row takes its place, so I can't hardcode the xpath of elements. What I want is:
I want to (print) extract entire table data and compare with actual.
So when I ask for "Name" as key it should give cell value infront of it as value of key.
What I tried:
I was able to get text of tr tags elements keeping td fixed. But for another user when some row is missing it fails or gives wrong value.
for (int i = 2; i < 58; i++) {
String actor_name = new WebDriverWait(driver, 30).until(ExpectedConditions
.elementToBeClickable(By.xpath(first_part+i+part_two))).getText();
System.out.print("\n"+"S.no. "+(i-1)+" "+actor_name);
try {
driver.findElement(By.xpath(first_part+i+part_two)).click();
new WebDriverWait(driver, 30).until(ExpectedConditions
.elementToBeClickable(By.partialLinkText("bio"))).click();
//driver.findElement(By.partialLinkText("bio")).click();
} catch (Exception e) {
// TODO: handle exception
System.out.println("Not a link");
}
Thread.sleep(5000);
System.out.print(" "+driver.findElement(By.xpath("//*[#id='overviewTable']/tbody/tr[3]/td[2]")).getText());
driver.get("http://www.imdb.com/title/tt2310332/fullcredits?ref_=tt_cl_sm#cast");
}
Above code works fine for top 3 actors on this page but fails for 4th because that doesn't have one row missing on bio page.
On the bio page there two columns in the table one has attribute other has its value. I want to make a collection with key value pair with key as attribute (value from left column) and its value as value from right column. So that I get the freedom of fetching the values by mentioning the attribute value.
I am using JAVA to write scripts.
Can you try out with following code and provide me with any concerns if you have any...
driver.get("http://www.imdb.com/title/tt2310332/fullcredits?ref_=tt_cl_sm#cast");
String height = "";
String actorName = "";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
List<WebElement> lstUrls = driver.findElements(By
.xpath("//span[#itemprop='name']/..")); // all a tags
List<String> urls = new ArrayList<>();
for (WebElement webElement : lstUrls) {
urls.add(webElement.getAttribute("href")); // saving all hrefs attached in each a tag
}
Map<String, String> actorHeightData = new HashMap<String, String>();
for (String string : urls) {
driver.get(string);
actorName = driver.findElement(
By.xpath(".//*[#id='overview-top']/h1/span")).getText(); // Getting actor's name
driver.findElement(By.xpath("//a[text()='Biography']")).click(); // Clicking Biography
try {
height = driver.findElement(
By.xpath("//td[.='Height']/following-sibling::td"))
.getText(); // Getting height
} catch (NoSuchElementException nsee) {
height = ""; // If height not found
}
actorHeightData.put(actorName, height); // Adding to map
}
You can create class PersonData with all nullable fields you need. But with not null getters.
for example
calss PersonData{
private String name;
public getName(){
if(name == null)
return "";
return name;
}
}
and store all persons in a List.
In you page you will ask person for field and always have something in table's cell.
I am trying to get the value selected from the dropdown using the below snippet of code:
Select dropdown = new Select(driver.findElement(By.xpath(prop.getProperty("items"))));
WebElement tmp = dropdown.getFirstSelectedOption();
tmp.getText();
String s = tmp.getText();
System.out.println(s);
When I run the application it gives me following error:
org.openqa.selenium.StaleElementReferenceException: Element not found
in the cache - perhaps the page has changed since it was looked up
Thank You
boolean flag = false;
Select dropdown = new Select(driver.findElement(By.xpath(prop.getProperty("items"))));
List<WebElement> list = dropdown.getOptions();
for (WebElement temp : list) {
if (temp.getText().equals(prop.getProperty("your value").toString())) {
flag = true;
}
}