org.openqa.selenium.NoSuchElementException: no such element

前端 未结 7 2005
深忆病人
深忆病人 2020-12-09 13:35

Running Selenium WebDriver 2.37.1

I\'m receiving an intermittent problem when running a test and receive the following error:

org.openqa.selenium.No         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 14:11

    As Yuvaraj HK has mentioned ,using implicit wait just once in your code would be enough.It'l implicitly wait for every element that you try to find in your code.

    chrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    

    But try to keep implicit wait time as low as possible, because this might increase your code execution time..

    In some cases the element might take more than 30 seconds to be visible, Explicit wait can be used in these kind of situations.

    WebDriverWait some_element = new WebDriverWait(driver,100); 
    some_element.until(ExpectedConditions.visibilityOfElementLocated(By.id("id_of_some_element")));
    //do anything you want with some_element
    

    I strongly suggest using cssSelectors over xpath. This article might help.
    Even if xpath is used, try using shorter ones. Using an id is not the only way to reach an element. Its parent might have unique class names or other attributes, which you can use to create efficient xpaths or cssSelectors.

提交回复
热议问题