Is it possible to find and store element's location by text in selenium ide? - selenium-ide

I need to create the element and then delete it. Is there a way to find the element by it's text after it was created?
The xpath of the element is //div[#id='mif-tree-6']/span/span[3].

You can use xpath for it for example. Like:
//div[#id='mif-tree-6']//span[contains(text(),'your_text_here')]
UPDATE
Please provide an example of your html. It is possible to find a parent of your element with xpath and after that to find all the childs. For example your html =
<div id='lol'>
<div>first_item</div>
<div>second_item</div>
<div>third_element</div>
</div>
You get an array of elements with xpath =
//div[contains(text(),'first_')]/../div
So you can do something like:
click | //div[contains(text(),'first_')]/../div[2]
BUT if there are a lot of brothers-elements to find by text of one sibling it will be necessary to use loop to get every of them.
Once again. If you will provide full information about what are you doing and an example of your html it will be much easier to suggest.

Related

How do I get the nth element with a certain name in the entire XML document?

so will something like this work using Hpple Xpath
//a[4]
The fourth tag in a html tree?
Or do i need to do it programmatically by counting in a for() loop?
The XPath for the fourth <a> in an HTML document is:
(//a)[4]
Your example //a[4] will produce a set of all <a>s that are the fourth <a> in their respective parent, and that is not what you want here.
See also: https://stackoverflow.com/a/14209492/1945651

how to display text in MultiCell() in fpdf?

I have a problem in FPDF. I want to use MultiCell() to display a text. my code is as follows:
$syarat .='<ol><li>aaa</li>
<li> bbb
<li> ccc</li>
<liddd</li>';
$pdf->SetFont('Arial','',7);
$pdf->SetX(10);
$pdf->MultiCell(190,4,$syarat,0);
//but the problem is it display also with , tags. How to appear numbering instead of , tags?. Thank You
In FPDF did not support the HTML tag ,you have to write it by yourself.
1) if you want to do like this: you can try with HTML->PDF http://html2pdf.fr/
2) You can do with multiple cell Please learn from this exampel
http://www.fpdf.org/en/script/script3.php
If you need help from the example ,Please let me know.
FPDF supports HTML but you need to use a script such as: http://fpdf.org/en/script/script53.php
This one claims that it has full support for lists <li>.
To make the numbers appear, you can always do that manually, for example:
$syarat .='<ul>
<li>1. aaa</li>
<li>2. bbb
<li>3. ccc</li>
<li>4. ddd</li>
</ul>';
Other scripts are available here, but I don't see another one that supports <LI> tags.

DOM xpath html extraction

I am importing some html into a dom document ans use xpath to extract the html part I am interested in. See below:
$dom = new DOMDocument();
#$dom->loadHTML('$myHtmlFileHere');
$xpath_dom_doc = new DomXPath($dom);
$dom_object = $dom_document->query('myPathHere');
Here below the html structure "returned":
<div>GROUP A</div>
<span>aaa</span>
<span>zzz awesome</span>
<span>eee</span>
<div>GROUP B</div>
<span>fff</span>
<div>GROUP C</div>
<span>zzz</span>
<span>uuu</span>
<span>iii</span>
<span>rrr</span>
As you see I have categories (GROUP A, GROUP B and GROUP C). In the spans below each category I have information related to the category. What I would like is send to a db the span content with the related category. The problem that I am confronted to is that the div tag of the category is not wrapping the spans. So I do not see how I can manage that. hope someone can help. thank you in advance. Cheers. Marc
What about the following-sibling xpath function? You should be careful, though, and only select the siblings up to a next div.
For example, tested with xsh2:
$div = //div[2] ;
ls $div/following-sibling::span[count(preceding-sibling::div)=1+count($div/preceding-sibling::div)] ;

innerHTML of an element without id or name attributes

Why is the following code NOT working without id or name attribute specified for the anchor element?
<html>
<body>
First link
<p>innerHTML of the first anchor:
<script>document.write(document.anchors[0].innerHTML);</script>
</p>
</body>
</html>
But if I add an id (or name) attribute, like that:
<a id="first" href="#">First link</a>
It starts to work.
Why is id or name attribute so important? I don't refer to it in my javascript code. I don't use "getElementById" or anything, but it still wants an id to be specified.
P.S. I tested only in IE7 (not the best browser, but I don't have access to anything better at the moment, and it can't stop me from learning :)
UPDATE:
Thanks to Raynos who gave me an idea of HTMLCollection in his answer, I've gotten a deeper understanding of what's going on here, by searching the web.
When we use document.anchors collection, we're actually referring to a collection of a elements with the name attribute that makes an a element behave as an anchor, and not (only) as a link.
We don't have to specify the name attribute if we want to refer to a elements as links. In this case we just need to use a different instance of HTMLCollection object which is document.links.
So the original code will work without name attribute if we modify it to:
document.write(document.links[0].innerHTML);
What a nice feeling of enlightenment! :)
WHATWG says:
The anchors attribute must return an HTMLCollection rooted at the Document node, whose filter matches only a elements with name attributes.
the document.anchors collection needs <a> elements with a name attribute.
IE is known to have bugs where it treats id's and name's as the "same" thing. So that would probably explain why it works for <a> elements with an id attribute.
As an aside, document.write and .innerHTML are evil.
Why don't you use this:
document.getElementsByTagName('a')[0].innerHTML

Need to print out all links on a sidebar in selenium (xpath?)

I need to find any extra links and print them out. I started by doing:
get_xpath_count('//li/a')
and comparing it to the size of an array that holds the name of all the links for the sidebar. When the count is too high/low, I need to print out all the extra/missing links. I would like to make a list of the names so I can compare it to the array. I've tried a few things like get_text('//li/a'), which returns the name of the first. get_text('//li/a[1]) does the same, but any other index returns nothing.
Any ideas? Also, I need the name that's displayed on the link, not the actual href.
Edit* Also, i'm pretty new to selenium and Xpath. Please let me know if there's info I let out that is needed, or just any suggestions towards thew way I'm going about this.
I have been able to get this to work using CSS element locators. Since I use CSS selectors far more often than Xpath, I find it easier to always use them with Selenium as well.
$selenium->get_text("css=li a:nth-child(1)")
$selenium->get_text("css=li a:nth-child(2)")
$selenium->get_text("css=li a:nth-child(...)")
$selenium->get_text("css=li a:nth-child(n)")
Use:
(//li/a)[$someNumber]
this will get you the text of $someNumber-th //li/a in the XML document.
In order to know what values to use to substitute the $someNumber with, you need to know the total count of these elements:
count(//li/a)
This is in JAVA. You can use the same concept in perl
int totCountInPage=selenium.getXpathCount(//li/a);
for(int count=1;count<=totCountInPage;count++)
System.out.println(selenium.getText("xpath=//li[count]/a"));
This should print text inside the anchor links under all li tag.