简体   繁体   English

Selenium-PHP错误-查找标记中未包含的文本

[英]Selenium - PHP error - Finding text not contained in an tag

I find elements either by their ID or tag or etc. But my element is in a body tag with no tags at all, how can I find this? 我可以通过其ID或标签等找到元素。但是我的元素位于完全没有标签的body标签中,如何找到它? I know it is in the body tag but there are other elements too! 我知道它在body标签中,但是还有其他元素! The "text I want to find" is a php error displayed and I am hoping to catch that. “我要查找的文本”是显示的php错误,我希望能够捕捉到。 I usually go writing WebElement x = driver.findElement(By.??); 我通常会写WebElement x = driver.findElement(By.??); I cant proceed because I am uncertain what to do. 我无法继续,因为我不确定该怎么办。

Sample HTML doc 样本HTML文档

<head></head>

<body>

Text I want to find

<div>xx</div>
<div>yy</div>

</body>

The reason for the java tag is, I am using Java to write my code? java标签的原因是,我使用Java编写代码?

In your situation I'd have used "context item expression" ie a . 在您的情况下,我会使用“上下文项表达式”,即. (dot) operator. (点)运算符。 So if I write an Xpath like this: 因此,如果我编写这样的Xpath:

//div[contains(.,'Text To Be Searched')]

Then it will find all div elements which contain text Text To Be Searched . 然后它将找到所有包含Text To Be Searched div元素。 For you my answer would be 对你来说我的答案是

driver.findElement(By.xpath("//body[contains(.,'Text I want to find')]"));

您应该在p标签内添加该文本,然后可以编写:

WebElement x = driver.getElementByTag('p');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM