简体   繁体   English

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

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

In the below code I am passing url and a tag . 在下面的代码中,我传递了url和一个tag

tag is used to identify a div/section in a page. tag用于标识页面中的div / section。 I am trying to get all the url s under a section and comparing with the passed url and trying to match. 我试图获取一个部分下的所有url并与传递的url进行比较并尝试匹配。

Here the problem is when I run this code in the debug mode, I am able get a list of urls and find a matching url in the list but when I run the code below assert statement always fails saying url not found in the list. 这里的问题是,当我在调试模式下运行此代码时,我可以获取URL列表并在列表中找到匹配的url ,但是当我运行下方的代码时, assert语句始终无法说出在列表中找不到url I tried my level best to fix this by introducing web elements wait and and other sleep methods. 我通过引入Web元素等待和其他sleep方法来尽力解决此问题。 Nothing seems to be working. 似乎没有任何作用。 Can you please help me to fix this? 你能帮我解决这个问题吗?

Code : 代码

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