![](/img/trans.png)
[英]java selenium xpath to find element with text containing whitespaces
[英]Web Element is not identified by Xpath containing a text for Selenium WebDriver Java
我在Java中使用Selenium WebDriver。
以下是我的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。
您可以嘗試使用normalize-space()
//div[@id='servicetype-pp']//span[contains(text()[normalize-space()], 'Bill Generation Service')]
實際上,您的<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
,這意味着可見性不是問題,則此元素可能位於frame
或iframe
。 如果是這樣,則需要在找到以下元素之前切換該frame
或iframe
:
driver.switchTo().frame("frame name or id");
// Now go to find element
WebElement spanElement = driver.findElement(".//span[contains(text(),'Bill Generation Service')]");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.