GWT DOM not working - java

I want to append div inside <TH> in GWT
Element tr = DOM.createTR();
DOM.appendChild(thead, tr);
for (int index = 0; index < headers.size(); index++) {
Element th = DOM.createTH();
Element div = DOM.createDiv();
DOM.appendChild(th, div); // this is not working !
DOM.appendChild(tr, th);
}
Is any thing wrong in my code?

I assume it will give you compile time error because the method `DOM.appendChild(th, div);' second parameter should be part of package structure:
com.google.gwt.user.client.Element
instead of
com.google.gwt.dom.client.Element

Related

How to add a web elements to an ArrayList?

I have a method in which I am trying to add 12 web elements:
private List<WebElement> iphoneSnippetList = new ArrayList<>();
#Test
public void test(){
chromeDriver.get("https://market.yandex.ru/catalog--smartfony/54726/list?hid=91491&glfilter=7893318%3A153043&onstock=1&local-offers-first=0");
new WebDriverWait(chromeDriver, 15).until(ExpectedConditions.elementToBeClickable(By.xpath("//article[#data-autotest-id='product-snippet'][1]")));
for (int i = 0; i <= 12; i++) {
iphoneSnippetList.add((WebElement) By.xpath("//article[#data-autotest-id='product-snippet'][" + i + "]"));
}
System.out.println(iphoneSnippetList);
}
Simplified DOM elements in which I only need to get the text :
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
I need to add all 12 web elements to my array and then make sure that the received elements contain the name "Iphone", but when adding elements, there is exception:
java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class org.openqa.selenium.WebElement (org.openqa.selenium.By$ByXPath and org.openqa.selenium.WebElement are in unnamed module of loader 'app')
iphoneSnippetList is a List of WebElement in Java-Selenium bindings.
I am not sure why you want to add 12 web elements using the loop, instead a findElements with right xpath would have been a good choice. Anyway, there's a problem with you code related to casting.
See below, driver.findElement will return the web element, and we are storing that into a variable called Webelement e, and adding the same into iphoneSnippetList
for (int i = 0; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("//article[#data-autotest-id='product-snippet'][" + i + "]"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);
Also, this loop will run for 13 times not 12 times. In case you want this to run for 12 times, initialize i = 1 not i = 0
I think you will have issue with xpath also, cause you are not using xpath indexing correctly.
try this instead :
for (int i = 1; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("(//article[#data-autotest-id='product-snippet'])['" +i+ "']"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);

Get TH index number using Xpath in selenium

Hi I just want to get the index number of TH column using unique Xpath. Like A loop will run and check on which TH column Xpath match and return the index number. Is there any way i can do this in selenium ? Till now im able to get the tag index and run the the loop but now I have check the on each TH with xpath rather its matched or not and give me the index number. Please let me know is there anything with Im able to achieve this with this logic or any other any Xpath techniques.
Table = driver.findElement(By.xpath("//table/thead[#class='ui-datatable-thead']"))
List<WebElement> rows_head = Table.findElements(By.tagName('th'))
int head_size= rows_head.size()
System.out.println(head_size);
for (int c = 1; c <= head_size; c++) {
driver.findElement(By.xpath(
"//th[4][#class='ui-state-default ui-unselectable-text ui-sortable-column']")
)
// Here is something I want loop will check the each TH with above given
// Xapth and return the TH index in the table on match TH xpath index.
}
Selenium doesn't supply an API to return the xpath of found element. So you can't archive your goal by comparing xpath.
But Selenium supply API to get the attribute value of found element, you can compare with it to see it's your desired element or not.
WebElement thead = driver.findElement(By.xpath("//table/thead[#class='ui-datatable-thead']"));
List<WebElement> heads = thead.findElements(By.tagName('th'));
int head_size= heads.size()
System.out.println(head_size);
String expectedClass = "ui-state-default ui-unselectable-text ui-sortable-column"
int i = 1;
for (; i <= head_size; i++) {
if(heads[i].getAttribute("class").equal(expectedClass))
// check attribute class value is as expect
// you can change to other attribute or check more than one attribute
break;
)
}
System.out.println("Matched th index: " + i);

skipping first row of a html table Java

I am still learning xpath and I am trying to skip the first row of a table because the first row has no values. I am not having success i searched through stack over flow and could not find anyone with a similar issue. My code is below:
int reqIndex = 0;
do {
List<WebElement> payDates = driver.findElements(By.xpath("//tr[starts-with(#id,'changeStartWeekGrid_row_')]/td[contains(#id,'_cell_4')/span]"));
System.out.println(payDates);
for(WebElement pd:payDates) {
LocalDate localDate = LocalDate.now();
java.util.Date d = new SimpleDateFormat("yyyy-MM-dd").parse(localDate.toString());
String str = pd.getText();
if ( str >= (d) ){ // still figuring this out
driver.findElement(By.xpath( "//tr[#id='changeStartWeekGrid_row_'" + reqIndex +"])/TBODY[#id='changeStartWeekGrid_rows_tbody']/TR[7]/TD[1]/DIV[1]/DIV[1]/DIV[1]")).click();
break;
}else{
reqIndex++;
}
}
}while(reqIndex < 7 ); /// do this 7 times
Thread.sleep(10000);
This is the part i am trouble shooting right now
List<WebElement> payDates = driver.findElements(By.xpath("//tr[starts-with(#id,'changeStartWeekGrid_row_')]/td[contains(#id,'_cell_4')/span]"));
System.out.println(payDates);
Thread.sleep(10000);
The xpath works the problem is that it is selecting the first row and it has no values row zero does so it needs to skip the first row and go to row zero.
when i run the code i get this message:
( //tr[starts-with(#id,'changeStartWeekGrid_row_')]/td[contains(#id,'_cell_4')/span])
ilter expression must evaluate to nodeset.
the row highlighted is the row i am trying to skip
cell 4 image
-----radio button html code below
<div id="changeStartWeekGrid.store.rows[5].cells[0]_widget" tabindex="0" class="revitRadioButton dijitRadio" onfocus="var registry = require('dijit/registry'); registry.byId('changeStartWeekGrid').changeActiveCell('changeStartWeekGrid_row_5_cell_0', event);" onblur="var registry = require('dijit/registry'); registry.byId('changeStartWeekGrid').blurActiveCell('changeStartWeekGrid.store.rows[5]', 'changeStartWeekGrid.store.rows[5].cells[0]');"><div class="revitRadioButtonIcon"></div></div>
---radio button picture
Try to use XPath
//table[#id='changeStartWeekGrid_rows_table']//tr[preceding-sibling::tr]
to select all tr nodes except the firts one
You can also use position() as below:
//table[#id='changeStartWeekGrid_rows_table']//tr[position()>1]
Note that in XPath indexation starts from 1 and so [position()>1] predicate means return all sibling nodes skiping the first one
You may also use
//tr[starts-with(#id,'changeStartWeekGrid_row_') and not(starts-with(#id, 'changeStartWeekGrid_row_column'))]/td[5]/span

Java selenium avoiding iframe address bar element

Currently I wrote a short program that traverses through a page and looks for web elements with the tagname = "input", then uses sendkeys to send data to the element, then submits it. The problem I have is that although it works most of the time, if I have an iframe on the page, and I traverse through the webpage looking for an element with the tagname = "input", it focuses on the addressbar of the iframe and sends data to it, then tries to submit it causing an error (when I printed the tagname of the addressbar in the iframe, it printed out "input" on the console).
Is there a way to avoid the addressbar on an iframe from being picked up as an element with the tagname = "input"?
Im using the following to check if an element has tagname of "input":
List<WebElement> element = driver.findElements(By.xpath("//*"));
int mainSize = element.size();
for ( int j = 0; j < mainSize; j++ ) {
if(frameElement.get(j).getTagName().toString().equals("input")){
//do something
}
}
Some notes:
-This occurs when going through the webpage elements searching through tagnames for "input" elements BEFORE switching to the iframe element with the switchTo() method.
In order to avoid the address bar, you can introduce one more check to ensure that the input element is not an address bar. Following is the updated code to achieve this:
List<WebElement> element = driver.findElements(By.xpath("//*"));
int mainSize = element.size();
for ( int j = 0; j < mainSize; j++ ) {
if(element.get(j).getTagName().toString().equals("input") &&
!element.get(j).getAttribute("class").equals("urlbar")){
//do something
}
}
UPDATE 1
We can retry when unexpected browser search bar appears. Try following:
List<WebElement> element = driver.findElements(By.xpath("//*"));
int mainSize = element.size();
for ( int j = 0; j < mainSize; j++ ) {
if(element.get(j).getTagName().toString().equals("input") &&
!element.get(j).getAttribute("class").equals("urlbar")){
//do something
try {
element.get(j).sendKeys(somedata);
element.get(j).submit();
} catch (WebDriverException we) {
System.out.println("It seems browser search bar has been appeared. Retrying...");
//Pressing escape key to get rid of browser search bar
element.get(j).sendKeys(Keys.ESCAPE);
//Retrying
element.get(j).sendKeys(somedata);
element.get(j).submit();
}
}
}
Let me know, if you have any further queries.

How to remove iterator loop and allow cellDateType to work in xlsx java?

Hi to all experts out there,
Currently I have some problem regarding the iterator loop. I need to remove it in order for my data to appear on my xlsx excel sheet but I am not sure how do I go about removing it such that my codes are error free. And I suspect that the error may be on the iterator loop.
For now, this is my codes and an image link of how it looks like now.
The data in the excel sheet are not suppose to have a space in between but apparently, there is. There isn't any data in the first column because I didn't have any data being keyed in into the website. So it's okay for the first column to be blank.
int r = 3;
for (Iterator iter = Cells.iterator();iter.hasNext();) {
Object[] _o = (Object[]) iter.next();
currentRow = s.createRow(r);
for(int colNum = 0; colNum < _col_cnt; colNum++){
XSSFCell currentCell =currentRow.createCell(colNum);
if (CellDataType[c].equals("STRING")
|| CellDataType[c].equals("VARCHAR")) {
String _l = (String) _o[colNum];
if (_l != null) {
currentCell.setCellValue(_l);
System.out.println("Data: " + _l);
}
}
hardcode (Testing):
int r = 3;
for (Iterator iter = Cells.iterator();iter.hasNext();) {
Object[] _o = (Object[]) iter.next();
currentRow = s.createRow(r);
for(int colNum = 0; colNum < _col_cnt; colNum++){
XSSFCell currentCell =currentRow.createCell(colNum);
currentCell.setCellValue("Hello");
Your problem is not in the code you've shown here, but it's in the code that you showed in your earlier question. Two words of advice.
If you used indentation correctly, and lined up the curly braces where they're supposed to be, you would see the error almost immediately.
If you stepped through your code with the debugger, and looked at the value of r, you would have found the problem immediately.
Your line r++; is inside the inner loop. This means it gets incremented 3 times for each iteration of the outer loop. You need to move r++; down one line, so that it's outside the inner loop, but inside the outer loop. That way, it will get incremented just once per row, which is what you need.

Categories