簡體   English   中英

JavaScriptExecutor中的參考硒元素

[英]Reference selenium element in JavaScriptExecutor

我有一個元素列表,使用By.CssSelector方法位於Selenium中:

var contentRows = new List<TableRow>();

for (var i = 1; i < PositiveInfinity; i++)
{
    var cssSelectorToFind = $"tbody > tr:nth-child({i})";
    var bySelector = By.CssSelector(cssSelectorToFind);

    var rowElement = WebElement.FindElements(bySelector).ToArray();

    if (rowElement.Length == 1)
    {
        var description = $"{Description} Content row: {i}.  Selected by: {bySelector}.";

        var tableRow = new TableRow(bySelector, WebDriver, description, Headers);

        contentRows.Add(tableRow);
    }
    else
    {
        if (rowElement.Length == 0)
        {
            break;
        }
        else
        {
            throw new InvalidOperationException($"The selector {bySelector} returned more that one row at the same ordinal position.  Should be impossible... Best look at the selector and the HTML.");
        }
    }
}

return contentRows;

對於這些行中的每一行,我需要在selected的HTML中設置一個類。

我了解我必須使用JavaScriptExecutor進行此操作。

  • 有沒有一種方法可以獲取每個參考,所以我可以單獨添加它?
  • 是給每行一些唯一ID,然后在JavaScrit中使用它的唯一方法嗎?

您可以將IWebElement引用作為參數傳遞給ExecuteScript ,然后根據需要使用JavaScript中的引用。 因此,例如,如果您有一個IWebElement並希望通過在其周圍繪制紅色邊框來突出顯示它,則可以使用以下代碼(基於該問題的答案)來做到這一點

var element = driver.FindElement(By.Name("..."));
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].style.border='3px solid red'", element);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM