How do I get the inner text of an element that has been click?
string xplast = "//button[#id='test']";
IElementHandle last = await ((IPage)page).WaitForXPathAsync(xplast);
await last.FocusAsync();
await last.ClickAsync();
string innertext = (await last.GetInnerTextFromElement()).ToString()
is there a way to do something like this? string innertext = (await last.GetInnerTextFromElement()).ToString()
I have now tested that var text = (await last.GetPropertyAsync("innerText")).RemoteObject.Value.ToString(); works fine, at least here on Windows.
If you'd prefer a strongly typed experience then PuppeteerSharp.Dom provides a set of extensions to PuppeteerSharp.
Install PuppeteerSharp.Dom from Nuget.org then you can use the strongly typed extensions.
// Add using PuppeteerSharp.Dom; to access WaitForXPathAsync<T>
string xplast = "//button[#id='test']";
var last = await page.WaitForXPathAsync<HtmlButtonElement>(xplast);
await last.FocusAsync();
await last.ClickAsync();
var innerText = await last.GetInnerTextAsync();
Related
Im trying to update a string field in specific document using Firebase Firestore for my android app but every method I see is while knowing the document refernce which I find difficult to find in my program.
Would like for some help for another method or help in finding the document refernce using a specific field value.
Thanks in advance.
(using C# btw)
private async Task<string> GetDocRefAsync(string userId)
{
Object obj = await FirestoreData.GetFirestore().Collection(DBConstants.FS_COLLECTION_USERS).
WhereEqualTo(DBConstants.FS_COLLECTION_USERS_USER_ID, userId).Get();
QuerySnapshot snapshot = (QuerySnapshot)obj;
if (snapshot.IsEmpty)
{
Log.Debug("UpdateGroupAsync", "userId: " + userId + " not found");
return null;
}
string docRef = "";
foreach (DocumentSnapshot item in snapshot.Documents)
{
//docRef = item.;
}
return docRef;
}
Firstly ive tried to find the document ref using this code but dont have a function to get the ref even after getting the correct document.
the fourth line from the bottom is where I couldnt find it.
database pic
this.groupCode = code;
string strUserRef = GetDocRefAsync(userRef).ToString();
DocumentReference docReference = database.Collection(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE).Document(strUserRef);
docReference.Update(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE, groupCode);
If you want to get the documents where a field has a given value, you can use a query. Then once the query returns, you can get documents IDs with the .Id field on each DocumentShapshot in the returned documents.
You will also need to add await for the returned value since it is an async method returning a Task<string> not returning a string.
private async Task<string> GetDocRefAsync(string userId) {
CollectionReference usersRef = FirestoreData.GetFirestore().Collection(DBConstants.FS_COLLECTION_USERS);
Query query = usersRef.WhereEqualTo(DBConstants.FS_COLLECTION_USERS_USER_ID, userId);
// or GetSnapshotAsync depending on the version of firebase
QuerySnapshot querySnapshot = await query.Get();
// Note: if it matches multiple documents this will just return
// the ID of the first match
foreach (DocumentSnapshot item in snapshot.Documents)
{
return item.Id;
}
Log.Debug("UpdateGroupAsync", "userId: " + userId + " not found");
return null;
}
And you can use it like this to update a document (note that you were using a different collection here - probably by mistake).
string userDocId = await GetDocRefAsync(userId);
CollectionReference userCollection = database.Collection(DBConstants.FS_COLLECTION_USERS);
DocumentReference docReference = userCollection.Document(userDocId);
// or UpdateAsync depending on your version of firebase
docReference.Update(DBConstants.FS_COLLECTION_USERS_GROUPS_CODE, groupCode);
Using PuppeteerSharp, I am trying to get the text of the element.
ElementHandle elementHandle = await page.XPathAsync("//html/body/div[1]/section/div/section/h2")[0];
Now that I have the element handle, how do I actually get the text from it? I don't see any obvious methods. I would have expected TextAsync or something similar, but I don't see it.
Using PuppeteerSharp 5.0.
You can call EvaluateFunction passing that ElementHandle as an argument
var content = await Page.EvaluateFunctionAsync<string>("e => e.textContent", elementHandle);
If you have many scenarios like that, you can build an extension method to solve that for you ;)
#Botan, thank you!
I have tried (in VB.NET) and found:
(Await elementhandle.GetPropertyAsync("innerText")).ToString
result: "JSHandle:foo", but
(Await elementhandle.GetPropertyAsync("innerText")).RemoteObject.Value.ToString
result: "foo"
If your are after a strongly typed API for use with Puppeteer Sharp then you can use PuppeteerSharp.Dom which is available on Nuget.org.
// Add using PuppeteerSharp.Dom to access the extension methods
ElementHandle elementHandle = await page.XPathAsync("//html/body/div[1]/section/div/section/h2")[0];
// Create a strongly typed HtmlHeadingElement object
var headingElement = elementHandle.ToDomHandle<HtmlHeadingElement>();
// You'll now have context specific methods relevant to HtmlHeadingElement
//Get TextContent via the async method
var textContext = await headingElement.GetTextContentAsync();
var innerText = await headingElement.GetInnerTextAsync();
There's a number of QuerySelector extension methods also, so you can avoid the ToDomHandle method if you are using a query selector.
var element = await page.QuerySelectorAsync<HtmlElement>("#myElementId");
There are more examples on the GitHub page.
I can't figure out how i can get all computed styles of an element in AngleSharp. Here's what I've tried:
var config = Configuration.Default.WithDefaultLoader().WithCss();
var context = BrowsingContext.New(config);
var document = await context.OpenAsync("A website");
var button = document.QuerySelector("a.random class");
var styles = document.DefaultView.GetComputedStyle(button); // holds null
Can anyone help figure out how this is done? I found this method on their documentation but it doesn't work.
I'm brand new to puppeteersharp. Can you please tell me where, in my thought process and code, I'm going wrong. I'm trying to grab reviews on this product:
https://www.newegg.com/gigabyte-geforce-rtx-2060-gv-n2060oc-6gd/p/N82E16814932115
All reviews load when you click the reviews tab which is retrieved from
document.querySelectorAll(".tab-nav")[4]
reviews is null after this code:
using (var browser = await Puppeteer.LaunchAsync(options))
{
using (var page = await browser.NewPageAsync().ConfigureAwait(false))
{
await page.GoToAsync("https://www.newegg.com/gigabyte-geforce-rtx-2060-gv-n2060oc-6gd/p/N82E16814932115");
var clickReviews = "document.querySelectorAll('.tab-nav')[4].click();";
var reviews = "Array.from(document.querySelectorAll('.comments-content'));";
await page.EvaluateExpressionAsync(clickReviews);
var reviews = await page.EvaluateExpressionAsync(reviews);
Console.WriteLine(reviews);
EvaluateExpressionAsync won't return DOM elements. You should build a serialized output. For instance, a string[] with the review text.
var reviews = "Array.from(document.querySelectorAll('.comments-content')).map(r => r.innerText);";
var reviews = await page.EvaluateExpressionAsync<string[]>(reviews);
Console.WriteLine(reviews);
I'm trying to get a single file in an UWP based on its partial name. Basically, I want to select a file that starts with "latest_" in a given folder.
Right now I can do that with that command:
var previousInfo = (await rootFolder.GetFilesAsync()).Where(file => file.DisplayName.StartsWith("latest_")).FirstOrDefault();
I suppose this code is not very efficient if there are a lot of files in the folder...
Is there a simpler way to do this? Like:
var previousInfo2 = await rootFolder.GetFileAsync("latest_*");
Or:
var previousInfo2 = await rootFolder.GetFileAsync(#"latest_*");
Right now this code is throwing an exception (invalid parameter). And the MSDN documentation about this function is very light: it just says that the function takes a string as parameter...
Thanks!
You can do this by using Advanced Query Syntax and in fact, you can filter/search by many more file metadata. Below is an example for search based on File Name.
var queryOptions = new QueryOptions();
queryOptions.ApplicationSearchFilter = "System.FileName:latest_*";
StorageFileQueryResult queryResult = rootFolder.CreateFileQueryWithOptions(queryOptions);
var files = await queryResult.GetFilesAsync();