'Selenium handling html elements inside object tag

I'm struggling with validating html elements inside html object tag.

Following is my test html (test1.html).

<html>
<body>
    <h1>this is test 1</h1>
    <object id='obj1' width="100%" height="200px" data="http://www.w3schools.com/"></object>
    <br>    <br>    <br>    <br>
    <iframe id='ifr1' width="100%" height="200px" src="http://www.w3schools.com/"></iframe>
</body>
</html>

In case of iframe, it is easy to handle by using webdriver switchto frame method like following example.

@Test
public void testIframe() {
    try {
        FirefoxDriver driver = new FirefoxDriver();
        driver.get("http://localhost/test1.html");

        driver.switchTo().frame(0);
        Assert.assertTrue(driver.findElement(By.xpath("/html/body/div[1]/div")).getText().equals("THE WORLD'S LARGEST WEB DEVELOPER SITE"));

        driver.quit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But in case of object tag, switchTo method can't be applied with same way. I'm curious that Selenium provides this kind of handling.

Does anybody have an idea to solve this problem, or have any suggestions ?



Solution 1:[1]

You can use the SwitchTo api to access object element as well by this way:

driver.SwitchTo().Frame(driver.FindElement(By.TagName("object")));

Solution 2:[2]

Actually in WebDriver for frame we use

driver.switchTo().frame();

for objects we can directly handle or perform operations directly

for example from your code, if i want to take the value of data attribute

I used

driver.findElement(By.xpath("//object[@id='obj1']")).getAttribute("data")

which will return the value http://www.w3schools.com/

we can handle it like normal web element , we no need to use switchTo()

Solution 3:[3]

I also used something similar to @Deepak_Mahalingam. In case anyone finds his syntax not working. I use chrome and inspect the code to get the xpath.

link = driver.find_element_by_xpath('//*[@id="gl-consoleTabs-slides"]/div/div/object').get_attribute('data')

Solution 4:[4]

I was having the same problem and the below solution worked for my project:

1.I wrote a java method to get the URL from the data attribute in the <Object> tag from source code.
2.Opened the extracted URL in a new tab and performed the testing needed.
3.Switched back to the main tab.

Here the method I used, you can modify this as per your requirement:

public void getObjectTagURL(){
   try {
      String pageSource= driver.getPageSource(); 
      int startIndex =str.indexOf("http://www.myURL");         
      int endIndex= str.indexOf("</object>");
      String url= str.substring(startIndex, endIndex);  
   } catch (Exception e) {
        System.out.println(e);   
     }

Hope it helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pedro Herrarte
Solution 2 Deepak_Mahalingam
Solution 3 NatalieL
Solution 4 Nitesh