1

the html I'm digging (there is no iframe on it) is:

<div id="app" class="container-fluid">

I try to get the block of the DOM tree corresponding to the above div

this is my code:

from selenium.common.exceptions import NoSuchElementException
try:
    app = driver.find_element_by_xpath('//div[@id="app"]')
    print(4444444444444444, app)
    is_vue_app_loaded = len(app.text) != 0
except NoSuchElementException as e:
    print(f'e: {e}')

I get this error:

 4444444444444444 <selenium.webdriver.firefox.webelement.FirefoxWebElement
 (session="c8da2a89-63fd-4dce-bdb5-28fdf0e67b01",
  element="b9be2828-115e-4965-8ebc-b75c09236193")>

 e: Message: Unable to locate element: //div[@id='app']

Is there a way to ask the driver to return me the raw html so I can check that the DOM try I think I have is the one I have actualy ?

user3313834
  • 7,327
  • 12
  • 56
  • 99

2 Answers2

1

Check if the element is present inside any iframe.If there you need to switch it first.

If Not then try.

Induce WebDriverWait() and visibility_of_element_located()

WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,"//div[@id='app' and @class='container-fluid']")))

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
KunduK
  • 32,888
  • 5
  • 17
  • 41
1

If your usecase is to return me the raw html of the element you have to induce WebDriverWait for the visibility_of_element_located() inconjunction with get_attribute("outerHTML") and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.container-fluid#app"))).get_attribute("outerHTML"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='container-fluid' and @id='app']"))).get_attribute("outerHTML"))
    
  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352