How to use xpath for extracting text between <br/> tag

半世苍凉 提交于 2021-02-08 08:40:30

问题


Following is the code of a button in my application.

<span class="ms-cui-ctl-largelabel" unselectable="on">
New
<br>
Map
</span>
</a>
</span>

I want to click on that button and using Selenium Webdriver for it. I tried multiple combinations but it's not working for me. Following are the different xpaths which I tried

1. By.xpath("//span[contains(@class, 'ms-cui-ctl-largelabel') and text() = 'New/nMap']"));
2. By.xpath("//span[contains(@class, 'ms-cui-ctl-largelabel') and text() = 'New/r/nMap']")); 
3. By.xpath("//span[contains(@class, 'ms-cui-ctl-largelabel') and text() = 'New Map']"));

Could someone please help ?

Thanks


回答1:


Alternative solution using normalize-space()

//span[@class="ms-cui-ctl-largelabel" and normalize-space()="New Map"]



回答2:


You can use anyone of these xpath:

  1. //span[@class='ms-cui-ctl-largelabel'][contains(., 'New')][contains(., 'Map')]

  2. //span[contains(., 'New')][contains(., 'Map')]

  3. //span[@class='ms-cui-ctl-largelabel']

  4. //span[@class='ms-cui-ctl-largelabel'][contains(., 'New')]

  5. //span[@class='ms-cui-ctl-largelabel'][contains(., 'Map')]

All are tested xpath.




回答3:


You can use the following xpath to find span by text with <br> and class:

//span[contains(.,'New') and contains(.,'Map') and @class='ms-cui-ctl-largelabel']



回答4:


Try using this line:

//span[translate(string(),' ','')=translate('New Map',' ','')]


来源:https://stackoverflow.com/questions/23268927/how-to-use-xpath-for-extracting-text-between-br-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!