0

i want to use selenium to find element asap when the DOMcontentLoad

how canfindElement execute do not wait until the page loaded?

var webdriver = require('selenium-webdriver'),
    By = webdriver.By,
    until = webdriver.until

;(async function main(){
    driver =await new webdriver.Builder().forBrowser('chrome').build()
    await driver.get('some url')//wait until it throw timeout error
    ele=await driver.findElement(By.id('username'))
    ele.sendKeys('xxx')
})()

i try to use

await driver.manage().setTimeouts({pageLoad:3e3,script:2e3})

but after catch errors, all promises are timeouted

environment

  • nodejs

  • "selenium-webdriver": "^4.0.0-alpha.1"

  • chromedriver 73.0.3683.20


finally, my nodejs solution:

var {Options} = require('selenium-webdriver/chrome'),
    {Builder,By,until,Capabilities}=require('selenium-webdriver'),
    driver;
;(async function main(){
    driver =await new Builder()
        .withCapabilities(
            Options.chrome().setPageLoadStrategy('none')
        ).build()
})()
seasonley
  • 33
  • 6

1 Answers1

0

Selenium web driver supports three page-load strategies.

normal

This stategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).

eager

This stategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).

none

This strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).


You have to set your page load strategy to eager. Although chrome doesn't support 'eager', you can set it to 'none'. Then you need to synchronize driver wait for some elements.

Python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

option = Options()
option.set_capability("pageLoadStrategy","eager")

driver = webdriver.Chrome(executable_path="chromedriver.exe",options=option)

S Ahmed
  • 1,454
  • 1
  • 8
  • 14