I have three li elements in ul, as shown in the image http://i.imgur.com/vfZ15Gw.jpg I want to retrieve every li element separately specially the second one.
please give me suggestions
you can use
Elements e = doc.select("ul.about-stats>li");
if(e.size() > 2){
Element firstLi = e.get(0); //to get first
Element secondLi = e.get(1); //to get second
Element thirdLi = e.get(2); //to get third
}
or you can use
e.listIterator();
Related
I am iterating through elements on the website with XPath of each element.
//*[#id="app"]/div[1]/main/div[2]/section/div/section/div[4]/div[1]/div[X]
X values: from 1 to n. Where X is an ordinal number of the element.
xpath.getText() give me the name and price of current item.
I want to download the image also. Class name for the image isn't unique. It same for every element: "shop-card__image-block".
How to access"shop-card__image-block" for every element? I want to take the link to the image and download it with the name and price.
See the images.
First image
Second image
Use element.findElement(By by) or element.findElements(By by) to get child element(s). See https://www.tutorialspoint.com/locating-child-nodes-of-webelements-in-selenium.
Code :
WebDriverWait wait = new WebDriverWait(driver, 30);
int divSize = driver.findElements(By.xpath("//*[#id='app']/div[1]/main/div[2]/section/div/section/div[4]/div[1]/div")).size();
for (int i = 1; i<= divSize; i++) {
WebElement parentDiv = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='app']/div[1]/main/div[2]/section/div/section/div[4]/div[1]/div['"+i+"']")));
WebElement image = parentDiv.findElement(By.xpath(".//descendant::div[contains(#class,'shop-card__image-block')]"));
// now image is a web element, you can perform a click on it like image.click(); or any other operation.
}
now image is a web element, you can perform a click on it like image.click(); or any other operation.
trust this helps.
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);
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
I am trying to implement some code using Selenium Webdriver using Java.
Basically, I have a website with a text box. Once user enter the first letter, based on that a value will be displayed(using AJAX). I need to select the particular value, which i mentioned in send keys .
WebElement fromCity = driver.findElement(By.id("pickUpLocation"));
fromCity.sendKeys("A Ma Temple / 媽閣");
Thread.sleep(2000);
WebElement ajaxContainer1 = driver.findElement(By.className("txt-box ng-touched ng-dirty ng-valid"));
WebElement ajaxHolder1 = ajaxContainer1.findElement(By.tagName("ul"));
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
for (WebElement value1 : ajaxValues1) {
if (value1.getText().equals("A Ma Temple ")) {
((WebElement)ajaxValues1).click();
break;
}
}
After you send keys.your Ajax value should be retrieved in a box related to you keyword search.You need to get the complete box.and fetch each one as you have done in for loop .get the text and compare it with your expected text and click where this condition is true.
What is that line for before thread.sleep()
I think u can try selecting through index. It should be like this
List<WebElement> ajaxValues1 = ajaxHolder1.findElements(By.tagName("li"));
Select dropdown= new Select(ajaxValues1);
dropdown.selectByIndex(0);
dropdown.selectByIndex(1);
dropdown.selectByIndex(2);
0 represents the first element in the dropdown. Based on index number of that element, feed the corresponding number in selectByIndex(0)
Let me know if this helps. Thanks
hello guys i am try to print the output of two element data simultaneously
Document document2 = Jsoup.parse(webPage2);
Document document22 = Jsoup.parse(webPage2);
Elements links2 = document2.select("a.yschttl");
Elements links22 = document22.select("div.abstr");
can we include both a.yschttl and div.abstr or...
for (Element link2 : links2) {
out.println(link2);
}
can we include two say links2 and links22 in same for loop...
or how to achive it...
You can do something like:
for (int i = 0; i < links2.size(); i++) {
out.println(links2.get(i));
out.println(links22.get(i));
}
But in this case you will get IndexOutOfBoundsException if size of links22 higher than size of links2.
What do you want to achieve?
If you are just trying to select both at the same time, you can do something like this:
for (Element link : document.select("a.yschttl, div.abstr") {
out.println(link);
}
If you are trying to make two selections and outputting those values in tandem, you will have to do something like #vacuum suggests, but being careful of the lengths of the lists.
A side note, you don't have to parse the document twice to make two selections. You can parse once and select twice.