0

I'm new to selenium and am having trouble having it wait until an element is visible.

the element that shows after entering in incorrect credentials looks like this:

<div class="jss357">User with that username does not exist</div>

async function loginFailure(driver) {
    try {
      await driver.get('WEBSITE')
      await driver.findElement(By.name('username')).sendKeys(usernameBad)
      await driver.findElement(By.name('password')).sendKeys(passwordBad)
      await driver.findElement(By.css("button[type='submit']")).click()
      let elm = await driver.findElement(By.xpath("//div[contains(.,'User 
with that username does not exist')]"))
      await driver.wait(until.elementIsVisible(elm))
    } catch(e) {
      console.error('loginFailure failed.error: ', e.message)
    }
    finally {
        console.log('loginFailure finished')
    }
  }

loginFailure failed.error: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(.,'User with that username does not exist')]"}

ASSILI Taher
  • 1,210
  • 2
  • 9
  • 11
nikclel
  • 219
  • 1
  • 2
  • 10
  • If you set the implicit wait of the driver, then call the findElement method on an element you expect to be on the loaded page, the WebDriver will poll for that element until it finds the element or reaches the time out value. See here: https://stackoverflow.com/questions/5868439/wait-for-page-load-in-selenium – Jimmy Engelbrecht Jun 05 '19 at 20:59

3 Answers3

1

Try this,

let el = await driver.findElement("ByLocator_Here");

await driver.wait(until.elementIsVisible(el),100);

Also, Check out the seeleniumhq docs here https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/until.html

Magesh
  • 308
  • 1
  • 4
  • 18
0

I use java, but you should have somewhat similar syntax

wait.until(elementAvailable(By.className("jss357")));

--- revisiting this answer. I'm curious if you change it a little, and before assigning value to elm, wait for the presence of the element. I think you are getting the error because when initially asigning html element to the variable it's not present on the page yet. You might need to be explicit about waiting for it's presence.

So it will be something like this:

wait.until(elementAvailable(By.xpath("//div[contains(.,'User with that username does not exist')]"));
let elm = driver.findElement(By.xpath("//div[contains(.,'User with that username does not exist')]"))
Uma
  • 836
  • 5
  • 7
-1

I guess your locator is not correct, because error says "no such element: Unable to locate element" not sure why need that . before expected text. You can make a search by exact text not just contains.

Your locator should be -

driver.findElement(By.xpath("//div[text() = 'User with that username does not exist']"))

If its not gonna help, pls reply.

IPolnik
  • 619
  • 5
  • 13
  • did let el = await driver.findElement(By.xpath("//div[text() = 'User with that username does not exist']")) and came out with the same exact error – nikclel Jun 05 '19 at 21:31