How to access a element within a emebedded HTML

Get the most out of asking a question.

  • Is the topic title a question? No
  • Does the topic title include a “question mark”? No
  • What are you asking of the community? I dont know whether this is the right way to do this, how to access a element inside an embedded HTML document which was loaded into the page via the element. in robot framework using selenium
  • How can they help you? However I try, I cant seem to access the elements within the emebedded HTML document
  • What context can you share to increase the likelihood of someone replying to your question? I can share the HTML code
4 Likes

Hello @nithindante

To access an element within an embedded HTML document, you need to first locate the iframe or object tag that embeds the HTML document into the parent document. Once you’ve identified the iframe or object element, you can switch the Selenium WebDriver’s focus to that frame or object and then access the elements within it as you would in the main document.

Here’s a general approach using Python with Selenium WebDriver:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# Initialize the WebDriver
driver = webdriver.Chrome()

# Navigate to the page with the embedded HTML document
driver.get("URL_of_the_page_with_embedded_HTML")

# Switch to the iframe if embedded using iframe
iframe = driver.find_element(By.XPATH, "//iframe[@id='iframe_id']")
driver.switch_to.frame(iframe)

# Or, switch to the object if embedded using object
# object = driver.find_element(By.XPATH, "//object[@id='object_id']")
# driver.switch_to.frame(object)

# Now you can access elements within the embedded HTML document
embedded_element = driver.find_element(By.XPATH, "//Your_XPath_to_the_desired_element_within_the_embedded_HTML")

# Perform actions on the embedded element if needed
# For example, you can extract text or interact with it
element_text = embedded_element.text

# Once done, you may want to switch back to the default content
driver.switch_to.default_content()

# Close the browser window
driver.quit()

Replace "URL_of_the_page_with_embedded_HTML", "//iframe[@id='iframe_id']", and "//Your_XPath_to_the_desired_element_within_the_embedded_HTML" with the actual URL of the page containing the embedded HTML, the XPath to the iframe or object element, and the XPath to the desired element within the embedded HTML, respectively.

3 Likes

Thanks for getting back to me, the issue with the website that I test is, there is no iframe element, there is a object element though,

but when I try to select any element within the object, its showing as stale error,

3 Likes

Hey Nithin, perhaps share the html or a link to the page and element you are trying to test :slight_smile:

Do you have a language preference so we can try to help you?

This could also be an opportunity to ask chat GPT for example for help :wink:.

Viv

Hey , Sure, I can share the link to the page, but am not quite sure of that,as its our UAT environment in which we test in our office, let me check with my lead and get back to that,

My preferred language would be english, I tried chat GPT for around more than 24 hours, but it doesnt seem to work at all

staleelementexceptions are raised when you use an item or element that has animated or changed since the last api call to the driver. The one trick for this is to start a timer, for example 30 seconds, and keep trying the chain of calls until the outer element stops “animating” or updating/loading. If your developers have java scripts running that modify the dom continously, then I’m not sure what you can do though.

Great example there @ansha_batra :+1:

1 Like

ooh thanks for that, surely will look into it