0

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
  • 5,701
  • 4
  • 40
  • 76
  • Check the element is present inside any iframe?? – KunduK Jan 02 '20 at 11:23
  • Looking at the output the element was found `4444444444444444 – Guy Jan 02 '20 at 11:30
  • @user3313834 This sounds like an [X-Y problem](http://xyproblem.info/). Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? – DebanjanB Jan 02 '20 at 11:36
  • @DebanjanB I've edited the question to precise what I'm trying to do – user3313834 Jan 02 '20 at 12:57

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
  • 26,790
  • 2
  • 10
  • 32
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
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217