我想为以下div编写xpath:
<div class='someclass' id='someid'>:TEST SELENIUM 1234<div>请注意:
可以出现在文本的任何地方。到目前为止,我所做的尝试了:
//div[contains(text(),":TEST SELENIUM 1234")]
//div[contains(text(),":TEST{ }SELENIUM{ }1234")]
//div[contains(text(),":TEST SELENIUM 1234")]
//div[normalize-space(text()) = ':TEST SELENIUM 1234']
//div[normalize-space(text()) = ':TEST{ }SELENIUM{ }1234']
//div[normalize-space(text()) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST{ }SELENIUM{ }1234']
//div[normalize-space(.) = ':TEST SELENIUM 1234']
//div[normalize-space(.) = ':TEST{\u00a0}SELENIUM{\u00a0}1234']
//div[normalize-space(.) = ':TEST${nbsp}SELENIUM${nbsp}1234'] 对我有用的东西(感谢@Andersson)
//div[starts-with(text(), ":TEST") and substring(text(), 7)="SELENIUM" and substring(text(), 16)="1234"] 这是一项更多的工作,并将只适用于已知的字符串。
以下是我已跟进的职位:
任何帮助都将不胜感激。
发布于 2018-08-07 06:18:18
根据上面的对话,您可以使用这个示例xPath生成器(JAVA):
public class Test {
public static void main(String[] args) {
String s = ":TEST SELENIUM 1234";
String[] parts = s.split(" ");
StringBuilder xpath = new StringBuilder("//*");
for (int i = 0; i < parts.length; i++){
xpath.append((i == 0) ? "[contains(text(), '" + parts[i] + "')" : " and contains(text(), '" + parts[i] + "')");
}
xpath.append("]");
System.out.println(xpath);
}
}输出:
//*[contains(text(), ':TEST') and contains(text(), 'SELENIUM') and contains(text(), '1234')]发布于 2018-08-07 08:23:01
在Python里我会做
required_div = [div for div in driver.find_elements_by_xpath('//div') if div.text == ':TEST SELENIUM 1234'][0] 若要通过其完整的文本内容查找所需的节点,请忽略不间断的空格字符。
再说一次,这只是一个解决办法,但它似乎是相当简单的解决方案
https://stackoverflow.com/questions/51719379
复制相似问题