在 Selenium 中查找文本 Java
Find Text in Selenium Java
<div data-ng-if="!newRequestReceived" class="ng-scope">
<!-- ngIf: !authRequestCount --><p data-ng-if="!authRequestCount" class="ng-scope" style="" xpath="1">No more candidates awaiting Authorization</p><!-- end ngIf: !authRequestCount -->
<!-- ngIf: authRequestCount -->
</div>
我需要帮助,
如果找到类似“No more candidates awaiting Authorization
”的字符串
循环将如何工作???
driver.findElement(By.xpath("//p[@class='ng-scope'][contains('No more candidates awaiting Authorization']"));
这是正确的
要提取字符串 No more candidates waiting Authorization,您不需要这样的循环。由于该元素是一个 Angular 元素,您需要引入 WebDriverWait 并且您可以使用以下任一解决方案:
CssSelector
:
String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ng-scope[data-ng-if*='newRequestReceived'] p.ng-scope[data-ng-if*='authRequestCount']"))).getAttribute("innerHTML");
XPath
:
String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived')]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML");
<div data-ng-if="!newRequestReceived" class="ng-scope">
<!-- ngIf: !authRequestCount --><p data-ng-if="!authRequestCount" class="ng-scope" style="" xpath="1">No more candidates awaiting Authorization</p><!-- end ngIf: !authRequestCount -->
<!-- ngIf: authRequestCount -->
</div>
我需要帮助,
如果找到类似“No more candidates awaiting Authorization
”的字符串
循环将如何工作???
driver.findElement(By.xpath("//p[@class='ng-scope'][contains('No more candidates awaiting Authorization']"));
这是正确的
要提取字符串 No more candidates waiting Authorization,您不需要这样的循环。由于该元素是一个 Angular 元素,您需要引入 WebDriverWait 并且您可以使用以下任一解决方案:
CssSelector
:String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.ng-scope[data-ng-if*='newRequestReceived'] p.ng-scope[data-ng-if*='authRequestCount']"))).getAttribute("innerHTML");
XPath
:String myString = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived')]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML");