Selenium Webdriver - Using a stored string in a CSS locator - java

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));

Related

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")));

Java Selenium - Find string text from multiple divs with same class name using xpath

i'm hoping you can help me.
I've been going through all sorts of forums and questions on here on how to cycle through multiple divs with the same class name using xpath queries. I'm fairly new to WebDriver and Java so I'm probably not asking the question properly.
I have a table where i'm trying to identify the values within and ensure they're correct. each field has the same class identifier, and i'm able to successfully pull back the first result and confirm via report logging using the following
String className1 = driver.findElement(By.xpath("(//div[#class='table_class'])")).getText();
Reporter.log("=====Class Identified as "+className1+"=====", true);
However, when i then try and cycle through (I've seen multiple answers saying to add a [2] suffix to the xpath query) i'm getting a compile error:
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'])")[2]).getText();
Reporter.log("=====Class Identified as "+className2+"=====", true);
The above gives an error saying "The type of the expression must be an array type but it resolved to By"
I'm not 100% sure how to structure this in order to set up an array and then cycle through.
Whilst this is just verifying field labels for now, ultimately i need to use this approach to verify the subsequent data that is pulled through, and i'll have the same problem then
You are getting the error for -
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'])")[2]).getText();
because you are using index in wrong way modify it to -
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'])[2]")).getText();
or
String className2 = driver.findElement(By.xpath("(//div[#class='table_class'][2])")).getText();
And the better way to do this is -
int index=1;
List <WebElement> allElement = driver.findElements(By.xpath("(//div[#class='table_class'])"));
for(WebElement element: allElement)
{
String className = element.getText();
Reporter.log("=====Class Identified as "+className+""+index+""+"=====", true);
index++
}

Appium : Verify relative element presence

Can you give a suggestion(please see pic) how can I check if selectIndicator is present on one block then I should choose another one. I know how to check if that element isPresent on whole page, but I need to find if it present on particular element. In my example I have Living Room chosen, and I need to check if DVR not chosen -choose that one. Any idea how can I do it? I was trying to check this way, but no luck:
WebElement element= driver.findElementByAccessibilityId("First element").findElementByAccessibilityId("Second element");
[http://i.stack.imgur.com/F98DM.png]
If I am not getting you wrong you want to implement a self defined data structure for an appropriate solution. That could be something similar to this :
public class DVRList {
//declare components required to comprise one item
private String dvrOptionText ;
private boolean dvrOptionCheck ;
// implement setter..getter for these two
}
...in some method set the value using the logic
DVRList dvrlist = new DVRList();
WebElement parentOfBoth = driver.findElement(By.xpath("
//android.widget.RelativeLayout[1]/android.widget.R‌​elativeLayout[1]");
String text = parentOfBoth.findElementByAccessibilityId("First element").getText();
dvrlist.setdvrOptionText(text);
if(isElement(parenOfBoth.findElementByAccessibilityId("Second element"))
dvrlist.setdvrOptionCheck(true);
else dvrlist.setdvrOptionCheck(false);
and thereafter you can use these parameters accordingly.
Note : Parameters and approach are generalised and should be modified for serving the exact purpose.

Using multiple criteria to find a WebElement in Selenium

I am using Selenium to test a website, does this work if I find and element by more than one criteria? for example :
driverChrome.findElements(By.tagName("input").id("id_Start"));
or
driverChrome.findElements(By.tagName("input").id("id_Start").className("blabla"));
No it does not. You cannot concatenate/add selectors like that. This is not valid anyway. However, you can write the selectors such a way that will cover all the scenarios and use that with findElements()
By byXpath = By.xpath("//input[(#id='id_Start') and (#class = 'blabla')]")
List<WebElement> elements = driver.findElements(byXpath);
This should return you a list of elements with input tags having class name blabla and having id id_Start
To combine By statements, use ByChained:
driverChrome.findElements(
new ByChained(
By.tagName("input"),
By.id("id_Start"),
By.className("blabla")
)
)
However if the criteria refer to the same element, see #Saifur's answer.
CSS Selectors would be perfect in this scenario.
Your example would
By.css("input#id_start.blabla")
There are lots of information if you search for CSS selectors. Also, when dealing with classes, CSS is easier than XPath because Xpath treats class as a literal string, where as CSS treats it as a space delimited collection
Based #George's repply, the same code for C# :
//reference
using OpenQA.Selenium.Support.PageObjects;
...
int allElements = _driver.FindElements(new ByChained(
By.CssSelector(".sc-pAyMl.cnszJw"),
By.Id("base-field")
)).Count();

Problems with element locating with XPath in Selenium-RC

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.

Categories

Resources