How to get text content on XPath expression - xpath

I have like this.
var content = await page.$x("//span[#class='property-number']");
How to get text content inside that expression?
I already try with this, but not working.
var content = await page.evaluate(el => el.innerHTML, await page.$x("//span[#class='property-number']"));
Any help are appreciated.

Try "//span[#class='property-number']/text()"

Related

Cypress click on link in mailhog body email

I need a help about testing mailhog with cypress.
I am trying to click on "Forgot password" link in email body, any advice how to do it?
You can parse the body string to get the link, but it would be messy.
Better to use a DOMParser
cy.mhGetAllMails().mhFirst().mhGetBody().then(body => {
const parser = new DOMParser();
const doc = parser.parseFromString(body, 'text/html') // make a DOM
const anchor = doc.querySelector('a') // look for anchor tag
const href = anchor.href // get the link
cy.visit(href) // visit the link
})
Notes
You can't click on the link directly with .click() since the DOM created above is not the live one attached to Cypress. But you should be able to cy.visit(href) which does the same thing.
The only problem I foresee is a cross-origin error - if you get that, use the cy.origin() command Ref.
Please see #Mr.PrasadJ question How to access new tab by clicking on "href" if you need more details on cy.origin() usage with email body.
The method proposed by #VincentLarue is more complicated than needed, and has some bugs.
Check out this regex101.com.
Verify account
const regex = /(\/verify\/.*)"/
const url= content.match(regex)[1]
cy.visit(url)
But it is actually a fragile method, the better way is to parse the response body.
Assuming you have an HTML-based web app, you can directly use the text to find and click the element.
cy.contains('Forgot password').click()
In my case, parsing the body didn't work (I could not query my "a" tag). I used a regex to retrieve my link and then click it.
In the mail body, my link looked like :
Verify account
But in the log of
cy.mhGetAllMails().mhFirst().mhGetBody().then(body => {cy.log(body)})
it was melted with randoms = and \r\n since it was not parsed...
Working solution for me was to extract that match with a pattern accepting those character then remove them. And finally rebuild the link to visit it:
cy.mhGetAllMails().mhFirst().mhGetBody().then(content => {
let token = content.match('verify\/([A-Za-z0-9=~_\\r\\n-]+)<')[1];
token = token.replace(/(\r\n|=)/gm, "");
cy.visit('/verify/' + token);
})
Maybe not the cleaner solution but I hope it can helps

How to change in text before saving webpage as a picture

Is it possible with Puppeteersharp to render text, and highlight words that match specific criteria, before saving the webpage as a picture?
In order to save the webpage as html and then do the work, adds others problems (links to picture etc..)
You can use page.evaluate like so:
await page.evaluate(() => {
const pTags = document.getElementsByTagName("p")
for (let i=0;i<pTags.length-1;i++){
pTags[i].style.background="red"
}
})

Image duplicates itself when using appendParagraph in Google Script

I wrote a script to add an image from my Google Drive and some custom text to a Google Doc. (I got the image insertion code from here).
The resulting document is created ok, but my image is added twice for some reason...
function myFunction(e) {
var doc = DocumentApp.create('fileTest');
var body = doc.getBody();
var matchedFiles = DriveApp.getFilesByName('logo.png');
if (matchedFiles.hasNext()) {
var image = matchedFiles.next().getBlob();
var positionedImage = body.getParagraphs()[0].addPositionedImage(image);
}
body.appendParagraph('Test line of text for testing');
doc.saveAndClose();
}
However, if I get rid of my appendParagraph code (body.appendParagraph(t1);) I only get one image (but obviously without the paragraph of text I want)
What's going on here? And how do I add both one picture and my paragraph of text?
I have not even the slightest clue as to why, but I found a way to make this work.
Switching the order of my code seemed to do the trick. I simply moved the image-insertion code to the end (i.e., after the appendParagraph code), and it worked fine. No duplicate image!
function myFunction(e) {
var doc = DocumentApp.create('fileTest');
var body = doc.getBody();
body.appendParagraph('Test line of text for testing');
var matchedFiles = DriveApp.getFilesByName('logo.png');
if (matchedFiles.hasNext()) {
var image = matchedFiles.next().getBlob();
var positionedImage = body.getParagraphs()[0].addPositionedImage(image);
}
doc.saveAndClose();
}

