Problems with element locating with XPath in Selenium-RC - java

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.

Related

Appium NOT locating element when java variable is used in xpath

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.

Find element by text inside another element using UISelector query

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.

findElement that is generated HTML during runtime

I'm trying to find an element that is generated from the PCA Predict API, found in this link here. http://www.pcapredict.com/en-gb/address-capture-software/
The code I have at the moment is as follows but it throws an timeout exception due to it not finding any elements. Yet the xpath is correct as I have checked it in developer tools.
By PCA = By.id("inputPCAnywhere");
driver.findElement(PCA).clear();
driver.findElement(PCA).sendKeys(ValidPostcode);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='pcaitem pcafirstitem']")));
driver.findElement(By.xpath("//div[#class='pcaitem pcafirstitem']")).click();
The element is visible on the page, and developer tools only returns one result that that xpath, there is no ID's to find it by.
It looks like that the first item is getting "selected" by default leading to it's class value being equal to the following:
<div class="pcaitem pcafirstitem pcaselected"...>...</div>
All other following results have only pcaitem class, but none have a pcaitem pcafirstitem class value.
In other words, your problem is the strict class match. I would improve the locator to have a partial match on the class attribute. For instance, with a CSS selector:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".pcaitem.pcafirstitem")));

Selenium Webdriver - Using a stored string in a CSS locator

I am trying to write an automation step which clicks on a link, the locator which I need to use to target the link is matching to the end of the title attribute, the reason being there may be multiple links of the same type, some of which will be named the same, this section of the title I am looking at is the only bit within these links guaranteed to be unique (not my call, this is an existing system in place I am writing tests for). I have no issue finding the locator of the link using this method.
Note: some elements of the code have been amended due to data security restrictions of my employer.
#FindBy(css = "#id .Content form > a[title$='12345678']")
WebElement linkName;
However the reference number at the end of the title that I'm looking for may change, depending on other data inputs, and it will be used in multiple places thorughout my automation suite, so what I am trying to do is store that number as a String at the very start of the suite and have the locator, and any other areas which need it, reference it, which is where my trouble begins. I seem to be unable to get the locator to work referencing the string.. When I try this I keep getting errors, usually syntax errors.
#FindBy(css = "#id .Content form > a[title$='%s']", titleVariable)
WebElement linkName;
I have tried rearranging this multiple times but can't seem to get it into a working format. Any suggestions would be welcome.
I apologise if this seems unclear, As stated above due to the nature of my employers business I can't give too many specifics due to data security restrictions so have had to explain things in a more round about way than I could have.
first of all, u must have to use constant variable here. so use code like below:
final String titleVariable = "ur title";
#FindBy(css = "#id .Content form > a[title$='"+titleVariable+"']")
WebElement linkName;
If you have WebDriver object in this class you can do this:
String titleVariable = "Your Title";
String cssSelector = "#id .Content form > a[title$='" + titleVariable + "']";
WebElement linkName = driver.findElement(By.cssSelector(cssSelector));

XPath- Getting Element from a table having dynamic ID

I'm trying to automate my Test Cases using Selenium for an OBIEE application. Now, I need to read a value from a tabular report generated. The problem is, the ID of the last cell where the total is, keeps on changing.
For example- Currently the id is: db_saw_9270_6_1610_0.
After refreshing, the ID becomes something else. The 4 numbers in between (9270) changes. The remaining bit are the same. I'm using the following logic to capture this element:
driver.findElement(By.xpath(".//*[contains(#id, '_6_1610_0')]")).getText();
But, it is returning org.openqa.selenium.NoSuchElementException: Unable to locate element:
Please tell me where did I go wrong and what should I do?
you can try starts-with and substring (as a substitute for xpath 2.0 methdod ends-with):
string xpath = "//*[starts-with(#id, 'db_saw_') and substring(#id, string-length(#id) - 8) = '_6_1610_0']"
driver.findElement(By.xpath(xpath)).getText();
You can try below xpath:-
driver.findElement(By.xpath("//*[starts-with(#id, 'db_saw')]")).getText();
driver.findElement(By.CSSselector("a[id*='_6_1610_0']")).getText();
Note: the a represents a html element. If your id is in a element then you have to replace a by table.
Check this out for more examples with css selector

Categories

Resources