Asked  7 Months ago    Answers:  5   Viewed   77 times

I want to make sure that an element is present before the webdriver starts doing stuff.

I'm trying to get something like this to work:

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 0, 5));

I'm mainly struggling how to setup up the anonymous function...



Alternatively you can use an implicit wait:

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

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. Once set, the implicit wait is set for the life of the WebDriver object instance.

Tuesday, June 1, 2021
answered 7 Months ago

From the Selenium Documentation PDF :

import contextlib
import selenium.webdriver as webdriver
import as ui

with contextlib.closing(webdriver.Firefox()) as driver:
    wait = ui.WebDriverWait(driver,10)
    # Do not call `implicitly_wait` if using `WebDriverWait`.
    #     It magnifies the timeout.
    # driver.implicitly_wait(10)  

    wait.until(lambda driver: driver.title.lower().startswith('cheese!'))

    # This raises
    #     selenium.common.exceptions.TimeoutException: Message: None
    #     after 10 seconds
    wait.until(lambda driver: driver.find_element_by_id('someId'))
Saturday, June 5, 2021
answered 7 Months ago

I stumbled upon an answer to my question

So to wait for an element to appear we have to:

driver.wait(function () {
    return driver.isElementPresent("username"));
}, timeout);
Thursday, June 10, 2021
answered 7 Months ago
 element = WebDriverWait(driver, 10).until(
            EC.invisibility_of_element_located((By.ID, 'button'))

you don't need to use while. it already waits for time that you present in WebDriverWait() function.

Saturday, August 14, 2021
answered 4 Months ago

As per the screenshot you have shared the element is clearly within a #shadow-root (closed).

We have been discussing about Shadow DOM traversal support through Selenium for quite some time now and the key take aways are:

  • @43081j mentioned, for closed shadow roots it should just be accepted that you can't access it (throw an exception). So, we don't need to care about closed roots as we shouldn't be able to access them anyway.
  • @AutomatedTester in his spec proposal suggested the ability to move between a Shadow DOM context and the document context.

So currently you won't be able to interact with the elements within the #shadow-root (closed).


If the #shadow-root had been open we could have easily interacted with the desired element. You can find a couple of relevant discussions in:

  • How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector
  • How to automate shadow DOM elements using selenium?


Shadow root DOM automation using selenium


A couple of pull requests:

  • support shadow DOM when reading element text
  • add shadow dom v1 support to DOM atoms
Saturday, August 28, 2021
Alexander Trauzzi
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :