M
M
Michael Compass2021-08-03 16:24:49
XPath
Michael Compass, 2021-08-03 16:24:49

How to make an XPATH query with multiple elements?

Loading https://www.youtube.com/results?search_query=music
Trying with XPATH query

//h3[@class='title-and-badge style-scope ytd-video-renderer']


find string
<a id="video-title" class="yt-simple-endpoint style-scope ytd-video-renderer" title="Best songs to boost your mood ~ Chill vibes  Chill out music mix playlist" href="/watch?v=E4KMnaOF7lU" aria-label="Best songs to boost your mood ~ Chill vibes  Chill out music mix playlist by Chilled Feelings 14 hours ago 56 minutes 42,032 views">


But it only finds: 610942b13791f313799988.jpeg

How to change XPATH so that it finds all elements in the output and so that each contains title="" href="" aria-label=""
and it is desirable to put a separator between them, such as @@@ .

It should look like this: Best songs to boost your mood ~ Chill vibes Chill out music mix playlist @@@ /watch?v=E4KMnaOF7lU @@@ Best songs to boost your mood ~ Chill vibes Chill out music mix playlist by Chilled Feelings 14 hours ago 56 minutes 42,032 views

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael Compass, 2021-08-08
@mik777em

Here is a solution I found myself (for Browser Automation Studio):
//a[@id='video-title']/@*[name()='title' or name()='href' or name()= 'aria-label']
[0]
[1]
[2]
These are the first 3 elements and so on until the end. The best way is to use a loop to process the data and write it to a file or to Excel. It would be best to write to a CSV file rather than just open it in excel
When using a loop, it's easier to use the following notation for LIST VARIABLE>
VAR_XPATH_TEXT_LIST[0]
VAR_XPATH_TEXT_LIST[1]
VAR_XPATH_TEXT_LIST[2]
There are no commas in the list. They just show up when you LOG ​​LIST VARIABLE and they don't mix with existing commas in the text.
Create an lLOG cube, set it to "EXPRESSION" and paste the following code inside - it will output what you want. But that's not necessary at all, you just need to process the list in a LOOP and write/output whatever you need.
code >
VAR_XPATH_TEXT_LIST[0] + "@@@" + VAR_XPATH_TEXT_LIST[1] + "@@@" + VAR_XPATH_TEXT_LIST[2]
remember to set "EXPRESSION"
It's better to use LOOP. For example FOR LOOP. Before creating a loop, first check how many items are in the list using "Item Number" from the LIST MODULE. Then create a FOR LOOP starting from 0 (because the index of the list elements starts from 0) and TO "ELEMENT NUMBER" - 1 (minus 1) because you started from 0 and the ending LIST INDEX number is actually LIST LENGTH - 1 .Inside this loop, write to a TXT, CSV or EXCEL file what you need,
AND use the loop's CYCLE INDEX as LIST ELEMENT INDEX LIKE THIS is VAR_XPATH_TEXT_LIST []. And set a condition to write a new line every 3 cycles - in this case it's 2 | 5 | 8 | 11 etc. because it starts at 0, so 012 - 345 - 678 etc.
This is done by logging or writing to a variable like this:

VAR_KOD_VSEY_STRANIZY[  * 3 ] + "@@@" + VAR_KOD_VSEY_STRANIZY[  * 3 + 1 ] + "***trash1***" + VAR_KOD_VSEY_STRANIZY[  * 3 + 2 ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question