I am using Appium and I want to print names of the elements in the list.
I am using following code
List<WebElement> list = getDriver().findElementsByXPath(getLocator(Locators.MY_ITEM));
List<String> strings = new ArrayList<>();
for (WebElement object : list) {
String text = object.getText();
logger.info(text);
if (!text.isEmpty())
strings.add(text);
}
But I am getting text always as empty.
What is the suggested approach over here.
Note each element is of type UIACollectionCell in case of iOS and on Android //android.widget.TextView[#text='%s']
From what I understand, you should be getting the text from the text attribute, replace:
String text = object.getText();
with:
String text = object.getAttribute("text");
Related
this is the structure of the unodered list
structure of the unodered list
i want to get the value of "var" and "time" to two strings and save them in a ArrayList
List<String[]> finalResult = new ArrayList<>();
This is what i tried
List<WebElement> typeElements =driver.findElements(By.xpath("//*[#id=\"container\"]/section[2]/div/div[2]/div[2]/ul/li"));
for(int i=0;i<typeElements.size();i++){
WebElement typeSingle = typeElements.get(i);
String stName = typeSingle.findElement(By.xpath("//div/a/var")).getText();
String stTime = typeSingle.findElement(By.xpath("//div/a/time")).getText();
String singleType[] =stName,stTime};
finalResult.add(singleType);
}
im getting total of 13 items in the
finalResult
variable. but all the items are same
{"title1","00:23"}
{"title1","00:23"}
{"title1","00:23"}....
i want them to be like this
{"title1","00:23"}
{"title2","00:31"}.....
what am i doing wrong?
***Edit*
On each itterations inside the for loop typeSingle
is refering to different id's,
but stName , stTime variables are same
To get child element with xpath you need to use ./:
String stName = typeSingle.findElement(By.xpath("./div/a/var")).getText();
String stTime = typeSingle.findElement(By.xpath("./div/a/time")).getText();
Or use css selector:
String stName = typeSingle.findElement(By.cssSelector("var")).getText();
String stTime = typeSingle.findElement(By.cssSelector("time")).getText();
Changing this
String stName = typeSingle.findElement(By.xpath("//div/a/var")).getText();
String stTime = typeSingle.findElement(By.xpath("//div/a/time")).getText();
to this solved the issue
String stName = typeSingle.findElement(By.xpath("div/a/var")).getText();
String stTime = typeSingle.findElement(By.xpath("div/a/time")).getText();
driver.findElement(By.id("lnkLogin")).click();
WebElement cmp = driver.findElement(By.id("txtCompanySearch"));
cmp.sendKeys("Demo Company");
driver.findElements(By.xpath(".//*[#id=\"TenantTBL\"]/tbody/tr[5]/td")).get(0).click();
This code above worked for me but it works as static xpath index for selecting,
but I want to get all rows which match my sendKeys value.
I have tried this but this is not working
ArrayList<Integer> numbers = new ArrayList<Integer>();
By elems = By.xpath(".//*[#id=\"TenantTBL\"]/tbody/tr");
WebElement select = driver.findElement(elems);
List<WebElement> matches = select.findElements(By.xpath(".//*[#id=\"TenantTBL\"]/tbody/tr"));
List<String> currentVals = new ArrayList<>();
for (WebElement match : matches) {
currentVals.add(match.getText());
}
When using . in xpath you are using current context, so
WebElement select = driver.findElement(By.xpath(".//*[#id=\"TenantTBL\"]/tbody/tr"));
List<WebElement> matches = select.findElements(By.xpath(".//*[#id=\"TenantTBL\"]/tbody/tr"));
Is the equivalent of
List<WebElement> matches = select.findElements(By.xpath(".//*[#id=\"TenantTBL\"]/tbody/tr//*[#id=\"TenantTBL\"]/tbody/tr"));
Basically you are looking for an element which is a child of itself. Just locate the elements directly
By elems = By.xpath(".//*[#id=\"TenantTBL\"]/tbody/tr");
List<WebElement> matches = driver.findElements(elems));
i am using maps.mapmyindia.com, when i am searching for a state or city it shows a list of search list now i need to print the index of the exact match of my search, i want to print that index on the consol, thanks in advance. i have tried this code
WebElement list = driver.findElement(By.xpath("//div[#class='directions-route-text']"));
String str = "Nehru Place";
WebElement li = list.findElement(By.xpath("*[. = str]"));
List<WebElement> myElements = driver.findElements(By.tagName("li"));
System.out.println(myElements.indexOf(li));
List<WebElement> myElements = list.findElements(By.tagName("li"));
ArrayList<String> as = new ArrayList<String>();
int listsize= myElements.size();
for(int i=0;i<listsize;i++)
{
as.add(myElements.get(i).getText());
}
System.out.println(as.indexOf(str));
Try above code:
I have a form which I have to read with jsoup, it contains several fields including checkboxes and comboboxes (select inputs).
I am reading there values with following code -
Element campaignForm = doc.getElementById("Campaign");
Elements allInputFields = campaignForm.getElementsByTag("input");
Elements allSelections = campaignForm.getElementsByTag("select");
Map<String, String> postData = new HashMap<String, String>();
for(Element selectField:allSelections){
postData.put(selectField.attr("name"), selectField.attr("value"));
}
for(Element inputField:allInputFields){
if(inputField.attr("type").equalsIgnoreCase("checkbox")){
postData.put(inputField.attr("name"), inputField.attr("checked").equalsIgnoreCase("checked")?"1":"0");
}else{
postData.put(inputField.attr("name"), inputField.attr("value"));
}
}
So when I print the postData Map, it gives correct values for text input fields but for checkboxes and dropdown(comboboxes) it is not working. Please let me know if there is different way to handle checkboxes and select inputs in jsoup.
EDIT:
Checkboxes I got working with help of comment, but select input still not working.
Thanks in advance.
I got it working with following code -
for(Element selectField:allSelections){
String nameField = selectField.attr("name");
String valueField = "";
Elements allOptions = selectField.getElementsByTag("option");
for(Element opt:allOptions){
if(opt.attr("selected").equalsIgnoreCase("selected")){
valueField = opt.attr("value");
break;
}
}
postData.put(nameField, valueField);
}
for(Element inputField:allInputFields){
if(inputField.attr("type").equalsIgnoreCase("checkbox")){
postData.put(inputField.attr("name"), inputField.attr("checked").equalsIgnoreCase("checked")?"1":"0");
}else{
postData.put(inputField.attr("name"), inputField.attr("value"));
}
I have the following code. I am using the jsoup library to retrieve the URLs from a website; after that, I am checking if the URLs contain the keyword I want, and list them in another string. My problem is that I am not able to retrieve only one URL.
Have a look at my code:
// Get the webpage and parse it.
org.jsoup.nodes.Document doc = Jsoup.connect("http://www.examplepage").get();
// Get the anchors with href attribute.
// Or, you can use doc.select("a") to get all the anchors.
org.jsoup.select.Elements links = doc.select("a[href]");
// Iterate over all the links and process them.
for (org.jsoup.nodes.Element link : links) {
String scrapedlinks += link.attr("abs:href")+"\n" ;
String scrapedlinks3 ="";
}
String[] links2 = links.split("\n");
for (String newlink : hulklinks ) {
if (newlink("mysearchterm")) {
scrapedlinks3 +=newlink ;
String[] scrapedlines = scrapedlinks3.split("\n" );
}
}
I think it will be easier if you directly store your urls in an Arraylist:
Arraylist<String> urls = new Arraylist<String>();
for (org.jsoup.nodes.Element link : links)
urls.add(link.attr("abs:href"));
After this you can easy access them with
urls.get(i);