PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 15, 2022

[FIXED] How to extract the src attribute of an iframe using Selenium and C#

 April 15, 2022     c#, css-selectors, iframe, selenium, xpath     No comments   

Issue

I use selenium webdriver with C#,

<iframe class="x-component x-fit-item x-component-default" id="component-1105" name="ets_grd_02_IFrame" src="frm_01_master_training_plan.aspx?RID=196&amp;RIU=U&amp;_dc=1641984641351" frameborder="0" style="margin: 0px; width: 718px; height: 535px;"></iframe>

I need to get src attribute of this element. I can locate inside of this iframe and make operations on the form which is located inside that iframe but I can't get src attribute.


Solution

To get the iframe src attribute:

driver.SwitchTo().DefaultContent(); // call this or make sure you are not already switched to this iframe
string iframeSrc = driver.FindElement(By.Xpath("//iframe[contains(@id, 'component-')]")).getAttribute("src")

To interact with the iframe elements:

IWebElement frame = driver.FindElement(By.Xpath("//iframe[contains(@id, 'component-')]"))
driver.SwitchTo().Frame(frame);
// do somethind you like within iframe
driver.SwitchTo().DefaultContent();

Make sure that your target iframe is not placed within some parent iframe. Otherwise, you'll have to first switch to the parent iframe to access the current one.



Answered By - Max Daroshchanka
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing