'Click on a value that is derived from a text file Playwright-C#

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>


Solution 1:[1]

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}]");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xen0byte