我无法使用Selenium webdriver从下面的代码中获取文本“Ready”

时间:2016-05-31 07:09:01

标签: selenium webdriver

我首先需要找到元素“Ready”,然后检查它是否显示在屏幕上。请帮忙

html代码: -

<div class="sf-element sf-element-text-box sfpc-left" style="float: left;" title="Ready ">Ready </div>

这是我尝试的内容

首先尝试: -

WebElement search = driver.findElement(By.cssSelector("span[title=\"Ready \"]"));
if(search.isDisplayed()){ . I will proceed with other things here.... }

第二次尝试: -

WebElement search = driver.findElement(By.xpath("//span[contains(text(),'Ready ')]"));
if(search.isDisplayed()){. I will proceed with other things here.... }

但这不起作用抛出异常..没有找到元素。请帮忙

3 个答案:

答案 0 :(得分:0)

您正在寻找一个span元素:

By.cssSelector("span[title=\"Ready \"]")

但是你的html显示了一个div元素:

<div title="Ready ">Ready </div>

将您的定位器更改为:

By.cssSelector("div[title=\"Ready \"]")

答案 1 :(得分:0)

C#

IWebElement test = driver.FindElement(By.CssSelector(&#34; div [title = \&#34; Ready \&#34;]&#34;)); Console.WriteLine(test.Text);

<强>演示:

        IWebDriver driver;
        string url = "http://localhost:58807/Test1.html";
        var options = new InternetExplorerOptions();
        options.RequireWindowFocus = true;
        options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        options.IgnoreZoomLevel = true;
        string path = Environment.CurrentDirectory;
        driver = new InternetExplorerDriver(path, options); 
        TimeSpan span = TimeSpan.FromSeconds(60);

        driver.Manage().Timeouts().ImplicitlyWait(span);
        driver.Manage().Timeouts().SetPageLoadTimeout(span);
        driver.Manage().Window.Maximize();
        driver.Navigate().GoToUrl(url);

        IWebElement test = driver.FindElement(By.CssSelector("div[title=\"Ready \"]"));
        Console.WriteLine(test.Text);

答案 2 :(得分:-1)

//*[contains(concat(' ', @title, ' '), ' Ready ') and contains(text(), 'Ready')]

driver.findElement(By.xpath("//*[contains(concat(' ', @title, ' '), ' Ready ') and contains(text(), 'Ready')]"))

中尝试上述xpath

还要小心空间!!

编辑:即使删除了标题属性中的空格,上面的xpath也能正常工作