2

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.

Jay-flow
  • 167
  • 1
  • 14

1 Answers1

2

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?']"))
)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for your reply. Based on your answers, I revised the questions in more detail. According to what you said, it sounds like you need to point to a child tag, exactly h1, to get a guarantee for the load, is that right? If that's right, what's the best way to achieve my desired goal? If that's right, what's the best way to achieve my desired goal, as in the question? – Jay-flow Aug 12 '20 at 08:18
  • 1
    The consideration of `parent` tag and `child` tag arises when there is a dependency. To keep the locators optimized the xpath of a ` – undetected Selenium Aug 12 '20 at 08:49
  • I'm sorry to bother you. I understand that what you said should refer to the tag that is waiting for the load. If the entire code has both the same structure and parent tag but has different child tags, that is, if you use WebDriverWait if one page has a button and one page does not have a button, there will be a delay if the button is not present. However, I would like to receive a guarantee that the load has been completed when the button is in place. What should I do with this? – Jay-flow Aug 12 '20 at 09:09
  • 1
    @Jay-flow _if one page has a button and one page does not have a button_ in such cases use`try-catch{}` so on error your program doesn't crashes/closes and the remaining tests are executed. – undetected Selenium Aug 12 '20 at 09:12
  • Yes, I am using that method to process TimeOutException. However, this still results in a delay of the time specified by WebDriverWait. I want to eliminate the delay itself, is it impossible to achieve it? – Jay-flow Aug 12 '20 at 09:16
  • 1
    @Jay-flow The delay you are concerned about is the delay caused by your own code for `WebDriverWait`. That implies you have to change your **test strategy** / **test architecture** and possibly you can't derive a common code for all the pages as _one page has a button and one page does not have a button_. – undetected Selenium Aug 12 '20 at 09:22