1

I want to write JavaScript code and to use it in Python+Selenium script. Target URL is Microsoft account login page. Here I want to force onclick opening Terms of use page not in new tab, but in new window so I made some changes to appropriate anchor tag. I have a little experience in JS, so this is what I got for now:

document.querySelector('a#ftrTerms').setAttribute("onclick", "window.open('https://login.live.com/gls.srf?urlID=WinLiveTermsOfUse&mkt=EN-US&vv=1600', '', 'width=800,height=600')")
document.querySelector('a#ftrTerms').removeAttribute('href')

...and this works fine (not the best way, but it's ok). However, redirection URL is hardcoded, so I use following to get URL from href first and then pass it to attribute:

var reference = document.querySelector('a#ftrTerms').getAttribute('href');
document.querySelector('a#ftrTerms').setAttribute("onclick", "window.open(\'' + reference +  '\', '', 'width=800,height=600')")
document.querySelector('a#ftrTerms').removeAttribute('href')

...and this code is not working: my attribute in HTML appears like

onclick="window.open('' + reference + '', '', 'width=800,height=600')".

So how to substitute reference name with its actual value?

1 Answer 1

1

You simply need to calculate the string from the value:

document.querySelector('a#ftrTerms').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")

reference should not be in the string, but concatenated with the rest of the string prefix and suffix

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.