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));
wait.Until(By.Id("login"));

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

 Answers

67

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
 
devo
answered 7 Months ago
16

From the Selenium Documentation PDF :

import contextlib
import selenium.webdriver as webdriver
import selenium.webdriver.support.ui as ui

with contextlib.closing(webdriver.Firefox()) as driver:
    driver.get('http://www.google.com')
    wait = ui.WebDriverWait(driver,10)
    # Do not call `implicitly_wait` if using `WebDriverWait`.
    #     It magnifies the timeout.
    # driver.implicitly_wait(10)  
    inputElement=driver.find_element_by_name('q')
    inputElement.send_keys('Cheese!')
    inputElement.submit()
    print(driver.title)

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

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

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(webdriver.By.name("username"));
}, timeout);
Thursday, June 10, 2021
 
Novalirium
answered 7 Months ago
10
 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
 
nhunston
answered 4 Months ago
61

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).


Conclusion

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?

Alternative

Shadow root DOM automation using selenium


References

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 :
 
Share