How to distinguish between table elements with identical names? Selenium Java - java

I need to get to the second table's element. However since the web page contains two tables, I always end up getting an element from the first table.
Here is the code:
driver.get("http:.............");
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
String el = driver.findElement(By.xpath("//tr[1]/td[2]")).getText();
System.out.println(el);
//I need to get the following value: $0.00564000. How can I overcome this issue?

Try this:
String el = driver.findElements(By.xpath("//tr[2]/td[2]")).get(1).getText();

Related

How to get all the values against a particular tag in webpage

Is there any way to get all the values displaying against "BRAND" .
I want to store all the value that will display for the BRAND and sore those value in the collection so that I can compare with one more collection and find which value will get match in between those.
below is the screenshot . Please give some input .
Probably the simplest way:
List<String> brands = new ArrayList<String>();
List<WebElement> fields = driver.findElements(By.className("asset-page__field-value"));
for (WebElement field: fields) {
brands.add(field.getText());
}

select element in typeahead with changing ID java- selenium

I have a typeahead drop down. the elements in the list are populated depending upon what I type. All the elements that populate have the ID something like "typeahead-123" where "typeahead" remains same but "123" keeps changing for all the elements' id. Now how do i capture all those elements that are populated in the drop down in a java List.
List<WebElement> options = driver.findElements(By.xpath("//a[starts-with(#id, 'typeahead')]"));
for (WebElement option1 : options) {
if (option1.getText().equals(mychoice))
option1.click();
return "Pass";
}
I just found an answer to the question myself. There was an error in my code.
driver.findElements(By.xpath("//a[starts-with(#id, 'typeahead')]"))
this should have been
driver.findElements(By.xpath("//*[starts-with(#id, 'typeahead')]"))
I don't what exactly has happened but apparently It solved the problem now I have all the elements of the typeahead dropdown in my List. Thanks.

Java Couchbase Querying to find a document's ID?

I'm new to couchbase. I'm using Java for this. I'm trying to remove a document from a bucket by looking up its ID with query parameters(assuming the ID is unknown).
Lets say I have a bucket called test-data. In that bucked I have a document with ID of 555 and Content of {"name":"bob","num":"10"}
I want to be able to remove that document by querying using 'name' and 'num'.
So far I have this (hardcoded):
String statement = "SELECT META(`test-data`).id from `test-data` WHERE name = \"bob\" and num = \"10\"";
N1qlQuery query = N1qlQuery.simple(statement);
N1qlQueryResult result = bucket.query(query);
List<N1qlQueryRow> row = result.allRows();
N1qlQueryRow res1 = row.get(0);
System.out.println(res1);
//output: {"id":"555"}
So I'm getting a json that has the document's ID in it. What would be the best way to extract that ID so that I can then remove the queryed document from the bucket using its ID? Am I doing to many steps? Is there a better way to extract the document's ID?
bucket.remove(docID)
Ideally I'd like to use something like a N1q1QueryResult to get this going but I'm not sure how to set that up.
N1qlQueryResult result = bucket.query(select("META.id").fromCurrentBucket().where((x("num").eq("\""+num+"\"")).and(x("name").eq("\""+name+"\""))));
But that isn't working at the moment.
Any help or direction would be appreciated. Thanks.
There might be a better way which is running this kind of query:
delete from `test-data` use keys '00000874a09e749ab6f199c0622c5cb0' returning raw META(`test-data`).id
or if your fields has index:
delete from `test-data` where name='bob' and num='10' returning raw META(`test-data`).id
This query deletes the specified document with given document key (which is meta.id) and returns document id of deleted document if it deletes any document. Returns empty if no documents deleted.
You can implement this query with couchbase sdk as follows:
Statement statement = deleteFrom("test-data")
.where(x("name").eq(s("bob")).and(x("num").eq(s("10"))))
.returningRaw(meta(i("test-data")).get("id"));
You can make this statement parameterized or just execute like that.

Selenium Extract the ID of an element found via text in Java

