Home > database >  Selenium: How do I check if an element is on a page without use implicitlyWait?
Selenium: How do I check if an element is on a page without use implicitlyWait?

Time:01-13

How do I check if an element is on a page without use function implicitlyWait? I may check that element is on page with use implicitlyWait:

    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

    if (driver.findElements(By.cssSelector(MY_CSS_SELECTOR)).size() != 0) {
        //...
    }

Is there any analogue of the solution?

CodePudding user response:

You are already using findElements, Note that findElements will not throw any error even the passed locators is not available in HTMLDOM.

If it finds, it will return a list of web elements.

Now findElements will try to POLL the DOM if it does not find anything immediately, so that's the reason we use ImplicitWait.

Since you've mentioned that you do not wanna deal with ImplicitWait, and wants to grab the list of web elements, you either have to use ExplicitWait

WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("css_selector_here")));

or

wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.cssSelector("css_selector_here")));

But note that using explicit wait, if elements are not found then You will likely get TimeOutException.

ExplicitWait:

Explicit waits are available to Selenium clients for imperative, procedural languages. They allow your code to halt program execution, or freeze the thread, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value, it will keep trying and waiting.

Since explicit waits allow you to wait for a condition to occur, they make a good fit for synchronizing the state between the browser and its DOM, and your WebDriver script.

ImplicitWait:

There is a second type of wait that is distinct from explicit wait called implicit wait. By implicitly waiting, WebDriver polls the DOM for a certain duration when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.

Implicit waiting for elements to appear is disabled by default and will need to be manually enabled on a per-session basis. Mixing explicit waits and implicit waits will cause unintended consequences, namely waits sleeping for the maximum time even if the element is available or condition is true.

Warning: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example, setting an implicit wait of 10 seconds and an explicit wait of 15 seconds could cause a timeout to occur after 20 seconds.

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0, meaning disabled. Once set, the implicit wait is set for the life of the session.

Official Docs

CodePudding user response:

You need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use the following solution:

if(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("MY_CSS_SELECTOR"))).size() != 0) {
    //...
}

CodePudding user response:

If you don't want to worry about waits at at, you can use a Selenium Python framework called SeleniumBase that handles all the waiting for you. You can use assert statements to verify elements on a page. Install with pip install seleniumbase, then run tests with pytest. Here's an example test:

from seleniumbase import BaseCase

class DemoSiteTests(BaseCase):
    def test_demo_site(self):
        self.open("https://seleniumbase.io/demo_page")

        # Assert that the element is visible on the page
        self.assert_element("tbody#tbodyId")

        # Assert that the text appears within a given element
        self.assert_text("Demo Page", "h1")

        # Type text into a text field
        self.type("#myTextInput", "This is Automated")

        # Verify that a button click changes text on the page
        self.assert_text("This Text is Green", "#pText")
        self.click('button:contains("Click Me")')
        self.assert_text("This Text is Purple", "#pText")

        # Verify that a slider control updates a progress bar
        self.assert_element('progress[value="50"]')
        self.press_right_arrow("#myslider", times=5)
        self.assert_element('progress[value="100"]')
  •  Tags:  
  • Related