硒等待直到不等待元素可见

时间:2020-08-02 17:56:49

标签: node.js selenium selenium-webdriver mocha

我正在使用NodeJS,Mocha和Selenium来自动填写时间表,并且在等待登录表单时遇到问题。

URL“ https://timesheet.mydomain.com/timesheet.aspx”将打开重定向到我公司的登录页面,我需要等待直到登录输入可见。

用户名文本框可以由ID-用户ID标识,我添加了以下行以等待其可见。但是,它无法等待并显示以下错误。

我该如何解决?我试图在Stackoverflow和其他论坛中已经回答的问题中找到解决方案,但是什么也没有。请帮助解决此问题。

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("userid@mydomain.com")
    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)

1 个答案:

答案 0 :(得分:0)

您是否尝试过让Selenium尝试通过XPath查找元素?有时它找不到ID,我只使用XPath,因为它们可以工作。 那应该是:

 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("userid@mydomain.com")
    await driver.findElement(By.XPATH("XPATH")).sendKeys(Key.ENTER)

您还可以使用隐式等待方法,例如:

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