I'm trying to locate elements dynamically usign the xpath. However, when I use variable in the xpath, elements are NOT located. However, if I use hardcoded value, elements are located properly.
What am I missing here?
Below xpath locates the elements perfectly:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, 'hp')]"));
whereas, below xpath doesn't locate the elements:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '" + device + "')]"));
Please note that , there are multiple elements matching the above xpath.
I even tried below code but of no use:
driver.findElements(By.XPath(String.Format("//XCUIElementTypeStaticText[contains(#value, '{0}')]", device)));
Any help would be appreciated.
Try do debug this issue as following:
Define the XPath string before calling driver.findElements method, format the string to have the proper value and then pass it into Selenium method, as following:
String xpathLocator = "//XCUIElementTypeStaticText[contains(#value, '%s')]";
xpathLocator = String.format(xpathLocator, device);
driver.findElements(By.xpath(xpathLocator));
As about your existing code.
Here driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '" + device + "')]"));
I can't see the formatting action.
And here driver.FindElements(By.XPath(string.Format("//XCUIElementTypeStaticText[contains(#value, '{0}')]", device)));
it seems to be a wrong syntax.
It should be String.format while you wrote string.Format
Try trimming the spaces as:
driver.findElements(By.xpath("//XCUIElementTypeStaticText[contains(#value, '"+device+"')]"));
Or using String.format() as:
String device = "hp";
driver.findElements(By.xpath(String.format("//XCUIElementTypeStaticText[contains(#value, '%s')]", device)));
Note:
Instead of FindElements() it should be findElements()
Instead of String.Format() it should be String.format()
The issue was with the case mismatch in the value returned by variable. i.e; device variable was returning 'hP' instead of 'hp'.
Corrected the code and it works fine now.
Related
I am working with Java Selenium. And I need to put String line computeEngine.getMachineSeries() from properties inside of my xpath. How can I do this?
#FindBys({#FindBy(xpath = "//md-option//div[#class='md-text ng-binding'][contains(text(), '"+computeEngine.getMachineSeries()+"')]")})
List<WebElement> seriesOptionNOne;
I cant use this code above, because of this error: "Attribute value must be constant". How can I do this in another way?
You can try the following solution:
String myString = computeEngine.getMachineSeries();
#FindBys({#FindBy(xpath = "//md-option//div[#class='md-text ng-binding'][contains(., '"+ myString +"')]")})
List<WebElement> seriesOptionNOne;
Not possible:
https://stackoverflow.com/a/10636320/1387701
There is no way to dynamically generate a string used in an
annotation. The compiler evaluates annotation metadata for
RetentionPolicy.RUNTIME annotations at compile time, but
GENERIC_GENERATED_NAME isn't known until runtime. And you can't use
generated values for annotations that are RetentionPolicy.SOURCE
because they are discarded after compile time, so those generated
values would never be known.
As you are able to see, I have used one dynamic xpath: //td[text()='Discharge Air']/following-sibling::td/span to go from zone1 until zone3, but when I am using gettext() to fetch only 100 but special character °F is also coming. Hence please suggest how to remove this special character °F, because I want only data 100 from this xpath? As you can see in the image, only 1 span is available, so I can't separate span also.
String s = driver.findElement(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span")).getText();
s.replace("°F","");//replace the °F with empty string
Instead of String, can i use List because all these xpath are of same type,hence directly i can write and afterwards i can use for loop for getText().
List s=driver.findElements(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span"));
s.replace("°F","");
Thanks in advance,
List disch_Air = driver.findElements(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span"));
for(int i=0;i<disch_Air.size();i++) {
System.out.println(disch_Air.get(i).getText().replace("°F", ""));
}
}
This is what i want and its working fine thank you so much guys for ur help
Use this:
//first find the elements and save it as you did (with the xpath you posted)
String s = driver.findElement(By.xpath("//td[text()='Discharge Air']/following-sibling::td/span")).getText();
s.replace("°F","");//replace the °F with empty string
and if you see that there are still spaces on your string you can use this to remove them:
s.trim();
I have the following code snippet and the screenshot attached.
String query = "new UiScrollable(new UiSelector().className(\"androidx.recyclerview.widget.RecyclerView\"))" +
".scrollIntoView(new UiSelector().text(\"Test Group\"))";
driver.findElementByAndroidUIAutomator (query).click ();
What I want is to find an element with the text "Test Group" using UISelector, but inside the RecyclerView only (not searching the whole app source). What I get is the element inside search field instead (not in the RecyclerView).
Please advice. I know that I can get all searched elements using findElements(By.id("name")). But I want to use UI selector in this case.
With UiSelector you can use chaining:
String query = "new UiScrollable(resourseIdMatches(\".*recycler_view\")).scrollIntoView(resourseIdMatches(\".*recycler_view\")).childSelector(text(\"Text Group\")))";
In addition new UiSelector... part can be omitted. Appium does support this syntax.
I'm having a strange condition where i'm trying to type into input by using sendKeys , the reuslt is that specific chars doesn't seem to be implemented in the input at all.
What i'm trying to do:
webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)")).sendKeys("(test)");
the result is that input field is now : test) and the missing char is '(' .
If i will try
webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)")).sendKeys("((((((((((")
the result is that the input is empty.
Anyone ever faced this issue before? it is happening on a very specific input in the app, couldn't find anything related to it in the html code.
Thanks in advance.
Edit: I can manually type ( in the input field.
Maybe it's a special character for selenium, have you tried using escape characters? Something like backslash before it if it allows it.
Edit: I found some issue report on github from last year, not sure if they agreed to not fix it. Executing a script to type "(" seems to be an alternative.
Source: https://github.com/seleniumhq/selenium/issues/674
try declaring the key as a string first
String keyToSend = "(test)";
webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)")).sendKeys(keyToSend);
In this case you should try using JavascriptExecutor as below :-
WebElement el = webDriver.findElement(By.id("additionalInfo(token_autocompleteSelectInputId)"));
((JavascriptExecutor)webDriver).executeScript("arguments[0].value = arguments[1]", el, "(test)");
Hope it helps..:)
I'm trying to perform very simple automated test. I created XPath selector in a FirePath, here it is:
//a[#href='http://i.yandex.ru/'][span[contains(.,'ledak.e.v#yandex.by')]]
But Selenium-RC can't locate this element. Code is:
final String StrEmailToTest = "ledak.e.v#yandex.by";
String linkEmailSelector = "//a[#href='http://i.yandex.ru/'][span[contains(.,'"+ StrEmailToTest + "')]]";
selenium.isElementPresent(linkEmailSelector);
and it returns "false"
Could you tell me, what am I doing wrong?
UPD. I've uploaded the *.maft - file here: http://depositfiles.com/files/lhcdh2wtl
Don't be afraid, there are some russian characters on the screen.
Shouldn't your XPath be:
"//a[#href='http://i.yandex.ru/']/span[contains(.,'"+ StrEmailToTest + "')]";
My guess is that selenium is looking for the element even before it's loaded. Is it a dynamically loaded/generated element? If so, use waitForElementPresent(). If not, try changing the method of element identification - use id or name and then try to execute it. To make sure your xpath is correct, in the selenium IDE/plugin for firefox, type the path of the element(issue some random command for command field) and click on "Find Element". If it finds, then selenium has no problem finding it, given that the page/element is loaded or generated. If not, you will have to ask Selenium to wait till the element is loaded.