2016-08-04 65 views
0

我正在使用Selenium WebDriver和Java。Web元素不能被包含Selenium WebDriver的文本的Xpath识别Java

下面是我的html代码:

<div id="servicetype-pp" class="z-combobox-popup " style="display: none; width: auto;"> 
    <ul id="servicetype-cave" class="z-combobox-content"> 
     <li id="zk_comp_140" class="z-comboitem"> 
     <li id="zk_comp_141" class="z-comboitem"> 
      <span class="z-comboitem-image"></span> 
      <span class="z-comboitem-text">Bill Generation Service</span> 
     </li> 
     <li id="zk_comp_142" class="z-comboitem"> 
     <li id="zk_comp_143" class="z-comboitem"> 
     <li id="zk_comp_144" class="z-comboitem"> 
     <li id="zk_comp_145" class="z-comboitem"> 
     <li id="zk_comp_146" class="z-comboitem"> 
     <li id="zk_comp_147" class="z-comboitem"> 
     <li id="zk_comp_148" class="z-comboitem"> 
     <li id="zk_comp_149" class="z-comboitem"> 
     <li id="zk_comp_150" class="z-comboitem"> 
    </ul> 
</div> 

我已经定义与XPath包含特定文本WebElement。

//div[@id='servicetype-pp']//span[contains(text(),'Bill Generation Service')] 

它不工作。但是,当我用一个字搜索时,没有任何空间,它工作正常。

//div[@id='servicetype-pp']//span[contains(text(),'Bill')] or 
//div[@id='servicetype-pp']//span[contains(text(),'Generation')] or 
//div[@id='servicetype-pp']//span[contains(text(),'Service')] 

看来这是空间问题。

请帮忙。

TIA。

+0

你能尝试使用刚刚全文,而不是包含API,例如搜索: - driver.findElement(By.xpath( “// * [text()='Bill Generation Service']”)); –

+0

@AnupamSaini:试过了。没有工作。 :( –

+0

@BhargavRaval你是什么意思没有工作..是否有任何例外?? –

回答

2

您可以尝试使用normalize-space()

//div[@id='servicetype-pp']//span[contains(text()[normalize-space()], 'Bill Generation Service')] 
+0

试过了。没有工作。:( –

+0

@BhargavRaval你也可以试试'// div [@ id ='servicetype-pp'] // span [normalize-space(。)='Bill Generation Service'] ' – Guy

+0

和'// div [@ id ='servicetype-pp'] // span [text()[n ormalize-space(。)] ='Bill Generation Service']' – Guy

0

其实你<div id="servicetype-pp" class="z-combobox-popup " style="display: none; width: auto;">看起来无形的,这就是为什么你不能够与要素互动,尝试如下: -

WebElement el = driver.findElement(By.id("servicetype-pp")); 
el = (WebElement)((JavascriptExecutor)driver).executeScript("arguments[0].style.display = 'block';return arguments[0]", el); 

//Now you can find your desire element 
WebElement spanElement = el.findElement(".//span[contains(text(),'Bill Generation Service')]"); 

//Now do your further steps with this element 

编辑: - 如果您获得NuSuchElementExcpetion,这意味着可见度不是问题,该元素可能位于frameiframe。如果是,你需要切换该frameiframe寻找元素,如下之前:

driver.switchTo().frame("frame name or id"); 

// Now go to find element 
WebElement spanElement = driver.findElement(".//span[contains(text(),'Bill Generation Service')]"); 
+0

我不认为风格是这里的问题。我有其他没有'style = display:none'的元素。对他们来说,我也面临同样的问题。 –

+0

@BhargavRaval好的,你找帧或iframe?确保这个元素不在任何框架或iframe里面... –

+0

如果你得到nosuchelement可能是这是在任何帧...检查它并让我知道.. –

相关问题