I'm trying to solve one simple problem that i can't understand how i could solve in excel vba.
I have one cycle for and a Xpath with 24 elements and i want to get text from each element, but when i use xpath shows me that i put the wrong xpath. I understand that i put i inside the string of xpath and that get me wrong. I try different approach like using
Name.Add findApp.FindElementByXPath("(//span[#class='offer-item-title'])["&"i]").Text
but nothing seems work. Can someone help me how i could solve this? Thank you so much :)
Code:
for i=0 to 23
Name.Add findApp.FindElementByXPath("(//span[#class='offer-item-title'])[i]").Text
Next i
XPath doesn't know that i is the name of a VB variable, it thinks it is the name of an element in your source document.
You can construct an expression like this:
FindElementByXPath("(//span[#class='offer-item-title'])[" & i & "]")
Or better, but I don't know if VBA offers the capability, is to pass a parameter into the XPath expression -- ideally you only want to compile the XPath expression once, rather than repeating the compilation 23 times, because compiling it typically takes 100 times longer than executing it.
But for this particular example, it would be better to construct an expression that reads everything you want in one go, rather than making 24 separate calls. Incidentally, XPath indexing starts at one, so the call with i=0 will select nothing. Given that this is XPath 1.0, you can do
//span[#class='offer-item-title'])[position() < 24]
Related
For example, I have div tag that has two attributes.
class='hello#123' text='321#he#321llo#321'
<div> class='hello#123' text='321#he#321llo#321'></div>
Here, I want to write xpath for both class and text attributes but numbers may change dynamically. ie., "hello#123" may become "345" when we reload. "321#he#321llo#321" may become "567#he#456llo#321".
Note: Need to write xpath in single line not separately.
Assuming that you have the (corrected) two-attribute-HTML
<div class='hello#123' text='321#he#321llo#321'>...</div>
you can select it using the following, for example:
Using the contains() function
//div[contains(#class,'hello') and contains(#text,'#he#')]
This is quite specific and only applicable if the "hello" is always split in the same way
Using the translate() function to mask everything except the chars for "hello"
//div[translate(#class,'#0123456789','')='hello' and translate(#text,'#0123456789','')='hello']
This removes all # chars and digits and checks if the remaining string is "hello"
I guess combining these two approaches you will be able to create your own XPath expression fitting your needs. The patterns you provided were not fully clear, so this may only approach a good enough solution.
Workfusion I am trying to make two variables through web-element. When running individually its great but when running separately it has problems.
Please see the picture for the same. This doesn't get executed.
If your XPath has any variable then try the giving a value like following:
//*[#id="mv-tiles"]/a[${i}]
and you can keep changing value of i through loop. A variable inside another will throw error in Workfusion RPA Express.
The ${} evaluates the whole it's body.
Does the following, without nested ${} execute OK?
${webrange[countertemp]}
I am trying to build an XPath for a property that is constantly changing. The number prefix is bound to change sometimes.
Original:
//*[#id="MainContent_DXEditor3_I"]
which I can query using
$x('//*[#id="MainContent_DXEditor3_I"]
Intended use: I would like to build the string to handle any number in the sub-string. Example: if the property changes to 'MainContent_DXEditor33_I' or 'MainContent_DXEditor8_IXYZ' - I still want to be able to find the element without having to rebuild
You can try to relax the predicate by using starts-with() :
//*[#starts-with(#id, "MainContent_DXEditor")]
You should try to identify a unique parent of the element or save xpath as a string that contains a variable.
These are the 2 possible solutions.
A general selector will return multiple elements, if you identify a unique parent then you are closer and after that you can select any first, second.. last if you have a list.
In Beautiful Soup is it possible to search for a text string and then from there find the nth element down?
Update
I am Trying to target the following code field to grab the text. I tried a soup find and findall however I have other pages that I want to target that are just slightly different so I need something really robust
My Plan
Go to page
Find the string Model name
Find nth element down, in this case the next anchor tag
My code to find string
model_name=soup.find(text='Model name')
print model_name
Ok I got it, its actually really simple. The solution I found gets a little messy but it works
All you got to use is the next operator so the code looks like this
model_name=soup.find(text='Model name').next.text
adding as many next operators until you reach the target.
ok so i've made a HTTPWEBREQUEST and i've made the source of the result show in a richtextbox, Now say i have this in the richtextbox
<p>Short URL: <code>http://URL.me/u/eywnp</code></p>
How would i go about just getting the "http://URL.me/u/eywnp" ive tried split but didnt work, guess i'm doing it wrong?
NOTE the URL will be different everytime
Split isn’t the right tool for the job. It will result in a rather complex piece of code that’s quite brittle (meaning it will break as soon as there’s the slightest change in the input).
For a robust, well-written solution you need to parse the HTML properly. Luckily there exist canned solutions for that: The HtmlAgilityPack library.
Dim doc As New HtmlDocument()
doc.LoadHtml(yourCode)
Dim result = doc.DocumentElement.SelectNodes("//a[#href]")(0)("href")
The only complicated part here is the string "//a[#href]". This is an XPath string. XPath strings are a mini-language that is used to address elements in an HTML or XML document. They are conceptually similar to file paths (like C:\Users\foo\Documents\file.txt) but with a slightly different syntax.
The XPath simply selects all the <a> elements having a href attribute from your document. Then you can grab the first of that collection and retrieve the href attribute’s value.
Thanks for all your help, i did find a solution and i used
Dim iStartIndex, iEndIndex As Integer
With RichTextBox1.Text
iStartIndex = .IndexOf("<p>Short URL: <code><a href=") + 29
iEndIndex = .IndexOf(""">", iStartIndex)
Clipboard.SetText(.Substring(iStartIndex, iEndIndex - iStartIndex))
End With
works perfect so far