WebDriver - How to locate iframe without id - selenium

I'm trying to switch to an iframe in order to locate an element, but I'm unable to locate the iframe since it has no id or name
<div id="eyein-modal" style="display: block; position: fixed; width: 100%; height: 100%; top: 0px; left: 0px; bottom: 0px; right: 0px; z-index: 90000000; background-color: rgba(0, 0, 0, 0.6); overflow: auto; opacity: 1;">
<iframe style="display: block; width:90%; height:90%; border: 0px; margin: 2.5% auto; z-index: 90000000; overflow: hidden;" scrolling="no" src="about:blank">
<html>
<head>
<body class="">
<div id="modal">
<div id="modal-header">
<div id="header-logo">
<div id="title-container" class="">
<a id="view-event" class="button" target="_blank" href="http://www.link.com">view event</a>
<div id="close-modal" class="close-dark"></div>
close-modal is the element I need eventually

Aside from providing frame name or id, you can switch to the frame by index (zero-based):
Select a frame by its (zero-based) index. That is, if a page has three
frames, the first frame would be at index "0", the second at index "1"
and the third at index "2". Once the frame has been selected, all
subsequent calls on the WebDriver interface are made to that frame.
driver.switchTo().frame(0); // assuming this is the first frame on the page
Or, you can make a WebElement instance by locating the iframe, for example, by CSS selector:
WebElement frame = driver.findElement(By.cssSelector("div#eyein-modal iframe"));
driver.switchTo().frame(frame);
See also:
How to switch between frames in Selenium WebDriver using Java

If anyone using RSelenium in R happens on this little problem, this is what I used:
webElement <- remDr$findElement(using = "css selector", "#dsq-app1")
remDr$switchToFrame(webElement)
# note: remDr is the remoteDriver instance that needs to be opened at the beginning
or
webElement <- remDr$findElement(using = "id", value = "dsq-app1")
remDr$switchToFrame(webElement)

I had this problem ... using Webdriver with Codeception. My solution was to run a javascript snippet to give the iframe a name, and then I could switch to it based on name ... details here: https://stackoverflow.com/a/48123837/1593026

As per the html of the iframe element:
<iframe style="display: block; width:90%; height:90%; border: 0px; margin: 2.5% auto; z-index: 90000000; overflow: hidden;" scrolling="no" src="about:blank">
It is pretty much evident that the <iframe> doesn't have either of the following attributes:
id
name
class
title
specific value of src attribute
In such cases to switch to the desired iframe you can refer to the preceeding(sibling) WebElement and you can use either of the following locator strategies:
Java based solutions:
Using xpath and following:
WebElement iframe = driver.findElement(By.xpath("//div[#id='eyein-modal']//following::iframe[1]"));
driver.switchTo().frame(iframe);
Using xpath and following-sibling:
WebElement iframe = driver.findElement(By.xpath("//div[#id='eyein-modal']//following-sibling::iframe[1]"));
driver.switchTo().frame(iframe);
Using cssSelector:
WebElement iframe = driver.findElement(By.cssSelector("div#eyein-modal +iframe"));
driver.switchTo().frame(iframe);
Python based solutions:
Using xpath and following:
driver.switch_to.frame(driver.find_element(By.XPATH, "//div[#id='eyein-modal']//following::iframe[1]"))
Using xpath and following-sibling:
driver.switch_to.frame(driver.find_element(By.XPATH, "//div[#id='eyein-modal']//following-sibling::iframe[1]"))
Using cssSelector:
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "div#eyein-modal +iframe"))
Reference
You can find a couple of relevant discussions in:
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
Selenium - How to locate elements on iframe

Related

Is there any function in selenium to input text in field which is not visible using java?

