2
votes

If I wait for the parent tag to load when using the WebDriverWait function on Selenium, can I be assured that the child tag is loaded?

Code example:

WebDriverWait(self.web_driver, WEB_DRIVER_WAIT_TIME).until(
            EC.presence_of_element_located(By.ID, "test")
)

HTML:

<div id="test">
   <h1>Is this tag also loaded?</h1>
<div>

In this situation, when the test tag is finished loading, I wonder if the tag for the h1 tag is guaranteed to be loaded as well.

If no child tag (h1 tag in the example code) In other words, some pages have child tags called h1 and some pages have the same structure, but no child tags called h1.

I didn't like the delay, even though I knew there would be no tags if I used WebDriverWait for my child tags. So, like the question above, I was going to wait for the load of the parent tag and then take care of it.

As I thought, if applying WebDriverWait to parent tags is not guaranteed to load child tags, how can I achieve the goals I want?

Thanks for your help.

1

1 Answers

2
votes

A bit of more information would have helped us to construct a more canonical answer. However, just like each test is based on a unique test aim, the target element must also be unique for each test / test aim.

So, if your target element is the <div> your test should be based on the <div> as follows:

WebDriverWait(self.web_driver, WEB_DRIVER_WAIT_TIME).until(
        EC.presence_of_element_located((By.ID, "test"))
)

Incase, your target element is the <h1> your test should be based on the <h1> as follows:

WebDriverWait(self.web_driver, WEB_DRIVER_WAIT_TIME).until(
        EC.presence_of_element_located((By.XPATH, "//div[@id='test']/h1[text()='Is this tag also loaded?']"))
)