Make expression match optional value if possible - regex

Consider the following text that might be part of an HTTP response from jMeter:
<menu id="Alpha" name="alpha">
<option value="a">A</option>
<option value="b">B</option>
<option value="c" selected="selected">C</option>
</menu>
<menu id="Bravo" name="bravo">
<option value="d">D</option>
<option value="e">E</option>
</menu>
I'm trying to extract the ID of each menu, as well as the selected option's value if there is an option selected. If there is no option selected, then by default, the first option's value should be matched. For example, in this example, I want the following to be matched:
"Alpha" "c"
"Bravo" "d"
So far, I have written the following:
<select id="Form:parameterList:([^"]+?)".*?>.*?(?:<option value="([^"]*?)".*?(?:selected="selected")?>)?.*?</select>
The problem with this is that only the first option's value is ever matched, and the selected option is never matched. That is, I want to prioritize matching the optional pattern.
Thanks,
Victor

Consider how maintainable your script will be using that complex regex.
You can achieve the result you desire using xpath extractors, conditional/loop controllers and or post-processors...
First get a list of all ids:
//menu/#id
You can get a list of ids containing an option with attribute selected = 'selected' with something similar to:
//menu/#id[/option[#selected='selected']]
Iterate through the first list (in a beanshell processor or loop controller, for instance). Where an id appears in the second list, extract the selected value with:
//menu[#id='xxxx']/option[#selected='selected']/#value /*substitue xxxx with appropriate id*/
Where the id does not have a 'selected' value, extract the default value:
//menu/[#id='xxxx']/option[1]/#value /*substitue xxxx with appropriate id*/
(sorry if my xpath aren't completely accurate, I have written this from memory alone, but hopefully the breadcrumbs are there to follow)

Related

Google Data Studio Calculated Field by Extracting String from Event Label Values

I'm trying to use the CASE statement to output string values for an Event Label field using RegEx to produce a table that shows the number of events for each field value. So, if I'm looking for foobar, and other string values separately, within values for Event Label; it may either stand alone or be part of a URL like so:
|[object HTMLLabelElement] | Foobar |
/images/foobar-26.svg
It seems REGEXP_EXTRACT might suit this the best:
CASE WHEN REGEXP_EXTRACT(Event Label, '.(?i)foobar.') THEN Foobar
However, the table produced using the calculated field as the dimension only contains a blank row that seems to be the sum of the number of events.
What am I missing?
I think you need to use REGEXP_MATCH not REGEXP_EXTRACT, given your existing syntax, or to change the syntax to a straight REGEXP_EXTRACT without the CASE element.

Regular expression to capture selected value in the drop down list

I want to create a regex which gives result for the selected field in a drop down list for a particular id.
For example: if I pass id="countries" the result should be India (since India is selected) or id="Gender" the result should be Male.
<select id='countries'>
<optionvalue='0'>All Categories</option>
<option value='1'>USA</option>
<option value='2'>China</option>
<option selected='selected' value='3'>India</option>
<option value='4'>Japan</option>
</select>
<select id='Gender'>
<option value='0'>All Categories</option>
<option selected='selected' value='1'>Male</option>
<option value='2'>Female</option>
</select>
Note: Consider above HTML as a plane text.
Extracting HTML data using regex is a very very very bad idea. It's complicated and inefficient. You are better off using a HTML parser (such as JSoup or BeautifulSoup) for HTML, a JSON library for parsing JSON, etc.
That being said, here is a regex that should work as long as the id is quoted. If your id is in a variable called id, then your regex will be
regex = "<select[^>]*? id=['\"]" + id
+ "['\"].*?<option[^>]*? selected[^>]*?>([^<]*)";
Note that for this to work, the select with that id must actually have a selected option.

//*[#id="gwt-uid-2423"] is getting changed every rime in selenium python2.7 chrome

HTML Code is ..
<div class="N1LCMI-ib-e" role="option" id="gwt-uid-2423" style="user-select: none;">Hotel ID</div>
There is one button when i click on it there are some categories to select Only one category can be selected in one time. So Id in xpath is getting changed every time when i am using Xpath of that category. below is code which i am using to select that category.
driver.find_element_by_xpath('//*[#id="gwt-uid-2423"]').click() #select Hotel ID
so only 2423 part is getting changed every time.
is there any other way to go for it. pls help....
As you mentioned that the 2423 part is getting changed every time for the id attribute, so we can create a unique css_selector or xpath as follows:
css_selector :
driver.find_element_by_css_selector("div[class='N1LCMI-ib-e'][id^='gwt-uid-']").click()
xpath :
driver.find_element_by_xpath("//div[#class='N1LCMI-ib-e'][starts-with(#id, 'gwt-uid-')]").click()
You can find the element by contained text using an XPath.
//div[.='Hotel ID']

how can i put in label a value from the xml

i want a label in my xsl which it's value be one from the xml,
how can i do that?
From your comment I think what you want is this:
<label text="{#theAdv}">
The {} allows you to evaluate XPATH expressions inside attribute values.
EDIT:
if the text is inside an element and not an attribute you can use the following:
<label text="{text()}">
text() get's the inner text of the current element.

How can I disable href link in cfgrid cells for blank rows?

I have the following cfgrid:
<cfgrid format="html" name="grid" pagesize="10" selectmode="row" striperows="yes"
bind="cfc:data.getData({cfgridpage},{cfgridpagesize},{cfgridsortcolumn},{cfgridsortdirection})">
<cfgridcolumn name="link" header="Link" href="link.cfm" hrefkey="link_id">
</cfgrid>
Since pagesize is 10, I'll have some blank rows if I have less than 10 rows in the grid. For those blank row I'd have a '-' sign in place of the usual data, and the hrefkey will be null instead of the usual link_id. Is there a way to disable the href for blank rows? Or if there's a way to capture the null value with javascript?
You can loop through the a hrefs within the table and find all the hrefs that contain the '-' and change this to something else or remove the href completely. This can be done in JS but ideally I would try and do this before hand (i.e in CF) as there will be an overhead using JS to do this.