Zeus
Zeus

Reputation: 571

Selenium wait until is not waiting until the element is visible

I'm using NodeJS, Mocha & Selenium to automate filling my timesheet and I'm facing issue in waiting for the login form.

The URL "https://timesheet.mydomain.com/timesheet.aspx" will open redirect to the login page of my company and I need to wait until the login inputs are visible.

The username text box can be identified by the id - userid and i've added the below line to wait for it's visibility. However, it fails to wait and showing the following error.

How do i fix this ? I've tried to get solutions for this in already answered questions in Stackoverflow and other forums, but couldn't get any. Please help to fix this.

Protractor Async/Await Error: Unhandled promise rejection

const { Builder, By, Key, until } = require('selenium-webdriver')
const assert = require('assert')

describe('Timesheet', function() {
  this.timeout(30000)
  let driver
  let vars
  beforeEach(async function() {
    driver = await new Builder().forBrowser('chrome').build()
    vars = {}
  })
  afterEach(async function() {
    await driver.quit();
  })
  it('Timesheet', async function() {
    await driver.get("https://timesheet.mydomain.com/timesheet.aspx")
    await driver.manage().window().setRect(1920, 1053)
    await driver.wait(until.elementIsVisible(await driver.findElement(By.id("userid"))), 60000)
    await driver.findElement(By.id("userid")).sendKeys("[email protected]")
    await driver.findElement(By.id("userid")).sendKeys(Key.ENTER)
  })
})

./node_modules/.bin/mocha timeSheet.js

       Timesheet:
     NoSuchElementError: no such element: Unable to locate element: {"method":"css selector","selector":"*[id="userid"]"}
  (Session info: chrome=83.0.4103.116)
      at Object.throwDecodedError (node_modules/selenium-webdriver/lib/error.js:550:15)
      at parseHttpResponse (node_modules/selenium-webdriver/lib/http.js:565:13)
      at Executor.execute (node_modules/selenium-webdriver/lib/http.js:491:26)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
      at async Driver.execute (node_modules/selenium-webdriver/lib/webdriver.js:700:17)
      at async Context.<anonymous> (timeSheet.js:18:46)

Upvotes: 2

Views: 2364

Answers (2)

spockmonster
spockmonster

Reputation: 41

This happens intermittently with my first use of selenium. I am using Selenium.WebDriver nuget version 4.10, with ChromeDriver.

One thing I have done is create a new WebDriverWait object on every use. THe thought was perhaps the Wait timeout is not resetting for the second use of .Until. This seemed to reduce the occurrences of it not waiting at all and failing immediately, but it coudl be happenstance.

But it still happens sometimes. Second thing I tried is make sure not to touch the mouse while test is running. Maybe mouse events mess with the ChromeDriver.

Third thing I am trying is to use the IClock, and configure the SleepInterval.

    private static WebDriverWait GetWaiter(IWebDriver driver)
    {
        IClock clock = new SystemClock();
        return new WebDriverWait(clock, driver, TimeSpan.FromSeconds(9), TimeSpan.FromSeconds(1));
    }

Upvotes: 0

Razorbackfire
Razorbackfire

Reputation: 58

Have you tried making selenium trying to find the element by XPath ? Sometimes it can't find the ID, I only use XPaths because they ever work. Then it would be:

 it('Timesheet', async function() {
    await driver.get("https://timesheet.mydomain.com/timesheet.aspx")
    await driver.manage().window().setRect(1920, 1053)
    await driver.wait(until.elementIsVisible(await driver.findElement(By.XPATH("XPATH"))), 60000)
    await driver.findElement(By.XPATH("XPATH")).sendKeys("[email protected]")
    await driver.findElement(By.XPATH("XPATH")).sendKeys(Key.ENTER)

You could also use the implicitly wait method, such as below:

driver.implicitly_wait(15) #seconds to wait
await = driver.find_element_by_xpath("//*[@id='--ID--']") #Put in ID or XPATH
await.click() #If the driver is able to find the element within the time you set it to wait for, it will go on doing everything written under the implicitly wait line

Upvotes: 1

Related Questions