Ignore case Xpath #Name attribute c# Selenium/Appium - selenium

I have a C# selenium/Appium project where I need to find a desktop Application window By.Xpath("").
This works:
By.XPath("//*[#Name='ASDASD']");
However, some builds of the app have the window name be "ASDasd", which causes the Xpath above to not find the window element and the test fails.
Is it possible to Ignore the case of the #Name attribute whether it be "ASDASD", "ASDasd" or something else?
I did try using the XPath translate function, but I am not able to find the element, I assume I am doing it wrong.
What I tried:
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
or
By.XPath("//*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd']")
or
By.XPath("//*[#Name='translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'asdasd'']")
or
By.XPath("//*[#Name='translate(asdasd,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')']")
Maybe some other variations too, but I could not get it to work.
Some of the examples may have invalid formatting.
While other seems to be valid but could not find the element and it would timeout.
UPDATE:
Thank you for the assistance, this worked:
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')='asdasd']");
However, it added 60 seconds to the test somehow, it seems to stall for 60 seconds on one of the places where it looks for the main window.
Thanks for the help!
Regards

name() gives you the name of context node. In this case (//*), the name of whatever element you are currently looking at. You meant to write #Name, i.e. the attribute that happens to be called Name.
By.XPath("//*[translate(#Name, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'asdasd']")
Using translate() is clunky and fails when the search string contains unanticipated characters.
Unfortunately, there is no lower-case() function in XPath 1.0. But you can work around this limitation with the help of the host language (such as C#).
The following will dynamically create an XPath expression which finds arbitrary values case-insensitively:
var searchValue = "asdasd";
var uc = searchValue.ToUpperInvariant();
var lc = searchValue.ToLowerInvariant();
var xpath = $"//*[translate(#Name, '{uc}', '{lc}') = '{lc}']";
// -> "//*[translate(#Name, 'ASDASD', 'asdasd') = 'asdasd']"

Related

clicking on dynamic values in a web table in selenium

Below is my Table structure:
I want to click on the first cell of "policyno " column only if value is not empty
How do I achieve this?
Since you didn't specified, I have no idea what programming language do you need, but the idea is the same for all. I've used C# for the code below:
Get all the policy number elements. This can be easily achieved by getting the elements by xpath. In your case, I expect the lblPolicyNumber to be present in all:
IList allPolicyElems = driver.FindElements(By.Xpath(".//*[contains(#id,'lblPolicyElements')]"));
Now, you have 2 options to click on the element you need. First, you click on an element using his position in the list: allPolicyElems[0].Click(); (not the best way to do it) or by using LINQ (or lambda expressions for Java) to get the element by the text (perhaps you have the text stored in a variable from some previous actions): allPolicyElems.FirstOrDefault(t => t.Text == "your_text_here").Click();
This can be further expanded by applying the same logic in case you need to get the correct element by knowing other cell values from the same row.
Try using this.
WebElement elem = driver.findElement(By.xpath("//table/tbody/tr[2]/td[3]/span/a"));
if(!(elem.getText().trim().equals(""))){
elem.click();
}

Getting description using selenium xpath

I am trying to get the job description for job search page indeed.com This is how it looks like
Provide technical leadership around
QA
automation to IT teams. Work with various team to promote
QA
processes, practices and standardization....
Any idea how can I get that description? I tried the following:
//span[contains(#class,'summary')]
That does not give me the text description. Should I xpath or is there any other solution? Thanks in advance for your time.
This XPath are correct.
//span[contains(#class,'summary')]
//span[#class='summary']
I'm a Python guy, But I translated it to Java. You can do:
element = driver.findElement(By.name("summary"));
element = driver.findElement(By.className("summary"));
element = driver.findElement(By.cssSelector('span[class="summary"]');
And remember that If you want the element text, every element has the method .getText(), the find* functions only retrieve the element/s.
Double check you were not using driver.findElements(By.xpath()) in plural. In that case you should first retrieve the individual elements. Then access to the .getText() method.
description = driver.findElement(By.className("summary")).getText();
System.out.print(description);
Alternatively you could do:
description = driver.findElement(By.className("summary"));
description_text = description.getAttribute("innerHTML");
System.out.print(description_text);
If your problem is that your element is not visible or reachable (stale). Then you can use javascript.
element = driver.executeScript("return document.querySelector('span[class=\"summary\"]');");
For more reference:
https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html

extracting part of success text using selenium webdriver

I have the following text appearing on the success page of my application.
This is to confirm that your application has been received. Your Order Number is “#00007942”. If further instructions or any clarification is needed regarding your application, a representative will contact you.
Complete text having same property.
Please help me in extracting the value 00007942 and store it in variable.
First, get your text in your way.
String successMessage = driver.findElement(By.cssSelector("your selector")).getText(); // use locator of your wish
Now, use replace all non-digit from your string as follows-
String orderNumber = successMessage.replaceAll("\\D+", ""); // this replaces all non-digits from your previous string
there is no way to retrieve partial text in selenium webdriver.
Instead, you access the complete text of an Web Element using element.getText() in Java or element.text in python and store it as a String variable.
Then you process the string to retrieve the substring you want.
In all programming languages, there are many ways to achieve it. some of them are substring method, regular expression.

Finding text on page with Selenium 2

How can I check whether a given text string is present on the current page using Selenium?
The code is this:
def elem = driver.findElement(By.xpath("//*[contains(.,'search_text')]"));
if (elem == null) println("The text is not found on the page!");
If your searching the whole page for some text , then providing an xpath or selector to find an element is not necessary. The following code might help..
Assert.assertEquals(driver.getPageSource().contains("text_to_search"), true);
For some reason, certain elements don't seem to respond to the "generic" search listed in the other answer. At least not in Selenium2library under Robot Framework which is where I needed this incantation to find the particular element:
xpath=//script[contains(#src, 'super-sekret-url.example.com')]
A simpler (but probably less efficient) alternative to XPaths is to just get all the visible text in the page body like so:
def pageText = browser.findElement(By.tagName("body")).getText();
Then if you're using JUnit or something, you can use an assertion to check that the string you are searching for is contained in it.
assertThat("Text not found on page", pageText, containsString(searchText));
Using an XPath is perhaps more efficient, but this way is simpler to understand for those unfamiliar with it. Also, an AssertionError generated by assertThat will include the text that does exist on the page, which may be desirable for debugging as anybody looking at the logs can clearly see what text is on the page if what we are looking for isn't.

Selenium *ElementPresent and *XpathCount give different results?

I am getting different results for the same locator. For example
//table[#id='foo']
returns true when testing ElementPresent, but returns 0 for XpathCount. In Selenium v1.0.10 IDE the Find button highlights the correct element for both functions. Any ideas on what could be causing this?
Notes:
We have frames on the page EDIT: This is probably the problem. Bounty to verification.
There are many tables on the page, but only one with #id of "foo"
Firefox 3.6
Happens in both IDE and Java RC
Well, this is not a verification more of a non-verification.
I use Selenium to test a GUI with frames. To make isElementPresent and getXpathCount to work I always have to select a frame first with selectFrame (even to get isElementPresent to work correctly). By just opening an URL no frame at all seems to be selected.
This is what the HTML and corresponding selectFrame code looks like:
<frameset id="mainframeset"><frame name="nav" id="nav" src....
selenium.selectFrame("nav");
Use these XPath expressions:
boolean(//table[#id='foo'])
and
count(//table[#id='foo'])
In case there is a table element whose id attribute's value is "foo", then the first expression above should evalute to true() and the second expression above should evalute to a positive integer.
Not really a direct answer to the question, but a workaround if you are reading this and want to loop over the elements. Use isElementPresent in the for loop like this:
for(int i = 2; selenium.isElementPresent("//table[#id='foo']//tr["+i+"]"); i++)
{
selenium.getText("//table[#id='foo']//tr["+i+"]//td["+columnNum+"]");
}
Note that we start i at 2 since XPath is indexed from 1 and we want to skip the header