Using a variable in Python selenium XPATH - javascript

I have a variable as follows:
client_Id = driver.execute_script("return getCurrentClientId()")
I am trying to replace the last value in the XPATH (after clientid=2227885) i.e.2227885 with the variable client_Id. So:
prog_note = wait.until(EC.presence_of_element_located((By.XPATH, "//a[#href='/admin/client/cp_progressnotes.jsp?ESOLclientid=2227885']")))
Should have the number component, in this case, 2227885 replaced with client_Id variable.
How could I go about this? I tried the following to no success
prog_note = wait.until(EC.presence_of_element_located((By.XPATH, "//a[contains(#href, 'cp_progressnotes', "+client_Id + "')]")))

xppath = "//a[#href='/admin/client/cp_progressnotes.jsp?ESOLclientid={}']".format(client_Id )
prog_note = wait.until(EC.presence_of_element_located((By.XPATH, xpath)))
use format

Related

Javascript JSON.stringify method removes trailing zero if object has value as x.0 ( like 6.0 ) [duplicate]

I am working on a project where I require to format incoming numbers in the following way:
###.###
However I noticed some results I didn't expect.
The following works in the sense that I don't get an error:
console.log(07);
// or in my case:
console.log(007);
Of course, it will not retain the '00' in the value itself, since that value is effectively 7.
The same goes for the following:
console.log(7.0);
// or in my case:
console.log(7.000);
JavaScript understands what I am doing, but in the end the actual value will be 7, which can be proven with the following:
const leadingValue = 007;
const trailingValue = 7.00;
console.log(leadingValue, trailingValue); // both are exactly 7
But what I find curious is the following: the moment I combine these two I get a syntax error:
// but not this:
console.log(007.000);
1) Can someone explain why this isn't working?
I'm trying to find a solution to store numbers/floats with the exact precision without using string.
2) Is there any way in JS/NodeJS or even TypeScript to do this without using strings?
What I currently want to do is to receive the input, scan for the format and store that as a separate property and then parse the incoming value since parseInt('007.000') does work. And when the user wants to get this value return it back to the user... in a string.. unfortunately.
1) 007.000 is a syntax error because 007 is an octal integer literal, to which you're then appending a floating point part. (Try console.log(010). This prints 8.)
2) Here's how you can achieve your formatting using Intl.NumberFormat...
var myformat = new Intl.NumberFormat('en-US', {
minimumIntegerDigits: 3,
minimumFractionDigits: 3
});
console.log(myformat.format(7)); // prints 007.000
Hi
You can use an aproach that uses string funtions .split .padStart and .padEnd
Search on MDN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
Here you have an example:
const x = 12.1;
function formatNumber( unformatedNumber) {
const desiredDecimalPad = 3;
const desiredNonDecimalPad = 3;
const unformatedNumberString = unformatedNumber.toString();
const unformatedNumberArr = unformatedNumberString.split('.');
const decimalStartPadded = unformatedNumberArr[0].padStart(desiredDecimalPad, '0');
const nonDecimalEndPadded = unformatedNumberArr[1].padEnd(desiredNonDecimalPad, '0');
const formatedNumberString = decimalStartPadded + '.' + nonDecimalEndPadded;
return formatedNumberString;
}
console.log(formatNumber(x))

Manipulate a string and return it as html

I have a string coming in from a CMS like this:
MSRP Base Price †
The string is not mutable and I need to replace the † wrapped around superscript tags like this:
<sup>†</sup>
However, with the str.replace method, I'm using:
var superLabel = str.replace(new RegExp('†'), '<sup>†</sup>');
superLabel is returning this: MSRP Base Price < sup>†< sup>
You mentioned React in your question. React will automatically decode your string to avoid XSS attacks.
You need to use dangerouslySetInnerHTML to set your value.
Example:
// Does not work
const string = '<sup>†</sup>';
return <div>{string}</div>;
// Works
const string = '<sup>†</sup>';
return <div dangerouslySetInnerHTML={string} />;
Be careful though and make sure your input is safe.
The following is one way to do what you need except for what's mentioned in the answer accounting for React, which I'm not familiar with.
var fromCMSPrefix = 'MSRP Base Price'
var fromCMS = 'MSRP Base Price †';
var superLabel = '<sup>' + fromCMS.substr(fromCMSPrefix.length).trim() + '</sup>';
Here's another:
var fromCMS = 'MSRP Base Price †';
var superLabel = '<sup>' + fromCMS.replace('MSRP Base Price', '').trim() + '</sup>';

Cannot read property 'match' of undefined

