I have an element whose value changes dynamically. This is the following:
"Pay 13.61"
I want to save the element 13.61 in a string and compare with 0.
This is how the appium structure looks like:
enter image description here
I want to extract the value of pay.
This is what I did:
String amt = wait.until(
ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#text='Pay '+%s]"))).getText();
logger.info(amt);
logger.info("Cash Pay succeeded!");
Its not able to find the element, can anyone please help. Thanks. :)
Related
I'm trying to get the value of a permission key in Selenium web-driver(java), but I couldn't able to identify the element. Can anyone please help me to identify the element and get the value "4BF12-50763-166E0".
There are 9 span class with same name and it's quite hard to identify the same.
The web page part look like as below and i want to take each of the below elements value like version, status etc. All are present in the span class as shown in the below pic.
Any suggestions will be of great help.
Version 1.2
Status Enabled
Days left 373
Permission KEY 4BF12-50763-166E0
Permission Serial Number 99678905096711
Subscription End Date 2020-08-31
The sample of the HTML is attached.
I have tried this, but it is selecting "Permission KEY" only.
WebElement details = driver.findElement(By.xpath("//b[text()='Permission KEY']"));
String title = details.getAttribute("title");
String label = details.getText();
System.out.println("Title is " +title);
System.out.println("Label is" +label);
You can find your desired text in this case "Permission Key" go the ancestor element and go to the second span that contains the value.
XPath:
String xpath = "//b[text()='Permission Key']/ancestor::dd/span[2]";
WebElement spanElement = driver.findElement(By.xpath(xpath));
spanElement.getText(); // Will give you '4BF12-50763-166E0'
IMPORTANT NOTE: You can change the 'Permission Key' to any other value like for example 'Version' and IF they follow the same pattern, it will work for all, example:
String xpath = "//b[text()='Version']/ancestor::dd/span[2]";
WebElement spanElement = driver.findElement(By.xpath(xpath));
spanElement.getText(); // Will give you '1.2'
You can use preceding-sibling strategy in your xpath, try this bellow :
WebElement key = driver.findElement(By.xpath("//span[#class='pull-right property-value' and ./preceding-sibling::span/b[contains(text(),'Permission KEY')]]"));
System.out.println("Permission KEY : " +key.getText());
Make sure this is the correct class name : #class='pull-right property-value', because i see from your image.
i'm trying to automate a app to do everything that you would have to do by hand. My main goal right now is to get it to type my email in the "Enter your email address" field.
But I've ran into a bump while trying to find the resource-id element in uiautomatorviewer.bat
There is no text in the resource-id field.
Is there anyway to get around this? How else could I find the element to plug into my
driver.findElement(By.id("com.offerup:id/")).sendKeys("xxxxxx#gmail.com");
Use Xpath for this
By path = By.Xpath("//*[#text='Enter your email address']");
driver.findElement(path).sendKeys("xxxxxx#gmail.com");
You xpath aa follows
//android.widget.EditText[#text='Enter your email address']
And use sendKeys() for what ever you need to use.
You can also try like this to find text based on classname and then perform sendKeys
List<WebElement> offerElements=
driver.findElements(By.className("android.widget.EditText"));
for (int i=0;i<offerElements.size();i++) {
if (offerElements.get(i).getText().equals("Enter your email address"))
offerElements.get(i).sendKeys("email text");
}
My requirement is I want to select the particular name from the auto-suggestion in the autocomplete text box.
So here I only know how to get the name by using the mouse down to achieve this But I know it's not a good solution to get that because we don't give the guarantee to the auto-suggestion is same as all the time in the browser.
So if anyone knows how to get the auto-suggested text names for the auto-complete text box in Selenium Web Driver using Junit (Here I am using the Junit in Selenium WebDriver to develop the automation test script).
My code:
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ARROW_DOWN);
Thread.sleep(1000);
driver.findElement("//input[#id='phSearchInput']").SendKeys(KEYS.ENTER);
Here the above code is only working for my correct option is shows as
the second option of the auto-suggested texts.
So that's why I need how to get the text names in the auto-suggestion for the autocomplete text box.
Please the give the solutions as the JUnit Form to my question because I am using the JUnit to develop the automation test script.
Thanks
The auto-suggest box is HTML like anything else on the page. Create a locator that finds the auto-suggest panel and then parse the contents. Once you figure out the structure, you can get the text that you need and then assert that it is correct.
I can't see the full HTML from your screenshot but you can see that the list is contained in an HTML UL. The UL is the container and each LI is a list item in the dropdown. You can use that info to do something like
ul.autoCompleteGroup > li
to get all the list items. I can't see what's inside of there but at some point you should be able to use .getText() to get the auto suggest items. From there you just compare what you pulled off the list to what you are expecting with an Assert.
Please try below code
public void selectAutoCompleteItem(String itemText) {
String xpathPattern = "//div[#id='phSearchInput_autoCompleteBoxId']" +
"//ul/li/a[.='%s')]";
String xpathExp = String.format(xpathPattern, itemText);
driver.findElement(By.xpath(xpathExp)).click();
}
In that case you can use the findelements functionality. So you can say:
List<WebElement> elements = driver.findElements(by.xpath("//a[#class='autoCompleteRowLink']");
And then for
each list item you can use getText to get the exact text. Then you can assert it with the expected values
I'm currently having troubles on locating this element with dynamic id. Here are the screenshots below.
What i have right now is the element of Variables (8) //a[contains(.,'Variables (8)')]. What i need is only the "Variables" since the number 8 is always changing.
Any thoughts on this? Any ideas will be much appreciated. Thanks
First of all 'Variables (8)' is not Id, its text. Id's are not dynamic since they represent unique identifier for the web element. This will look like this (based on your example):
<div class="field" id="fieldId">
As for your question, you can find the element by partial linked text:
driver.findElement(By.partialLinkText("Variables"));
This will give you the a element no meter what the number is.
You can try the below:
driver.findElement(By.cssSelector("a:contains('Variables')"));
If you want the word "Variables" , use the below:
String str = driver.findElement(By.cssSelector("a:contains('Variables')")).getText().split(" ")[0];
Hope this Helps....
What I understand from your question is you want to locate the <a> tag which contains text "Variables".
Try using this xpath:
//div[#class="field"]/a[contains(.,"Variables")]
This xpath will locate the <a> tag after the div tag with class name =field and Contains method with <a> tag will find the element which contains text "Variables"
try this:
String varText = driver.findElement(By.cssSelector("div.triggerFirst>div:nth-child(1)>a")).getText();
I am creating an automatic test for some webs and I'm using WebDriver, TestNG and code that is written in Java. On the page is shown register of categories, in parentheses is number of auctions and i need to get this number as variable, but i don't know, how.
I use the following code:
WebElement number1_1_vse = (driver.findElement(By.linkText("Vše")));
String text_vse1_1 = number1_1_vse.getText();
but output is only: "Vše" without (number of auctions)
link to the website
screenshot -> image with the number
can anyone advise me please?
With linktext you are finding the nested a which text is Vše only. You need to find the li containing the text Vše (949)
Use the following css selector to identify the element
By bycss =By.cssSelector(".list.list-categories>li:first-child");
WebElement number1_1_vse = driver.findElement(bycss );
String text_vse1_1 = number1_1_vse.getText();
WebElement parent = number1_1_vse.findElement(By.xpath(".."));
// which will give you access to <li>
List<WebElement> childs = parent.findElements(By.xpath(".//*"));
// childs.get(1) has the result.
Please try to get the xpath value and then try to search it with below mentioned syntax :
findelementbyxpath("//*[#id="post-form"]/h2")
To get xpath value of and element : right click --> inspect element --> right click copy xpath