我正在使用Selenium中的功能文件,尝试读取状态按钮时遇到问题。如果状态符号设置为“已邀请”,则应该切换到其他管理员帐户应用程序并批准邀请。但是,在这种情况下不这样做。有什么建议吗?
public void disconnectFromUser(String user) {
logger.info("Making sure that I am not connected to PurpleHS user");
iframeExit();
searchForUser(user);
communityFrame();
try {
String buttonText = driver.findElement(By.className("cp-ur-link-wrapper ")).findElement(By.tagName("a")).getText();
if (buttonText.equals("Invited")) {
acceptConnectionRequestByHSUser();
searchForUser(user);
communityFrame();
clickDisconnectBtn();
} else {
clickDisconnectBtn();
}
} catch (NoSuchElementException ex) {
logger.info("The user is not a connection.");
driver.findElement(By.className("close")).click();
waitUntilPageFinishLoading();
}
}
以下是一些HTML:
<div id="cp-ur-17417" class="cp-ur-link-wrapper "><a href="#" title="Invited" class="ur-pending ur-request-link ur-disabled-link">Invited</a></div>
答案 0 :(得分:0)
对于这一行代码:
String buttonText = driver.findElement(By.className("cp-ur-link-wrapper ")).findElement(By.tagName("a")).getText();
您只需将其替换为 linkText :
String buttonText = driver.findElement(By.linkText("Invited")).getText();
在代码中:
if (buttonText.equals("Invited")) {
System.out.println("Inside if else ladder")
acceptConnectionRequestByHSUser();
searchForUser(user);
communityFrame();
clickDisconnectBtn();
} else {
clickDisconnectBtn();
}
答案 1 :(得分:0)
您的类名应为ur-pending ur-request-link ur-disabled-link
而不是cp-ur-link-wrapper
,并且无需再次按标记名查找。
或者您也可以尝试xpath
//a[@title='Invited'][@class='ur-pending ur-request-link ur-disabled-link']
因此您的代码行将变为:
String buttonText = driver.findElement(By.xpath("//a[@title='Invited'][@class='ur-pending ur-request-link ur-disabled-link']")).getText();