Using Xpath Contains ID? - dom

<div id="content-body-14269002-17290547">
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
<p>...</p>
</div>
I Need To Select Everything in id = "content-body*"
content-body changes on every Page, May Be Need to Use Wildcards ?

Probably xpath search for divs where the id contains specific text Duplicate
Anyways,
For IDs
//div[contains(#id,"content-body")]//p #to Select all Paragraphs
//div[contains(#id,"content-body")]//p//text() # To Select all text in Paragraphs
For Classes
//*[contains(#class,"content-body")//p
//div[contains(#class,"content-body")]//p//text()
For Class in Class
//*[contains(#class,"content-body") and contains(#class,"another-sub-content-body")]//p//text()
Hope it Helps !!!

//div[contains(#id, "content-body")]
This means content-body in content-body-14269002-17290547
Normally, this will works.
Better:
//div[starts-with(#id, "content-body")]
This means the id attribute's value is started with content-body

Related

Replacing DOM elements that are dynamic

I have a dynamic list < li >
<li>The user name "marcus001" is already taken.</li>
I need to replace this whole line with:
<li><p>Thank you for your registration</p></li>
I'm not sure how to proceed, I have tried to replace this whole DOM, but because the username "marcus001" is dynamic, it always fails to target.
Notes:
This is generated by a system so there are no id's associated.
Try using id attribute to target the <li>
<li id="message">The user name "marcus001" is already taken.</li>
Change it using javascript
document.getElementById("message").innerHTML="<p>Thank you for your registration</p>";

Is it possible to find and store element's location by text in 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.

Selecting last child with specific id

So here is the thing:
<div id="wrapper" > <div id="x"></div ><div id="x"></div ><div id="x"></div><div id="y"></div>
</div>
So how do i select last div with id=x not y inside of wrapper? Sorry , but i dont know how to pase code to be visible.
An ID is unique so to select it you should just be using
#x {}
I would question why you are using multiple id's though, maybe a class would be better?
Edit
Just noticed you pasted some code.
You really shouldn't be using ID's the way you are, you need to be using classes replace your id's with:
class="x"
If only for modern browsers you can use the last-of-type selector
.x:last-of-type
if going back to IE6 you can always add a class like last and then select it
class="x last"
.x.last {}
Edit For question
To use your wrapper selection you add it to the front of your selector:
#x .x.last {
/** css goodness here **/
}

VBA getElementById with dynamic ID

I've been searching this whole forum, msdn and specialised tutorials and I can't find the answer for VBA:
How can I make the getElementById work in an access VBA module where the id to find is dynamic?
Let's see the html code:
<DIV id=rowToolTipContainer>
<DIV class=contactsCard id=resultsTooltip1122286Contents style="DISPLAY: none">
<TABLE class="shadow-box tooltip">
<TBODY>
And how I'm trying to find it:
Dim ResultDIV As HTMLDivElement
Set ResultDIV = HTMLDoc.getElementById("resultsTooltip*")
Let me say the html returned has a different id (the numbers change) depending on each result so the id for each DIV is always:
id=resultsTooltipxxxxxxxContents where xxxxxxx are always different numbers
Any help would be highly appreciated.
Try something like this one:
Dim ContainerDiv As HTMLDivElement, ResultDIV As HTMLDivElement
Set ContainerDiv = HTMLDoc.getElementById("rowToolTipContainer")
For Each ResultDIV In ContainerDiv.GetElementsByTagName("div")
If ResultDIV.ID Like "resultsTooltip*Contents" Then
'' What do you want to do here?
Exit For
End If
Next
Identify the closest parent tag that always contains the ID (manually, by looking at your HTML).
Enumerate all descendant <div>s of that tag, testing their ID property with Like.

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