0

I m trying to click on value (link format) on a webpage and i read it from a separate file. the value is read correctly from the external file but I cant get the script to click on it. the first line of code reads the value correctly from the external file but the second is supposed to click on the rendered value. So for instance, the text file has one value of X1 and the webpage has a value in link format called X1. So the idea is to click on the X1 link using the variable valueID rather than just reading the text link from the page. any idea how to implement it or get it to work with the code below please?

<pre>
string ValueID = System.IO.File.ReadAllText(@"ValueID.txt");
await _page.ClickAsync("Value=ValueID");
</pre>
hm9
  • 37
  • 8
  • `Value=` is not a valid selector. – hardkoded Jan 12 '22 at 17:58
  • I tried await _page.ClickAsync("text=${ValueID}"); but it doesnt do it – hm9 Jan 12 '22 at 18:02
  • Can you [edit] your question to include a [repro]? We do not have enough information. – Greg Burghardt Jan 27 '22 at 13:56
  • @hm9 you should check the docs both for [C# strings](https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/string-interpolation) and [Playwright selectors](https://playwright.dev/docs/selectors). `Value=ValueID` is just a string. It doesn't use the variable. So is `text=${ValueID}`. If you want to use an interpolated string use `$"text={ValueID}"`. Don't try to use fancy syntax though. `"text=" + ValueID` also works. Even better, store the selector in a variable so you can inspect it, eg `var selector = "text=" + ValueID;` – Panagiotis Kanavos May 23 '22 at 08:22

1 Answers1

2

The following code should work:

string ValueID = System.IO.File.ReadAllText(@"ValueID.txt");
await _page.ClickAsync($"text={ValueID}");

Notice that you are including the string interpolation operator inside your string, whereas it should be in front of it, e.g. $"text={ValueID}" instead of "text=${ValueID}", but if the above doesn't work, for some reason, also try the following:

  1. when you're reading the value from the text file, make sure there are no leading or trailing whitespace characters
  2. make sure the actual text value of the element does not contain any leading or trailing whitespace characters
  3. is the entire text value of the element exactly equal to the value of your ValueID string? if not, maybe consider using the :has-text() Playwright pseudo-selector, e.g. await _page.ClickAsync($":has-text('{ValueID}')"); which not only is case-insensitive, but also matches on substrings
  4. maybe try using the href attribute paired with a CSS contains instead, e.g. await _page.ClickAsync($"[attribute*={ValueID}]");
Xen0byte
  • 89
  • 9