org.openqa.selenium.NoSuchElementException: no such element

前端 未结 7 1949
深忆病人
深忆病人 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 13:57

    Right... so I managed to solve what was going on here. As I'm using IntelliJ, it seems I needed to tell it which order to call each method. So by adding @FixMethodOrder(MethodSorters.NAME_ASCENDING) and placing 'a' on my first method, then 'b' on my second method (I split the code I pasted in the question into methods since) and so on, it ran the methods in order with a wait on each method:

    try {
       Thread.sleep(10000);
    } catch (InterruptedException e) {
       e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
    
    0 讨论(0)
  • 2020-12-09 14:01

    Try use SeleniumWait library:

    SeleniumWait.withDriver(driver).withTimeOut(15).forElementToClick(element);
    
    0 讨论(0)
  • 2020-12-09 14:10

    It might be the browser issue. I had this problem and tried all sort of measures to fix it but didn't work until the Zoom percentage of the browser was fixed to 100%. this might sound a bit simple and funny but it worked for me.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-09 14:15

    did you open a new window? If yes, you need make driver to switch to the new window, following code is tested by me:

    String currentWindow = driver.getWindowHandle();// get handle of current window
    Set<String> handles = driver.getWindowHandles();// get handle of all windows
    Iterator<String> it = handles.iterator();
    while (it.hasNext()) {
    if (currentWindow == it.next()) {
    continue;
    }
    driver = driver.switchTo().window(it.next());// switch to new window
    
    //business action
    //xxxxxxx
    }
    driver.switchTo().window(currentWindow);//switch back to original window
    
    0 讨论(0)
  • 2020-12-09 14:20

    I guess your test fail sometimes due to the below statment

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

    Just place the timeout code once in ur begninning of the test and remove all other instances.

     chrome.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    
    0 讨论(0)
提交回复
热议问题