Enter text in ABSTRACT TILE filed using send keysI'm trying to input text in ABSTRACT_TITLE using send-keys in selenium.
I tried switching frames and giving input.
<div id="reTitleCenter" class="reContent" style="height: 2.91667em;" xpath="1">
<label for="reTitleContentHiddenTextarea" style="display:none;">RadEditor hidden textarea</label>
<textarea id="reTitleContentHiddenTextarea" name="ctl00$ctl00$ctl00$phDesktop$cphContent$cphContent$reTitle" rows="4" cols="20" style="display:none;"></textarea>
<iframe frameborder="0" src="javascript:'<html></html>';" id="reTitle_contentIframe" title="Rich text editor with ID reTitle" style="width: 100%; height: 100%; margin: 0px; padding: 0px;">Your browser does not support inline frames or is currently configured not to display inline frames.</iframe>
<textarea class="reTextArea reTextAreaHidden"></textarea>
</div>
the code samples I tried -
driver.switchTo().frame("reTitle");
driver.findElement(By.xpath("//div[#id='reTitleTop']/div[#class='RadEditor_Bootstrap reToolBarWrapper']")).click();
driver.findElement(By.xpath("//div[#id='reTitleTop']/div[#class='RadEditor_Bootstrap reToolBarWrapper']")).sendKeys("This is demo abstract");
OR
driver.switchTo().defaultContent();
WebElement Abstract_title = driver.findElement(By.xpath("//html//body"));
Abstract_title.sendKeys("This is abstract demo selenium");
OR
selenium.SelectFrame("xpath_of_iframe"); (Don't know how to use it)
selenium.Type("//html/body","Thank you");

Not able to click a value in Input drop down

I have a strange problem . I have a non select drop down with Id as below . There is a no select tag I can see in the code for this field just an input tag nor I can see the text contained in the drop down in the DOM after a select a value . StaffSoCategory is the text box , SelectStaffSoCategory is the icon for the drop down down arrow and StaffSoCategoryValue I believe is for the values displayed .
<input type="text" id="StaffSoCategory" style="width: 126px; font-weight: lighter; border: 1px solid gray; height: 25px; float: left; font-size: 12px; color: black; font-family: Arial; background-color: white;" readonly="readonly" class="ui-autocomplete-input" autocomplete="off" disabled="disabled">
<span role="status" aria-live="polite" class="ui-helper-hidden-accessible">4 results are available, use up and down arrow keys to navigate.</span>
<div id="SelectStaffSoCategory" tabindex="0" style="border-style: solid; border-width: 1px 1px 1px 0px; border-color: gray; width: 22px; height: 29px; background-position: center; cursor: pointer; background-image: url('Images/dropdown.png'); background-repeat: no-repeat; float: left; margin-left: 0px; "></div>
***<input id="StaffSoCategoryValue" type="hidden" value="">***
<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" id="ui-id-4" tabindex="0" aria-disabled="false" style="z-index: 1; display: none; top: 584px; left: 829px; width: 148px;"></ul>
Once I select the first drop down value (the visible text in drop down is "Confirmed") then value comes in tag but not the visible text of the value that I see , something like below
<input id="StaffSoCategoryValue" type="hidden" value="BCK-Yes">
Is there a way to click on the first value in the drop down I tried with this value "BCK-Yes" and it said element is not visible. I tried selecting the input by Id and then trying Keys down and keys enter that also didn't work.
The code I am trying is
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement revenuePotential = driver.findElement(By.id("SelectStaffSoCategory")); revenuePotential.click();
WebElement revenuePotentialValue = driver.findElement(By.id("StaffSoCategoryValue"));
js.executeScript("arguments[0].click();", revenuePotentialValue);
Console errors I am getting are
StaffSoJsFun.js?v=2019020100000:1 Uncaught TypeError: $(...).autocomplete is not a function
at HTMLDivElement.<anonymous> (StaffSoJsFun.js?v=2019020100000:1)
at HTMLDivElement.dispatch (jquery-2.0.0.js?v=2019020100000:25)
at HTMLDivElement.y.handle (jquery-2.0.0.js?v=2019020100000:25)
(anonymous) # StaffSoJsFun.js?v=2019020100000:1
dispatch # jquery-2.0.0.js?v=2019020100000:25
y.handle # jquery-2.0.0.js?v=2019020100000:25
Access to XMLHttpRequest at '' from origin 'xx.com' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Well, the input is hidden, so I'm not surprised selenium tells you the element is not visible. Methods that emulate user actions like click() and sendKeys will not be able to affect hidden elements. However, I think the following might work:
First store that element in an object, let's say element and then try the following code to click on that hidden element:
WebDriver driver= new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get(desiredURL);
//do all the stuff you want to do before `executeScript()`
js.executeScript("arguments[0].click();", element);
Give that a shot?

how to handle span class drowpdown in selenium webdriver

I am not able to click and select the value from dropdown by using XPath
driver.findElement(By.xpath(".//*[#id='ProcessTypeIGCombo']/span")).click();
MY HTML code is as below:
<input class="ui-igcombo-field ui-corner-all" readonly="" style="float: left; display: block; position: absolute; height: 20px; width: 99.7921%;">
for more details please use the screenshots

Not able to click on the fields inside the left panel of the page

I have tried below code to click on the fields but getting errors as
"org.openqa.selenium.WeddriverException:unknown error: Element is not clickable at point(138,353). Other element would receive the the click… display: block;>…."
Code:
WebElement el1 = driver.findElement(By.xapth(".//*#id='BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click()", el1);
UI:
Refer Attached Screenshot
HTML code:
<div id="datatab" class="dijitContentPane panel-content dijitStackContainer-child dijitStackContainer-dijitContentPane" style="padding: 0px; overflow: hidden; left: 0px; top: 0px; height: 154px; width: 258px;" selected="true" data-dojo-type="dijit.layout.ContentPane" widgetid="datatab" title="">
<div id="datatabpanel" class="tabpanel" style="height: 100%; overflow:hidden; padding: 0px">
<div class="section-header-outer">
<div id="fieldlistContainer" style="height: 0px;">
<div id="fieldlist" class="dojoDndSource dojoDndContainer" data-dojo-attach-point="containerNode" widgetid="fieldlist">
<div id="category11545">
<div id="field-BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID" class="category11545 field treenode-leaf-label pentaho-listitem dojoDndItem" fieldid="BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID" title="BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID">Patient ID</div>
You are not allowed to click on the div element, rather you can use a workaround to trigger click event by performing "dragAndDrop operation on the same element".
Try below code:
new WebDriverWait(driver, 10).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//div[#style='position: absolute; left: 0px; top: 88px; width: 1366px; height: 677px; display: block;']")));
WebElement el1 = driver.findElement(By.xpath(".//*[#id='field-BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click()", el1);
The html you have attached is do not have id attribute with value = BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID. can you just change your xpath from -
WebElement el1 = driver.findElement(By.xapth(".//*#id='BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID']"));
To
WebElement el1 = driver.findElement(By.xapth(".//*[#fieldid='BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID']"));
//or
WebElement el1 = driver.findElement(By.xapth(".//*[#id='field-BC_PATIENT_COMMUNITY_ENROLLMENT_PATIENT_ID']"));

Selenium WebDriver: How to wait for iframes to load completely? Can click on iframe element text box id = “div_4_1_2_1_1-lnk”

Question : I'm testing a page with an iframe whose contents are generated dynamically by JavaScript. I've to wait for the iframe to be loaded completely to make sure that all elements I need are present.
<iframe frameborder="0" style="border: 0px none; width: 100%; height: 290px; min-width: 0px; min-height: 0px; overflow: auto;" dojoattachpoint="frame" title="Fill Quote" src=" =1&zTaskId=9309&zResetContext=true&coachDebugTrace=none>
------------------------------------------------
<input id="div_4_1_1_1_1_1_2-in" class="p-field span12" type="text">
I have tried below code ,
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 4000);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("div_4_1_2_1_1-lnk"))));
But it's not working. And this iframe has dynamically generated only his title
Output :
Nov 22, 2013 2:52:03 PM org.openqa.selenium.support.ui.ExpectedConditions findElement
WARNING: WebDriverException thrown by findElement(By.id: div_4_1_2_1_1-lnk)
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
Selenium has one expected condition to solve your problem :
ExpectedConditions.frameToBeAvailableAndSwitchToIt(locator)
I recommend you read this link and this one