In CKEditor getSelection() returns null value in IE

I am having an small code to select an text in CKEditor. For that i am using following code in javascript.
var docx = editor.document;
var elementx = docx.getById(id);
editor.getSelection().selectElement(elementx);
editor.getSelection().scrollIntoView(true);
It works fine in Mozilla Firefox.But in IE9 it throws an error as selectElement is not an object. So i checked the code and found that getSelection() having an null value. Please help me how to solve it.
I tried some answers given in various sites even in CKEditor fourms nothing helped me.
That's the correct solution:
var editor = CKEDITOR.instances.editor1;
editor.focus(); // Without this selection will be null on IE.
var element = editor.document.getBody().getLast(),
selection = editor.getSelection();
selection.selectElement(element); // You have to reuse selection.
selection.scrollIntoView();
I tested this from the console on Firefox, Chrome and IE8 on http://ckeditor.com/demo and it worked.
This might work.
var docx = editor.document;
var elementx = docx.getById(id);
var resRange = new CKEDITOR.dom.range( editor.document );
resRange.selectNodeContents( elementx );
resRange.collapse();
editor.getSelection().selectRanges( [ resRange ] );
resRange.endContainer.$.scrollIntoView();
This may have something to do with what IE9 considers to be an object. Have you tried selecting different element types?
Maybe grabbing the parent of the element will give you something that IE9 considers to be an object, you can try this:
var docx = editor.document;
var elementx = docx.getById(id);
var parentx = elementx.getParent();
parentx.scrollIntoView();

Clickable Url in Twitter

i have implemented a simple twitter reader in my app. I am able to get the tweets of a
user. But, if there is a url in this tweet, i cant click on it, as its not detected as an URL.
Is there a possibility to implement this function, so that urls in the tweet are displayed
as clickable url, and then launch for example a webbrowser?
Thank you very much
I assume you are using a TextBlock to show the tweet text, correct? If so, change it to a RichTextBox and all you need to do is use Run for text and Hyperlink for the links!
Also, make sure you set the IsReadOnly property of the RichTextBox to true in order for it to work properly!
Next, parse the tweet text with a regular expression to find links, and use the Hiperlink class to create a clickable link on it, and Run on the remaining text!
Here's a sample function that will parse a tweet and build the content for a RichTextBox:
private Block ParseTweet(string tweetText)
{
var paragraph = new Paragraph();
var lastIndex = 0;
foreach (Match m in Regex.Matches(tweetText, #"(http(s)?://)?([\w-]+\.)+[\w-]+(/\S\w[\w- ;,./?%&=]\S*)?"))
{
if (m.Index > 0)
paragraph.Inlines.Add(tweetText.Substring(lastIndex, m.Index));
var hyperlink = new Hyperlink()
{
NavigateUri = new System.Uri(m.Value, System.UriKind.RelativeOrAbsolute),
TargetName = "_blank"
};
hyperlink.Inlines.Add(m.Value);
paragraph.Inlines.Add(hyperlink);
lastIndex = m.Index + m.Length;
}
if (lastIndex < tweetText.Length)
paragraph.Inlines.Add(tweetText.Substring(lastIndex));
return paragraph;
}
You should call this function like so:
var tweetText = #"Testing: http://twitter.com -> link for twitter";
MyRichTextBox.Blocks.Add(ParseTweet(tweetText));
I think it's not possible but but you can parse your text to find URL (with regex) and display a hyperlink below the text.
1) you search for URLs in the text with a regex
2) if a URL is found, you create a HyperlinkButton with this URL

Resources