I'm running the following:
var token = document.location.href.split('?s=')[1].match(/[a-z0-9]+/);
var longString = "?s=" + token + "?_sft_category=";
var tokenB = document.location.href.split(longString)[1].match(/[a-z0-9]+/);
var attribuB = "." + tokenB;
jQuery('a[data-filter-value="' + attribuB + '"]').parent().parent().parent().find(".dropdown-toggle").html(tokenB).append(' <span class="caret"></span>');
How come I get?
Uncaught TypeError: Cannot read property 'match' of undefined
If I remove .match(/[a-z0-9]+/) i don't get any error, but I do need match ..
The URL looks like:
http://www.example.com/xchanges/results/?s=sky&_sft_category=ogilvy
Probably using a library would be better here. I developed a tiny JavaScript library that work with the urls: url.js.
<script src="path/to/url.js"></script>
<script src="your-js.js"></script>
Then you can do:
var token = Url.queryString("s");
var tokenB = Url.queryString("_sft_category");
And that's it.
Note that instead of calling parent() multiple times, you can use the closest() method:
$('a[data-filter-value="' + attribuB + '"]')
.closest("<your_container>")
.find(".dropdown-toggle")
.html(tokenB)
.append(' <span class="caret"></span>')
;
Your location.href probably doesn't contain your longString. If you split the href using a string that's not present in href, you're left with an array with only 1 string, so you cannot get the [1] element.
It all depends on what your token variable is....
You've used a '?' character twice, which is incorrect.
I'd use
var tokenB = document.location.href.split('_sft_category=')[1].match(/[a-z0-9]+/);
to get the desired value, as _sft_category= will probably only occur once in location.href and you should then be able to split the href into an array of 2.
Saves you 2 lines of code ;) ...
You are using ? twice in longString, which in turn will make split() return an array with only one value. You need to change it to:
var longString = "?s=" + token + "&_sft_category=";
The ? character should only be used once, at the start of the querystring.

JavaScript function to re-arrange date format

I'm working on some analytics, using Google Tag Manager.
We have a datalayer on the site, one of the value's is a date in format DDMMYYYY.
For a media tag I need to change this date to YYYY-MM-DD.
I'm trying something like this as an example: but I can't get it to work.
I am a total novice with Javascript
(function(){
var d = {{Departure_Date}}
var u = {{Departure_Date}}.replace(/(\d{2})(\d{2})(\d{4})/,'$3$2$1')
return u
})()
Here {{Departure_Date}} is a variable that access the dataLayer and pulls out the original date format.
Any help would be greatly appreciated.
[assuming {{Departure_Date}} is a string, like '04032003']
Almost there. Try [ddmmyyyy].replace(/^(\d{2})(\d{2})(\d{4})$/, '$3-$2-$1'). Alternatively, you can slice up the ddmmyyyy string (see snippet)
var res = document.querySelector('#result');
res.innerHTML = 'input: 20052014, output: ' +
'20052015'.replace(/^(\d{2})(\d{2})(\d{4})$/, '$3-$2-$1');
// Alternative:
var inp = '18072013';
res.innerHTML += '<br>input: 18072013, output: ' +
[inp.slice(4), inp.slice(2,4), inp.slice(0,2)].join('-');
<div id="result"><div>

Set MaxLength for dynamic <p>

I have script which allows to display Bing search results. I can call for the search results url's like this:
'<p class="width">',imgResult.DisplayUrl,'</p>' ,
The problem is that sometimes the url is to long an 'sloppy' like this:
http://www.art-wallpaper.net/Game-Wallpapers/Assassins-Creed-Brotherhood/imagepages/image3.htm
I would like to 'hide' or 'delete' the beginning of the url (http://www.) and do the same from /Game... etc This way I can get a 'clean' and 'short' url like: art-wallpaper.net I there a (simple) way of doing this?
Perhaps a JavaScript function like this.
function shorten(str)
{
// Get rid of the protocol
str = str.replace("http://", "");
str = str.replace("https://", "");
// Return the domain-part of the URL
return str.split("/")[0];
}
Pass your URL to the JavaScript function prior to printing it.
Untested.
url="http://www.art-wallpaper.net/Game-Wallpapers/Assassins-Creed-Brotherhood/imagepages/image3.htm";
pathArray =url .split( '/' );
host = pathArray[2];
alert(host);// alert "www.art-wallpaper.net"
and,
pathArray = "http://www.art-wallpaper.net/Game-Wallpapers/Assassins-Creed-Brotherhood/imagepages/image3.htm".split( '/' );
host = pathArray[2].substring(4);
alert(host);// alert "art-wallpaper.net"
​

Categories

Resources