suggest an inexpensive XPATH for the below code - xpath

The below is the sample selenium code. What will be efficient XPATH for this or a better regular expression which can be used
#FindBy(xpath = ".//*[#id='Insurance_number' or #id='id_insurance_number_text' or #id = 'sInsuranceNumber']")

You could try the following variant:
#FindBy(xpath = ".//*[#id != ''][contains('Insurance_number;id_insurance_number_text;sInsuranceNumber', #id)]")
It is more concise, but I'm not sure if it yields better performance.

Replace your xpath with ".//*[ends-with(#id,'nsurance')]"
It assume it works for you.

Related

Iterate through all elements xpath using #Findby in java

I have started using Page factory, and now i need to provide xpath in #FindBy. It will be great if someone can provide any suggestion or reference on how to pass variable in xpath using #Findby.
Element which i want to replace with #Findby annotation
for(i=1; i <= liElements.size(); i++) {
WebElement linkElement = driver.findElement(By.xpath("//li[" + i + "]/div//a[contains(#class, 'btn-mini')]"));
linkElement.click();
}
Thanks a lot in advance for your help.
Regarding your question around parametrised FindBy - this is not possible as annotations are constant values. Take a look here - Can the annotation variables be determined at runtime?
In this particular case you can find a list of the elements in FindBy:
#FindBy(xpath = "//li/div//a[contains(#class, 'btn-mini')]")
private List<WebElement> links;
Then you can iterate through them like this:
for(WebElement link : links) {
link.click();
}
So you will click links found by that xPath one by one.

How do I get the text from an element that has two attributes?

I have a code such as:
<out:a>
<out:Name Type="First" TypeCode="Best">JAE</out:Name>
</out:a>
When I gave the xpath expression as
//*[name()='out:Name'],
I got the result as
<out:Name Type="First" TypeCode="Best" xmlns:out3="abc" xmlns:out2="def" xmlns:out1="ghi" xmlns:out="jkl">JAE</out:Name>
I would like to get the value JAE using xpath expression. Could someone help me in that please?
Add text() on the end:
//*[name()='out:Name']/text()
Or
//out:Name/text()
It depends on the tool you use.
With java / Xpath and evaluate, your xpath expression works well:
expression=" //*[name()='out:Name']";
String value = xPath.evaluate(expression, document);
System.out.println("EVALUATE:"+value); // => EVALUATE:JAE

Selenium WebDriver findElement(By.xpath()) not working for me

I've been through the xpath tutorials and checked many other posts, hence I'm not sure what I'm missing. I'm simply trying to find the following element by xpath:
<input class="t-TextBox" type="email" test-id="test-username"/>
I've tried many things, such as:
element = findElement(By.xpath("//[#test-id='test-username']"));
The error is Expression is not a legal expression.
I'm using Firefox on MacBook
Any suggestion would be greatly appreciated.
element = findElement(By.xpath("//*[#test-id='test-username']"));
element = findElement(By.xpath("//input[#test-id='test-username']"));
(*) - means any tag name.
You should add the tag name in the xpath, like:
element = findElement(By.xpath("//input[#test-id='test-username']");
your syntax is completely wrong....you need to give findelement to the driver
i.e your code will be :
WebDriver driver = new FirefoxDriver();
WebeElement element ;
element = driver.findElement(By.xpath("//[#test-id='test-username']");
// your xpath is: "//[#test-id='test-username']"
i suggest try this :"//*[#test-id='test-username']"
You missed the closing parenthesis at the end:
element = findElement(By.xpath("//[#test-id='test-username']"));
Just need to add * at the beginning of xpath and closing bracket at last.
element = findElement(By.xpath("//*[#test-id='test-username']"));
You can use contains too:
element = findElement(By.xpath("//input[contains (#test-id,"test-username")]");
You haven't specified what kind of html element you are trying to do an absolute xpath search on. In your case, it's the input element.
Try this:
element = findElement(By.xpath("//input[#class='t-TextBox' and #type='email' and #test-
id='test-username']");
Correct Xpath syntax is like:
//tagname[#value='name']
So you should write something like this:
findElement(By.xpath("//input[#test-id='test-username']"));

How to get part of string after some word with Ruby?

I have a string containing a path:
/var/www/project/data/path/to/file.mp3
I need to get the substring starting with '/data' and delete all before it. So, I need to get only /data/path/to/file.mp3.
What would be the fastest solution?
'/var/www/project/data/path/to/file.mp3'.match(/\/data.*/)[0]
=> "/data/path/to/file.mp3"
could be as easy as:
string = '/var/www/project/data/path/to/file.mp3'
path = string[/\/data.*/]
puts path
=> /data/path/to/file.mp3
Using regular expression is a good way. Though I am not familiar with ruby, I think ruby should have some function like "substring()"(maybe another name in ruby).
Here is a demo by using javascript:
var str = "/var/www/project/data/path/to/file.mp3";
var startIndex = str.indexOf("/data");
var result = str.substring(startIndex );
And the link on jsfiddle demo
I think the code in ruby is similar, you can check the documentation. Hope it's helpful.
Please try this:
"/var/www/project/data/path/to/file.mp3".scan(/\/var\/www(\/.+)*/)
It should return you all occurrences.

Selenium Webdriver + Ruby regex: Can I use regex with find_element?

I am trying to click an element that changes per each order like so
edit_div_123
edit_div_124
edit_div_xxx
xxx = any three numbers
I have tried using regex like so:
#driver.find_element(:css, "#edit_order_#{\d*} > div.submit > button[name=\"commit\"]").click
#driver.find_element(:xpath, "//*[(#id = "edit_order_#{\d*}")]//button").click
Is this possible? Any other ways of doing this?
You cannot use Regexp, like the other answers have indicated.
Instead, you can use a nifty CSS Selector trick:
#driver.find_element(:css, "[id^=\"edit_order_\"] > div.submit > button[name=\"commit\"]").click
Using:
^= indicates to find the element with the value beginning with your criteria.
*= says the criteria should be found anywhere within the element's value
$= indicates to find the element with with your criteria at the end of the value.
~= allows you to find the element based on a single criteria when the actual value has multiple space-seperated list of values.
Take a look at http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ for some more info on other neat CSS tricks you should add to your utility belt!
You have no provided any html fragment that you are working on. Hence my answer is just based on the limited inputs provided your question.
I don't think WebDriver APIs support regex for locating elements. However, you can achieve what you want using just plain XPath as follows:
//*[starts-with(#id, 'edit_div_')]//button
Explanation: Above xpath will try to search all <button> nodes present under all elements whose id attribute starts with string edit_div_
In short, you can use starts-with() xpath function in order to match element with id format as edit_div_ followed by any number of characters
No, you can not.
But you should do something like this:
function hasClass(element, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
return re.test(element.className);
}
This worked for me
#driver.find_element(:xpath, "//a[contains(#href, 'person')]").click

Resources