Within my HTML code, there is an element that looks like this:
<td title="11607" style="text-overflow:ellipsis;white-space:nowrap;">11607</td>
To locate this element, I use the following locator:
string number= "11607";
IWebElement element = DriverUser.FindElement(By.XPath(string.Format("//*[contains(text(),'{0}')]" , number)));
My goal is to highlight the text 11607 using Selenium WebDriver.
After exploring various solutions, I came across two options. The first solution seemed promising initially but did not yield the desired result. I am unsure if the issue lies with the fact that the HTML tag is TD or if the solution itself is flawed. The second solution worked, but I believe there is room for improvement.
Here are the details:
- The first solution is: (taken from Highlight text using Selenium)
public static void HighlightText(this IWebElement element)
{
element.Click();
Actions actions = new Actions(Driver);
actions.SendKeys(Keys.Home).Build().Perform();
int length = element.Text.Length;
actions.KeyDown(Keys.LeftShift);
for(int i = 0 ; i < length ; i++)
{
actions.SendKeys(Keys.ArrowRight);
}
actions.KeyUp(Keys.LeftShift);
actions.Build().Perform();
}
However, when I executed this solution, it had no effect.
Could it be due to the TD tag instead of an INPUT tag?
- The second solution is: (taken from , and found elsewhere)
public static void HighlightTextJS(this IWebElement element)
{
var jsDriver = ( IJavaScriptExecutor ) Driver;
string highlightJavascript = @"$(arguments[0]).css({ ""background"" : ""DodgerBlue""});";
jsDriver.ExecuteScript(highlightJavascript , new object[] { element });
}
This solution highlights the entire background of the element rather than just the letters. I am seeking a way to accurately simulate dragging a mouse cursor over the text.
Based on the provided screenshot The current outcome resembles the first line (11604), but I desire a visual similar to the second row (11602)
https://i.sstatic.net/ha9Vh.png
Is there a CSS property I can leverage to achieve this effect?
In addition, I attempted sending keys using SendKeys as follows:
Actions actions = new Actions(Driver);
actions.MoveToElement(element);
actions.SendKeys(Keys.Shift + Keys.ArrowLeft + Keys.ArrowLeft + Keys.ArrowLeft + Keys.ArrowLeft);
actions.Build().Perform();
and also tried:
Actions actions = new Actions(Driver);
actions.SendKeys(element, Keys.Shift + Keys.ArrowLeft + Keys.ArrowLeft + Keys.ArrowLeft + Keys.ArrowLeft);
actions.Build().Perform();
Unfortunately, these attempts were unsuccessful.
Thank you for any assistance provided!