Retrieving Text between Text in Selenium C#

前端 未结 6 1398
走了就别回头了
走了就别回头了 2020-12-19 06:38

I am facing problem in retrieving Subject title of a mail from Unread mails using Selenium webdriver-C#.

Here\'s the HTML code :

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 06:47

    The 'getText' method available in the Java implementation of Selenium Web Driver seems to do a better job than the equivalent 'Text' property available in C#.

    I found a way of achieving the same end which, although somewhat convoluted, works well:

    public static string GetInnerHtml(this IWebElement element)
    {
        var remoteWebDriver = (RemoteWebElement)element;
        var javaScriptExecutor = (IJavaScriptExecutor) remoteWebDriver.WrappedDriver;
        var innerHtml = javaScriptExecutor.ExecuteScript("return arguments[0].innerHTML;", element).ToString();
    
        return innerHtml;
    }
    

    It works by passing an IWebElement as a parameter to some JavaScript executing in the Browser, which treats it just like a normal DOM element. You can then access properties on it such as 'innerHTML'.

    I've only tested this in Google Chrome but I see no reason why this shouldn't work in other browsers.

提交回复
热议问题