Selenium WebDriver + JUnit. It doesn't see the items that Selenium IDE detects



  • Good day to all! I switched from Selenium IDE to WebDriver + JUnit and I have the following problem: locators, which Selenium IDE saw before, does not see WebDriver. For example:

    IDE command

    waitForVisible -> xpath =. // * [@ id = 'listContainer']
    

    The element is being discovered.

    Command from JUnit:

    1.Assert.assertTrue ("Element isn't exist",! Driver.findElements (By.xpath (".//* [@ id = 'listContainer']")). IsEmpty ());
    

    Error: Element isn't exist

    or

    If you try to find by id

    2.Assert.assertTrue (driver.findElement (By.id ("listContainer")). IsDisplayed ());
    Error: NoSuchElementException: Unable to locate element ...
    

    Note:

    The timer is on and the elements on the page are loaded.

    Full code below:

    public class Simpletest {
    String baseUrl;
    WebDriver driver;
        @Before
        public void testSetup () {
            driver = new ChromeDriver ();
            baseUrl = "http://127.0.0.1:8000";
        }
    
        @After
        public void testShutDown () {
            driver.close ();
        }
    
        @Test
        public void shouldHaveTestAboutProfileSettings () {
    
            driver.get (baseUrl + "/SIMULATOR/index.html");
            wait (2);
        // Assert.assertTrue ("Element isn't exist",! Driver.findElements (By.xpath (".//* [@ id = 'listContainer']")). IsEmpty ());
           Assert.assertTrue (driver.findElement (By.id ("listContainer")). IsDisplayed ());
    
        }
    
      public void wait (int seconds) {
      try {
          Thread.sleep (seconds * 1000);
      } catch (InterruptedException e) {
          e.printStackTrace ();
      }
     }
    }
    

    HTML: HTML

    efb72f18-504a-4d4d-aab4-1e8bd90a08d5-image.png

    Ideally, I need to check for the presence of a clickItem with id = 0



  • I figured out what the problem was. Indeed, the element I was looking for associated frame, and the webdriver was looking for elements in the root of the html page. From this it turns out that the Selenium IDE sees at once the entire structure of the page and, accordingly, all the elements. This is probably why the problem did not arise before. As a result, this is solved by switching to the desired frame via the command:

    driver.switchTo (). Frame (driver.findElement (By.id ("here is the frame id")));
    

Log in to reply
 

Suggested Topics

  • 2
  • 3
  • 2
  • 2
  • 2