My original question was confusing, I rewrote it. Now it is much clearer.
Hello I am trying to navigate web page using HtmlUnit. I am trying to
go to url -> enter text in text box (in frame 1) -> Press Search Button (in frame 1) -> go to new frame (frame 2) that appeared as result of search.
When I write this code:
HtmlPage page = webClient.getPage("http://someurl.com");
HtmlPage toolbar = (HtmlPage) page.getFrameByName("toolbar").getEnclosedPage(); //GET TOOLBAR FRAME
HtmlTextInput searchBox = toolbar.getElementByName("query"); //GET TEXT BOX ELEMENT
searchBox.setValueAttribute("SOME STRING"); //FILL TEXT BOX ELEMENT WITH TEXT
List<HtmlElement> elements1 = (List<HtmlElement>) toolbar.getByXPath(<SOME XPATH>); //LIST OF A SINGLE ELEMENT WHICH IS THE SEARCH BUTTON
HtmlElement element2 = elements1.get(0); // GET SEARCH BUTTON ELEMENT FROM LIST
page = element2.click(); // CLICK SEARCH BUTTON AND LOAD NEW PAGE TO "page"
I am searching for "SOME STRING". When I do this, another page comes up (though does not refresh the page, the URL never refreshes) with an ADDITIONAL frame. So now, I click the search button but thew new page is just a webpage of the toolbar frame, not of the whole webpage.
I want to now access the additional frame. So how do I "back out" of the current frame and select the new frame?
I found out the answer my trial and error.
page = (HtmlPage) searchButton.click().getEnclosingWindow().getTopWindow().getEnclosedPage();
page = (HtmlPage) page.getFrameByName("Frame 1").getEnclosedPage();
page = (HtmlPage) page.getFrameByName("Frame 2").getEnclosedPage();
It works great, hope I can help some of you out.
Related
On this webpage: https://www.selenium.dev/downloads/
I am trying to capture the screenshot of an element that is an img tag.
You can inspect : img[alt='Java']
This is the img element I want to capture:
Element to capture
So to capture the above element I wrote this code:
WebElement imgJava = driver.findElement(By.cssSelector("img[alt='Java']"));
Screenshot screenshotJava = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver, imgJava);
ImageIO.write(screenshotJava.getImage(), "png",new File(System.getProperty("user.dir") + "\\javaImg.png"));
But when I opened the image, it is just all white:
Image after caputring
And if I remove the shooting shotingStrategy and just use new AShot().takeScreenshot(driver,imgJava);
It will take screenshot of whole page even though I passed a particular element:
Whole page
After selecting a product and opening it in another page Im not able to click on add to cart button in amazon using selenium.
If you are not switching to other page, Add to card option will not be located.
Switch to the opened page and then try to click on the button.
handles = driver.window_handles
driver.switch_to.window(handles[1])
driver.find_element_by_id("add-to-cart-button").click()
If you have done this, share the code you have tried and error you get.
Without HTML page you are trying to perform, it is difficult to pinpoint exactly. However, I would like to take a stab at it considering these:
Click on product in home page
Product opens in a new tab window
Click on add to cart
P.S: I will write it using Java since you haven't mentioned on the language being used. Will be similar in others as well
Click on product in home page
List <Webelement> productCheck = driver.findElements(By.xpath("<xpath of the product>"))
if (productCheck.size() == 0){
// do something
else {
driver.findElement(By.xpath("<xpath of the product>")).click();
}
Product opens in a new tab window and Click on add to cart
String parent = driver.getWindowHandle();
List<String> windows = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(windows.get(1));
// Inside the tab window
List <WebElement> addtoCartButton = driver.findElements(By.xpath("<xpath of Add to cart button>"));
if (addtoCartButton.size() == 0 ) {
// do something
else {
driver.findElement(By.xpath("<xpath of Add to cart button>")).click();
}
// do whatever you want in the new tab and if you want to switch back then:
driver.switchTo().window(parent);
I am working on a test, where I am trying to utilize drag and drop to drag the 'Text' from grapejs editor into the Frame, where we enter in our content.
I first tried to move the element from one element to the other, but I noticed that the second element was within an iframe, so I tried to use the x, y coordinates. But of course, the XY coordinates are (0,0) within the frame. So the element was out of range. I also tried the 0,0, but also said it was out of bounds.
var target = Driver.Instance.FindElement(By.XPath("//*[#id='gjs']/div[1]/div[2]/div[5]/div[2]/div/div[1]/div[1]/div[2]/div[5]"));
var builder = new Actions(Driver.Instance);
var action = builder.ClickAndHold(target);
builder.Build();
action.Perform();
var iframe = Driver.Instance.FindElement(By.TagName("iframe"));
Driver.Instance.SwitchTo().Frame(iframe);
builder = new Actions(Driver.Instance);
var destination = Driver.Instance.FindElement(By.XPath("/html/body"));
action = builder.MoveToElement(destination);
builder.Release(destination);
builder.Build();
action.Perform();
Has anyone successfully used Selenium to Drag and Drop across to an iframe lately? All the examples are old...maybe there is a new way to do it. All the examples say use the offset, but putting what I think is the offset for that iframe in the page is not working, nor is switching to the iframe, and setting the co-ordinates within the frame.
I came across one of the scenario where I need to validate the user is scroll to top of the page when clicked on the "back to top" button on the bottom of the screen.
I tried with the following way but that didn't work.
I tried to validate the element present on the top of the page using
isDisplayed method
I have attached the image for clear description.
Solved it using the javascript concept used pageYOffset method.
Complete code
JavascriptExecutor executor = (JavascriptExecutor) driver;
Long value = (Long) executor.executeScript("return window.pageYOffset;");
pageYOffset method will return the vertical pixels, so as soon I logged in got the vertical pixels and then scrolled to the back to top button and after performing the action on back to top button, again got the vertical pixels and validated it.
isDisplayed() checks if the element is actually present in the viewport so it should work. May be put some wait in between clicking and checking isDisplayed for debugging puropose .
if (element.isDisplayed()) {
doSomething();
}
else {
doSomethingElse();
}
I need to show a virtual keyboard when the user selects an input text field in a webpage. This code runs on an set top box.
I am extending the QWebView, which is creating a new keyboard widget as a child.
WebView::WebView(QWidget* parent = 0): QWebView(parent)
{
WebPage *page = new WebPage(this);
this->m_keyboard = new widgetKeyBoard(this, Qt::Window|Qt::CustomizeWindowHint);
this->m_keyboard->createKeyboard();
this->m_keyboard->hide();
connect(this, SIGNAL(launchVirtualKB(WebView *) ), SLOT(launchKeyboard(WebView *)));
}
The widgetKeyBoard is made up of QGridLayout with many children QKeyPushButton.
When I do a show of keyboard, I see the whole keyboard drawn briefly and then overwritten 30% of the keyboard from the below web page (overwritten near the input field). I tried to have my own repaint for webview and mainwindow, but still see that someone else is overwriting.
What could be causing such an issue. I am using QT4.8.