Retrieving Text between Text in Selenium C#

前端 未结 6 1370
走了就别回头了
走了就别回头了 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:45

    I had the same problem. Worked on PhantomJS. The solution is to get the value using GetAttribute("textContent"):

    Driver.FindElementsByXPath("SomexPath").GetAttribute("textContent");
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-19 06:53

    Probably too late but could be helpful for someone.

            IWebElement spanText= driver.FindElement(By.XPath("//span[contains(text(), 'TEXT TO LOOK FOR')]"));
            spanText.Click();
    
    
            IWebElement spanParent= driver.FindElement(By.XPath("//span[contains(text(), 'TEXT TO LOOK FOR')]/ancestor::li"));
            spanParent.FindElement(By.XPath(".//a[contains(text(), 'SIBLING LINK TEXT')]")).Click();
    

    bonus content here to look for siblings of this text once the span element is found, look for siblings by starting from parent. I am looking for an anchor link here. The dot at the start of XPath means you start looking from the element spanParent

    <li>
    <span> TEXT TO LOOK FOR </span>
    <a>SIBLING LINK TEXT</a>
    </li>
    
    0 讨论(0)
  • 2020-12-19 07:03

    Using GetAttribute("textContent") instead of Text() did the trick for me.

    Driver.FindElement(By.CssSelector("ul.list span")).GetAttribute("textContent")
    
    0 讨论(0)
  • 2020-12-19 07:06

    This worked for me in Visual Studio 2017 Unit test project. I'm trying to find the search result from a typeahead control.

        IWebElement searchBox = this.WebDriver.FindElement(By.Id("searchEntry"));
    searchBox.SendKeys(searchPhrase);
    System.Threading.Thread.Sleep(3000);
    
    IList<IWebElement> results = this.WebDriver.FindElements(By.CssSelector(".tt-suggestion.tt-selectable"));
    if (results.Count > 1)
    {
    
        searchResult = results[1].FindElement(By.TagName("span")).GetAttribute("textContent");
    }
    
    0 讨论(0)
  • 2020-12-19 07:07

    Try this

    findElement(By.cssSelector("div.y6>span>b")).getText();
    
    0 讨论(0)
提交回复
热议问题