I have a set of Cucumber tests I wish to execute using selenium and Chrome. Most of my test work fine. I am currently trying to parametertize one particular step so that anyone using the Test can simply name the elements they want and the test will find them.
The Cucumber test looks something like this:
When I go to the "Inventory" / "Inventory" application
And I search the "Description" field for "a"
I was able to get the when statement to parameterized very easily, but the And Statement is giving me a bit more trouble. The page itself has several sets of tables with a consistent id scheme The xpath for the header with the word "Description" looks like this:
//label[#id='m6a7dfd2f_ttrow_[C:2]_ttitle-lb']
and the input field has this xpath
//td[#id='m6a7dfd2f_tfrow_[C:2]-c']/input
For reference the immediate next input field, located below the label room, has this xpath
//td[#id='m6a7dfd2f_tfrow_[C:3]-c']/input
So each label and input field have the same id scheme and only the number following "c:" seems to change. That number is the same for both the label and it's corresponding input value. I would like to some how scan the page for the word "Description" and extract the ID of the element associated with it and then truncate it down to the number. I already have variables set up to hold the rest and it would hopefully as follows:
#When("^I search the \"([^\"]*)\" field for \"([^\"]*)\"$")
public void i_search_the_field_for(String searchField, String searchItem) throws Throwable {
String baseinputXPath = "//td[#id='m6a7dfd2f_tfrow_[c:";
String endinputXPath = "]-c']/input";
String elementNumber = "The return of some sort of method based on searchField";//TODO
driver.findElement(By.xpath(baseinputXPath + elementNumber + endinputXPath)).clear();
driver.findElement(By.xpath(baseinputXPath + elementNumber + endinputXPath)).sendKeys(searchItem);
driver.findElement(By.id("search_button_img")).click();
If there is a way to pull out that number based only on the description text, that would be super helpful.
Use a xpath to search for the WebElement with word 'Description' in a label tag.
"//label[text(),'Description']" or "//label[.='Description']" or "//label[contains(text(),'Description')]"
Use the getAttribute("id") to get the id.

Unable to retrive Projection/Multi- Relantion field Requests.Custom_SFDCChangeReqID2 using versionone java sdk

I have been trying to retrieve information from querying a specific Asset(Story/Defect) on V1 using the VersionOne.SDK.Java.APIClient. I have been able to retrieve information like ID.Number, Status.Name but not Requests.Custom_SFDCChangeReqID2 under a Story or a Defect.
I check the metadata for:
https://.../Story?xsl=api.xsl
https://.../meta.V1/Defect?xsl=api.xsl
https://.../meta.V1/Request?xsl=api.xsl
And the naming and information looks right.
Here is my code:
IAssetType type = metaModel.getAssetType("Story");
IAttributeDefinition requestCRIDAttribute = type.getAttributeDefinition("Requests.Custom_SFDCChangeReqID2");
IAttributeDefinition idNumberAttribute = type.getAttributeDefinition("ID.Number")
Query query = new Query(type);
query.getSelection().add(requestCRIDAttribute);
query.getSelection().add(idNumberAttribute);
Asset[] results = v1Api.retrieve(query).getAssets();
String RequestCRID= result.getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= result.getAttribute(idNumberAttribute).getValue().toString();
At this point, I can get some values for ID.Number but I am not able to retrieving any information for the value Custom_SFDCChangeReqID2.
When I run the restful query to retrieve information using a browser from a server standpoint it works and it does retrieve the information I am looking for. I used this syntax:
https://.../rest-1.v1/Data/Story?sel=Number,ID,Story.Requests.Custom_SFDCChangeReqID2,Story.
Alex: Remember that Results is an array of Asset´s, so I guess you should be accessing the information using something like
String RequestCRID= results[0].getAttribute(requestCRIDAttribute).getValue().toString();
String IdNumber= results[0].getAttribute(idNumberAttribute).getValue().toString();
or Iterate through the array.
Also notice that you have defined:
Asset[] results and not result
Hi thanks for your answer! I completely forgot about representing the loop, I was too focus on the retriving information part, yes I was actually using a loop and yes I created a temporary variable to check what I was getting from the query in the form
Because I was getting the variables one by one so I was only using the first record. My code works after all. It was just that What I was querying didn't contain any information of my use, that's why I was not finding any. Anyway thanks for your comment and observations

Categories

Resources