Selenium webdriver Selenium-如果网页上的WebElement不可用,则无法获取异常/失败

Selenium webdriver Selenium-如果网页上的WebElement不可用,则无法获取异常/失败,selenium-webdriver,element,Selenium Webdriver,Element,我知道这个问题在这个论坛上被问了很多次。然而,在尝试了我在站点和其他资源中找到的所有解决方案之后,我再次发布了这篇文章。 至少我需要张贴在论坛上的帮助这个 我的产品是一个web内容过滤器,允许用户浏览internet或对其进行过滤(基于他拥有的权限) 因此,我的测试用例针对的是一个用户,他可以查看或不能查看基于授予他的权限的页面 有效的测试用例是 若用户拥有查看页面的权限,这可以正常工作,因为selenium能够检测元素并向前移动。在本例中,我围绕try-catch块使用了断言(之后,我也尝试了

我知道这个问题在这个论坛上被问了很多次。然而,在尝试了我在站点和其他资源中找到的所有解决方案之后,我再次发布了这篇文章。 至少我需要张贴在论坛上的帮助这个

我的产品是一个web内容过滤器,允许用户浏览internet或对其进行过滤(基于他拥有的权限)

因此,我的测试用例针对的是一个用户,他可以查看或不能查看基于授予他的权限的页面

有效的测试用例是 若用户拥有查看页面的权限,这可以正常工作,因为selenium能够检测元素并向前移动。在本例中,我围绕try-catch块使用了断言(之后,我也尝试了很多其他东西)

测试用例无效 如果用户没有权限,则无法查看页面。所以,每当他试图进入任何被黑名单的网站时,他都会得到一个被阻止的页面(这个页面完全不同)。 所以我知道硒试图找到的元素在这种情况下并不存在。但在执行过程中,我的脚本被卡住了&没有捕获异常并向前移动。 尝试过调试&在那里,当搜索元素的行到达时,它也会停止。 我原以为脚本在捕获异常后会向前移动。 我希望看到这个测试用例也失败了

我的一位朋友建议与开发人员一起检查DOM中是否存在该元素。我不确定这会有什么不同。(任何关于这方面的建议都会帮助我学习)

下面是我在代码中尝试过的东西

片段1

public static void main(String[] args){
-------some code---
System.out.println("User Logs in");
d1.get("http://www.maxim.com");
System.out.println("Site will be Blocked");
Assert.assertTrue(doesElementExist(By.id("block-system-user-menu")));
d1.quit();
}
public static boolean doesElementExist(By locator) {
if (d1.findElements(locator).size() > 0) {
return true;
} else {
return false;
}

片段2

d1.get("http://www.maxim.com");
By myLocator = By.id("block-system-user-menu");
if (isElementPresent(myLocator)) {
WebElement myElement = d1.findElement(myLocator);
myElement.getText();
     } 

public static boolean isElementPresent(By locator) {
return d1.findElements(locator).size() > 0;
}

片段3

private static boolean verifyElementAbsent() throws Exception {
try {
d1.findElement(By.xpath("//*[@id='block-system-user-menu']/div/div/ul/li/a"));
System.out.println("Element Present");
return false;
} catch (NoSuchElementException e) {
System.out.println("Element absent");
return true;
}

片段4

d1.get("http://www.maxim.com");
String S1 = "//*[@id='block-system-user-menu']/div/div/ul/li/a";
verifyElementAbsent(S1);
d1.quit();
}
private static boolean verifyElementAbsent(String locator) throws Exception {
boolean visible = d1.findElement(By.xpath(locator)).isDisplayed();
boolean result = !visible;
System.out.println(result);
return result;

片段5

boolean titleTextfield = d1.findElement(By.id("block-system-user-menu")).isDisplayed();
Assert.assertFalse(titleTextfield, "Title text field present which is not expected");

片段6

WebDriverWait wait1 = new WebDriverWait(d1,    20);WebElementelement2=wait1.until(ExpectedConditions.visibilityOfElementLocated(By.id("block-system-user-menu")));

片段7

isElementPresent(By.id("block-system-user-menu"));
public static boolean isElementPresent(By by){
try{
d1.findElement(by);
return true;
}catch(NoSuchElementException e){
return false;
}
}

片段8

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(presenceOfElementLocated(locator));

片段9

Boolean isPresent = driver.findElements(By.yourLocator).size() > 0

任何帮助都将不胜感激。 谢谢
Amar

正如您的朋友所建议的,如果您的DOM还没有准备好,请尝试将其置于睡眠状态几毫秒,或者等待该元素显示出来。尝试了一下&它不起作用。我只是好奇,当一个全新的页面出现时,人们是如何处理这种情况的&selenium试图在上面找到对象。