I go to the frame on the site using the code
driver.SwithToFrame(0)
How can I find out now in focus the main site or frame?
To validate if the focus is on the Top Browsing Context or within the iframe, you can switch to the active_element and then print the outerHTML and you can use the following solution:
driver.switch_to.frame(0)
element = driver.switch_to.active_element
print(element.get_attribute("outerHTML"))
Related
I am learning selenium for web scraping.I want to click on a link using selenium But could not find a way to do that.The problem is the link opens in a new frame inside a webpage and i can't locate any element on that frame.
I have attached a picture of that pop up frame and source code of that frame.I hope there is a way to solve this issue.[![enter image description here][1]][1]
the code i have tried :
try:
browser.get('link')
browser.switch_to.frame(0)
myElem_1 = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.LINK_TEXT, 'Manually enter account & routing info')))
myElem_1.click()
except:
pass
I have translated C# to your langage, not sure about By.Id but you will fix that.
just fyi its not alink but a button, you cant find it by linktext.
browser.maximize_window()
browser.get('https://link.goes/here')
frame = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'wepay-ach')))
browser.switch_to.frame(frame);
but = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'microdeposit-button')))
but.Click();
Generally speaking, you must:
first switch to the iframe … driver.switch_to.frame(frame_reference)
do your testing in the iframe
switch back in the main window … driver.switch_to.default_content()
I have been using a code since long time , but recently there is a new banner up which is hiding the element that I am trying to click.
Attaching the snapshot of error. The only help I need is I need to click the hidden element( if the browser window is maximized the element is visible).
.
Please help me.
If what you have said is true then you can use the following to maximize the window before clicking:
driver.Window.Maximize
Other options include:
1) Removing the banner
2) Scrolling the element into view
Can't write anything decent for those last two as your code is an image and I don't have a full URL to test with. You also haven't included the relevant HTML.
The "div.container-fluid" element is blocking the button you are trying to click.
You could try some of the following (as being shown here Element MyElement is not clickable at point (x, y)... Other element would receive the click):
prolong the wait before the click
use javascript executor
Hellow guys, please guide me to solve issue.
I am able to access all fields inside the iframe which is in div, I want to close iframe but I am unable to access (X) button.
The close button is outside iframe and inside div.
Here is my code:
To switch into iframe from main window:
BaseClassOne.driver.switchTo().frame(BaseClassOne.driver.findElement(By.xpath("//*[#id='Dealership quote Internal']/iframe")));
To access iframe element:
BaseClassOne.driver.findElement(By.xpath("//*#id='txtDealershipRef']")).sendKeys("XYZ090123");
I tried below mention code to close modal popup window:
BaseClassOne.driver.findElement(By.tagName("a")).click();// throwing no such element exception
BaseClassOne.driver.close();// this is closing browser instance
BaseClassOne.driver.switchTo().defaultContent();
// modal pop-up is not closing hence not able to access main window element
Please guide me.
Thanks in advance.
According to the above comments and discussions, I feel that you have to switch back to the default frame and then try to click on the close button.
driver.switchTo().defaultContent();
There seems to be an issue with the code sequence. Considering the close link is not contained in the iframe you have to switch to default content first and then click on close before proceeding. Try the following code:
//Switch to frame and perform actions
BaseClassOne.driver.switchTo().frame(BaseClassOne.driver.findElement(By.xpath("//*[#id='Dealership quote Internal']/iframe")));
BaseClassOne.driver.findElement(By.xpath("//*#id='txtDealershipRef']")).sendKeys("XYZ090123");
//switch to default content (to access elements outside the frame)
BaseClassOne.driver.switchTo().defaultContent();
//click on close
BaseClassOne.driver.findElement(By.xpath("//a[#class='close-window']")).click();
Note: the identifier for the close button has been changed in the code here to use the properties from the HTML sinceBy.tagName("a") which is a part of the code in the question could possible have many matching nodes which are higher up in the HTML hierarchy.
The Selenium webdriver locator always puts elements on top of the page if scrolling is needed, but it wont take into account a floating header. At the moment I created a workaround with
Actions actions = new Actions(this.webdriver);
actions.sendKeys(Keys.ARROW_UP).perform();
Isnt there a nicer solution to tell the webdriver to center an element in the middle of the screen instead or with a fixed distance to the top?
The orange part is from the button the blue part is the header:
hidden button in orange
In my case, I choose to hide that floating element.
top = dr.find_element_by_xpath('//div[#node-type="top_all"]')
dr.execute_script("arguments[0].setAttribute('style','display:none')", top)
After that, move to the element I want, I can even add an offset.
Bring the element into view using javascript. Here is a possible solution:
Bring the element into the view port using selenium api.
Use javascript to figure out if the element is obstructed by another element, (floating header / footer etc...).
Use javascript to scroll to the middle of the view port.
See the javascript utility class I created here: https://github.com/gterre/stuff
usage...JavascriptUtils.bringIntoView(WebElement element);
I'm sure you can modify the script to your needs.
Selenium also has the ability to not scroll items to the top of the window before interacting with them (and therefore hiding them behind a floating header) but instead to the bottom.
This is achieved via the 'elementScrollBehavior' capability which can be set to 1 to scroll stuff to the bottom.
See this stack overflow for how it's done in capybara.
I solved the problem like this, so you just need to define in which cases your element is not in scope and add this method, it will automatically scroll it into view:
public void scroll_element_into_view(WebElementFacade element) {
int Y = (element.getLocation().getY() - 200);
JavascriptExecutor js = (JavascriptExecutor) getDriver();
js.executeScript("javascript:window.scrollTo(0," + Y + ");");}
Works for every element you can find by id, css, xpath or whatsoever.
I am writing cucumber feature and I need to fill value in text field within iframe. I have tried with
find("#user_email").set "malicious_value"
but couldn't get success. I have selenium webdriver.
This is ruby code with selenium to switch in iframe. you can do it by:
#Move into iframe
page.driver.browser.switch_to.frame "name or id of frame"
#Move to default content or outsite frame
page.driver.browser.switch_to.default_content
If you want to do any thing inside the frame. First you have to go inside the frame.
Code to enter the frame:
//Assume driver is initiated properly some where.
driver.switchTo.frame(FrameName);
(Or)
driver.switchTo.frame(FrameIndexValue);
(Or)
WebElement element = driver.findElement(By.id(LocatorValue));
driver.switchTo.frame(element);
After finishing your action inside the frame. You have to come out to frame by using
code to leave the frame:
driver.switchTo.defaultContent();
If you are dealing with the iframe then the defaultContent() will take you to the main page above all the iframes, but if you deal with the frame this method will take you to the first frame of the page.
For more information on frame handling.