Selenium RC Having problems with XPath for a table - selenium

I'm trying to select an element given by:
/html/body[#id='someid']/form[#id='formid']/div[#id='someid2']/div[#id='']/div[#id='']/div[#id='']/table/tbody[#id='tableid']/tr[7]/td[2]
Now the html of that row I'm trying to select looks like this:
<tr>
<td class="someClass">some text</td>
<td class="someClass2">my required text for verifying</td>
</tr>
I need to check whether my required text for verifying exists in the page.
I used selenium.isTextPresent("my required text for verifying"); and it doesnt work
So now I tried with selenium.isElementPresent("//td[contains(text(),'my required text for verifying')]")
This works sometimes but occassionally gives random failures.
Tried with selenium.isElementPresent(//*[contains(text(),'my required text for verifying')]) too..
How do I verify this text on the page using selenium?
The problem is not with the page taking time to load. I took screenshots before the failure occurs and found that the page was fully loaded so that shouldnt be the problem.
Could someone please suggest any way to select this element or any way to validate this text on the screen?

Try locating it by CSS:
assertText(selenium.getText("css=.someClass2"), "my required text for verifying");
The above should give a better failure message than isElementPresent, but you can still use that with CSS locators:
assertTrue(selenium.isElementPresent("css=.someClass2"));
If there is an issue with the load times you could try waiting for the element to be present:
selenium.waitForCondition("var value = selenium.isElementPresent('css=.someClass2'); value == true", "60000");
Some other XPath locators that might work for you, if you prefer not to use CSS locators:
//td[contains(#class, 'someClass2')
xpath=id('tableid')/tr[7]/td[2]
xpath=id('tableid')/descendant::td[contains(#class, 'someClass2')][7]

I've never heard of selenium; but your initial XPath is unnecessarily fragile and verbose.
If an element has an id, it's unique; using such a long XPath just to select a particular element is unnecessary; just select the last element with the id. Further, I see that you're occasionally selecting xyz[#id=''] - if you're trying to select elements without id attributes, you can do `xyz[not(#id)] instead.
Assuming your initial XPath is basically correct, it would suffice to do something like this:
//tbody[#id='tableid']/tr[7]/td[2]
However, using a specific row and column number like that is asking for trouble if ever anyhow changes details of the html. Also, it's atypical to have id's on tbody elements, perhaps the table element has the id?
Finally, you may be running into space-normalization issues. In xml, multiple consecutive spaces are often considered equivalent to a single space, and you're not accounting for that. In particular, if the xhtml is pretty-printed and contains a line-break in the middle of your sought-after text, it won't work.
//td[contains(normalize-space(text()),'my required text for verifying')]
Finally, text() explicitly selects
child text nodes - so the above xpath won't select elements where the text isn't the immediate child of td (e.g. <td><b>my required text for verifying</b></td>) won't match. Perhaps you mean to look up the concatenated text vale of all descendents:
//td[contains(normalize-space(string(.)),'my required text for verifying')]
Finally, type conversion can be implicit in XPath, so string(.) can be replaced by . in the above, leading to the version:
//td[contains(normalize-space(.),'my required text for verifying')]
This may be slow on large documents since it needs to normalize the spaces and perform a string search for each td element. If you run into perf problems, try to be more specific about which td elements need to be inspected, or, if you don't care where the text occurs, try to reduce the number of "calls" to normalize-space by normalizing the entire doc in one go (e.g. via /*[contains(normalize-space(.),'my required text for verifying')]).

Related

How to Identify Elements in Salesforce Lightning for Selenium

I am trying to automate Salesforce lightning using Selenium, but getting issues with identifying elements. Reason, its having dynamic IDs , and other attributes are either very long , or they are not unique.
For eg ,
<a id="170:1968;a" class="textUnderline outputLookupLink slds-truncate forceOutputLookup"
data-refid="recordId"
data-recordid="0059E000001aOCSQA2"
data-special-link="true"
href="#/sObject/0059E000001aOCSQA2/view"
target="_blank" rel="noreferrer"
title="" data-aura-rendered-by="170:1968;a" data-aura-class="forceOutputLookup"/>
In above code , ID is dynamic , Class is not unique, and all the Lookup elements are associated with it. Also the absolute path is not much trusted , and hence I am trying to find any concrete option to handle these elements. Any help will be highly appreciated.
Here, you could try using the contains method if at least a part of the id attribute value is static.
From your code, you could try
//a[contains(#id,"a")]/ //--extended xpath--
From the given html code, the 'a' in the id attribute of the a tag looks static, while the rest changes.
You can ask the developers to provide an id to the lightning component using aura:id
Then the dynamic id won't be generated.
You can try with field labels and fetch its parent node(s), and then fetching childs or brother nodes to locate related texts/text boxes etc.
Eg. You are in Account Edit/New page, and you want to fill in a value to the text box for Account Name field. So you can firstly try with //*[text()='Account Name']/parent::* to find an element that covers BOTH the field label and the text box.
And then you can check if the text box is a 'brother' or a 'child'. If it's a 'child' then try with //*[text()='Account Name']/parent::*(/parent::*)//*[attributes for the text box];
If it's 'brothers' then try with //*[text()='Account Name']/parent::*(/parent::*)/following-sibling::*[attributes for the text box]
You can use this logic to locate all type of fields in all standard lightning pages.

Concise Xpath to simulate finding an element regardless of page structure? (selenium)

If you're visually looking at a webpage and there is something clickable and unique on the page, you'll just click it. Without thinking about the page structure.
I'm interested to see what the most concise xpath is that could be constructed to simulate this in a versatile manner.
For example, target the "I'm feeling Lucky" button on the Google homepage:
//*[contains(#*, 'Lucky')]
The above works. But would fail in the element contained Lucky as inner text, or if the wrong case was specified. As such, our xpath needs to cater for any sensitivity and also look for the given string matching inner-text as well.
How could the above xpath be expressed in the most concise yet encompassing structure?
There is nothing thats very generic and executing such xpaths could be costly also at times.
"//*[contains(#*, 'Lucky')] | //*[contains(text(), 'Lucky')]"
Above is one xpath you can combine to get some results. You start specifying which nodes you don't to examine or ones which you want to examine
"//*[contains(#*, 'Lucky')] | //*[contains(text(), 'Lucky')][not(self::script|self::td)]"
And you can keep improving it
It's not possible to create a versatile XPath to accurately/reliability locate an element by text.
Why?
Because the text evaluated by an XPath is not necessary rendered in the page.
Because there's a hight chance to end-up with multiple matches since each ancestor also contains the expected text.
But mainly because there's too many rules/specific cases to consider.
But if I had to create one, then I'd start with this one:
"(html/body//*[not(self::script or self::style)][contains(concat(#value, normalize-space()), 'MyText')])[last()]"
Get all the descendants of the <body>
html/body//*
except <script> and <style>
[not(self::script or self::style)]
where the value attribute or normalize html contains 'MyText'
[contains(concat(#value, normalize-space()), 'MyText')]
then returns the last and deepest match
[last()]

WebDriver not identifying WebElement

I using xpath="//div[#class='localityKewordDropDown']/descendant::div[#class='over']/span[text()='Dwarka, New']
but the element is not getting recognized. NosuchElementException is getting encountered.
Could anyone help me out here.I want to click the drop down value highlighted in the image.
It's failing beacuse there is a space after the word 'New'. The following should work.
//div[#class='localityKewordDropDown']/div/div[text()='Dwarka, New ']
Or consider serarching for the element containing the text rather than matching the entire text.
//div[#class='localityKewordDropDown']/div/div[contains(.,'Dwarka, New')]
And as cathal mentioned, you are searching for a div and not a span.
EDIT:As you request (although I don't believe there is a difference between "descendent" and "//".
//div[#class='localityKewordDropDown']/descendant::div[text()='Dwarka, New ']
//div[#class='localityKewordDropDown']/descendant::div[contains(text(),'Dwarka, New')]
Contains is an xpath function that allows you to query on something containing a value. It can be used with attributes, nodes but it's generally most useful for finding elements containg text. The reason this is working where as your query for the exact string fails is because the element you seek is padded with a trailing space. The contains query will find the element you are seeking as it's ignoring this trailing space.
i dont see anyw spans around your element, try the following:
WebElement name = driver.findElement(By.xpath("//div[#id='keyword_suggest']//div[text()='Dwarka, New']"));
Try this:
WebElement name = driver.findElement(By.xpath("//*[contains(text()='Dwarka, New')]"));

XPath query search

Such structure is given
<div class="user-number">123</div>
<div class="user-state">
<span class="u-state-icon icon icon-1"></span>
<span> User1</span>
</div>
I've tried such (incorrect) xpath for locating User1 by user-number and do not understand where is the problem..
xpath=//*[#class='user-number' and text() = '123']/following-sibling::*[contains(#class,'user-state')]/descendant::*[contains(#text,'User1')]
What is the best way to debug it?
For example, if
xpath=//*[#class='user-number' and text() = '123']/following-sibling::*[contains(#class,'user-state')]
locates some element - how to print out its text property - to check which element is actually located?
Your xpath expression is, surely, incorrect - #text should be replaced with text() (or just .):
//*[#class='user-number' and . = '123']/following-sibling::*[contains(#class,'user-state')]/descendant::*[contains(.,'User1')]
Debugging xpath expressions is usually done using the browser developer tools: in the firebug, or inside a browser console. For instance, in the google-chrome console, you can execute the following:
$x("//*[#class='user-number' and . = '123']/following-sibling::*[contains(#class,'user-state')")
And see if there is a match.
Or, you can also debug it inside your code. For example (using python), find the first div element and print out it's text:
element = driver.find_element_by_xpath("//*[#class='user-number' and . = '123']")
print(element.text)
The meta-question is, how to debug XPath expressions?
Well, for simple ones like these, it's really best to just stare at them till you see the problem. Check the spelling of names, check namespaces, check whitespace issues. At least it's easier than debugging regular expressions.
For more complex XPaths, try breaking them up. Remove a predicate and see if that makes a difference. Or work in reverse, build up the path expression by adding conditions, checking at each stage that it still finds something.
If you're really seriously into XPath, consider schema-aware processing: this will match your XPath expression against a schema to make sure it makes sense.
Consider using a visual XPath processor for debugging. There are a number around. I use the XPath processor in oXygen (though not really for debugging the XPath, more for discovering the content of the document, but those tasks often need to be done together.)

HtmlUnit skips table data text after <b> tag

I am using HtmlUnit version 2.10. I am reading data from an html table. The cell in question contains:
<td colspan="2" id="num_custs_text">
<b>Affected Customers:</b> 22
</td>
If I use:
final List<?> elements = pageHtml.getByXPath(getXPath());
for (Object rowObject : elements) {
(...)
String rowDataString = rowData.asText();
(...)
}
the rowDataString only contains "Affected Customers:". It does not contain "22". I have tried dumping the entire page to a log using pageHtml.asXml() but the output does not contain "22". It looks like HtmlUnit is ignoring the text after the tag on the initial getPage operation.
How do I force HtmlUnit to load?
Thank you,
Neil
Given the fact that you have not provided the input HTML text nor the XPath you use it is impossible to determine whether you have a wrong XPath string or not. I will assume you have the right XPath string.
Now, you said:
I have tried dumping the entire page to a log using pageHtml.asXml() but the output does not contain "22"
If that is the case, then how do you know the 22 is actually there? I will assume you have checked that in an actual web browser. Was JavaScript enabled in that browser? I will assume it was.
Then the most likely issue is that the 22 is being set by JavaScript (maybe AJAX) and HtmlUnit is failing to fetch it (or you don't have JavaScript enabled in HtmlUnit).
Were my guesses right?