繁体   English   中英

在运行代码时查找网络元素始终失败,但在调试模式下通过

[英]Finding a webelement always fails while running the code but passes in debug mode

在下面的代码中,我传递了url和一个tag

tag用于标识页面中的div / section。 我试图获取一个部分下的所有url并与传递的url进行比较并尝试匹配。

这里的问题是,当我在调试模式下运行此代码时,我可以获取URL列表并在列表中找到匹配的url ,但是当我运行下方的代码时, assert语句始终无法说出在列表中找不到url 我通过引入Web元素等待和其他sleep方法来尽力解决此问题。 似乎没有任何作用。 你能帮我解决这个问题吗?

代码

public void validateEndpoints(String url, String tag){
    tag = tag.trim();
    if(tag.contains("/"))
        tag = tag.replace("/", "_");

    WebElement resourceLink = driver.findElement(By.cssSelector("#resource_"+tag+"_"+" .heading a"));
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(resourceLink));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", resourceLink);

    By mySelector = By.cssSelector("#"+tag+"__endpoint_list .path a");  
    WebDriverWait wait2 = new WebDriverWait(driver, 30);
    wait2.until(ExpectedConditions.elementToBeClickable(mySelector));
    List<WebElement> urlList = driver.findElements(mySelector);

    List<String> resourceName = new ArrayList<String>();

    for (WebElement e : urlList){ 
        resourceName.add(e.getText());          
    }
    executor.executeScript("arguments[0].click();", resourceLink);
    Assert.assertTrue(resourceName.contains(url), "End point "+ url +" not found in the resource " + tag + "...");      
}

也对网址使用显式等待

List<WebElement> urlList = wait2.until(ExpectedConditions.presenceOfAllElementsLocatedBy(mySelector));

暂无
暂无

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

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