无法使用chrome上的webdriver获取/断言文本

时间:2014-08-05 09:17:33

标签: selenium-webdriver webdriver selenium-chromedriver

我有一个html页面,其中dom包含具有以下代码的span 我需要获取所选的文本/值“PortfolioPlan”。

我尝试了使用cssSelector,xpath的不同选项..但是没有什么能用于最新的chrome,但同样适用于firefox。
请帮我在这里修复chrome固定,下面是我试过的几个

assertPageContainsText(By.xpath( "//*[@id='pm_workspace']/div/div[2]/span/span[2]"),"PortfolioPlan");
    assertPageContainsText(By.xpath("//option[@selected='true']") , "PortfolioPlan");
    assertPageContainsText(By.xpath(     "//div[@id='pm_workspace']/div/div[2]/span/span"),"PortfolioPlan");
    assertPageContainsText(By.cssSelector( ".pm_toolbar_menu_input"),"PortfolioPlan");
    assertPageContainsText(By.cssSelector("#pm_workspace > div > div.pm_workspace_title_row > span > span.pm_toolbar_menu.pm_toolbar_menu_input"),"PortfolioPlan");

//下面是dom源

<span class="pm_toolbar_menu pm_toolbar_menu_input" role="menubar" aria-hidden="false" tabindex="0" style="min-width:? 184px;? display:? inline;? top:? 22px;? left:? 11px;? z-index:? 11110;? background-color:? rgb(252, 215, 65)?;">
<table border="0" cellspacing="0" cellpadding="0" width="100%" role="presentation">
<tbody>
<tr>
<td class="pm_toolbar_label" nowrap="true">
<label for="d8372e51">Plan</label>
</td>
<td class="pm_toolbar_cell" nowrap="true">
<table cellspacing="0" role="presentation">
<tbody>
<tr class="tableNestedAttribute">
<td class="tableNestedAttribute">
<select name="displayedPlan" id="d8372e51" style class="pm_field formFieldNoWidth" onchange="javascript:jumpMenu(this);">
<option value="navigateTo('page','fm.setPlanAsCurrent','selector=0','portfolio_id=5001001','df_return_to=app?action=fm.fmPriorityView&id=5001001')" data-pm="0">[--Portfolio--]</option>
<option value selected="true" data-pm="5000001">PortfolioPlan</option>
</select>
</td>
<td class="actions"></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</span>

由于 萨姆

1 个答案:

答案 0 :(得分:0)

我个人总是使用OpenQa.Selenium.Support.UI.Select,然后从属于Select元素的选项中进行选择。 C#

public string getSelectedOption()
{
    OpenQa.Selenium.Support.UI.Select select = new OpenQa.Selenium.Support.UI.Select(driver.FindElement(By.id("d8372e51")));
    string opt = "";

    foreach(var option in select.options)
    {
        if (option.selected)
           opt = option.text;
    }
    return opt;
}