One Video is there below that one button is there i need to automate that button wheather that button is present below that video or not any one please help me to resolve this
If the button is a part of the page DOM and it can be searched by Selenium you should be able to access it's Location property like:
System.Drawing.Point location = driver.FindElement(By.XPath("//your_element_locator")).Location;
Console.WriteLine("My element location: " + location);
If you want to conditionally execute this or that code block depending on presence/absence of the element you can go for FindElements() function like
if (driver.FindElements(By.XPath("//your_element_locator")).Count > 0)
{
// element is present
}
else
{
//element is absent
}
If the button is not present in the DOM and it's a part of video - you will have to go for image recognition frameworks like AForge.NET, Emgu CV or SeeTest
Related
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 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 am not able to get the Xpath for an input file with Selenium.
Here is a screenshot (of the mark-up).
Since the element you want is inside an iframe, you will have to switch selenium's focus to the iframe and then use the xpath to fetch the element.
JAVA:
To switch to the iframe, use
driver.switchTo().frame("uploadFile"); // used iframe's id as locator inside frame()
then, to fetch the input element, use the xpath //input[#name='file']
Do not forget to navigate away from the iframe after you are done interacting with this iframe. To navigate to default frame, use driver.switchTo().defaultContent();
There is a frame in your html code, first you have to switch to that frame.
WebDriver's driver.switchTo().frame() method takes one of the three possible arguments:
A number.
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.
A name or ID.
Select a frame by its name or ID. Frames located by matching name
attributes are always given precedence over those matched by ID.
A previously found WebElement.
Select a frame using its previously located WebElement.
Get the frame by it's id/name or locate it by driver.findElement() and you'll be good.
Then you can perform any action on input tag using below cssSelector :
driver.findElement(By.cssSelector("input[name=file]"));
And after performing your action don't forget to swith it to default frame :
driver.switchTo().defaultContent();
Exception Handling
You can wait for the alert to come for 30 seconds using below code , once the alert is present you can easily accept it.
try {
WebDriverWait waitAlert = new WebDriverWait(wb, 30);
waitAlert.until(ExpectedConditions.alertIsPresent());
wb.switchTo().alert().accept();
} catch (Exception e) {
logger.info("Alert Didn't Come");
}
Let me know if it helps.
Try below code.
//Going inside of the iframe
driver.switchTo().frame("uploadFile");
//Getting file input webelement
WebElement input=driver.findElement(By.xpath("//form[#name='fileUploadFrm']//input[#name='file']"));
//Sending file path using sendkeys method
input.sendKeys("filepath");
//Switch back to default
driver.switchTo().defaultContent();
Exception handling: Modal dialog present exception will be thrown when an alert appeared and we are not handling it. If the alert doesn't appear every time place below after your operations.
try {
Alert alert = driver.switchTo().alert();
alert.accept();
//if alert present, accept and move on.
}
catch (NoAlertPresentException e) {
//do what you normally would if you didn't have the alert.
}
Let me know if it works for you
Suppose that you have a web page with blinking elements (for instance the background of a cell blinks red when a condition happen on the database, the page queries the database with a javascript). How would you verify that the element is blinking with Selenium web driver?
You can check if element is visible or not.
IWebElement myBlink = chrome.FindElement(By.Id("blink"));
if (myBlink.Displayed)
{
//it's displayed
}
else
{
//it's not displayed
}
All you have to do next is create a timer to set up time when to check it again.
how do I find out if my custom widget has focus in Dojo?
i have dojo editor i wnat to know if the editor has already focus or not?
you can use the module dijit/focus to find out the focus
FROM DOJO DOCS
Tracking active widgets
At any point in time there is a set of (for lack of a better word)
“active” or “focused” widgets, meaning the currently focused widget
and that widget’s ancestors. “Ancestor” can mean either DOM ancestor
(ex: TextBox –> Form), or a logical parent-child relationship (ex:
TooltipDialog –> DropDownButton).
For example, if focus is on a TextBox inside a TabContainer inside a
TooltipDialog triggered by a DropDownButton, the stack would be
TextBox –> ContentPane –> TabContainer –> TooltipDialog –>
DropDownButton.
The activeStack[] parameter indicates this set of widgets, and an app
can monitor changes to activeStack[] by:
require([ "dijit/focus" ], function(focusUtil){
focusUtil.watch("activeStack", function(name, oldValue, newValue){
console.log("Focused widget + ancestors: ", newValue.join(", "));
});
});
the question in title has a different answer than the one in the descriptions.
there are two ways achieving the question in the title, by using dojo's focusUtil ("dijit/focus"). both ways give you something that you could find the widget using it and the dijit's registry ("dijit/registry").
focusUtil.curNode: gives you the DOM Node that currently has the focus. the function below, you could get the widget reference.
function getWidgetByNode(node){
var result;
while (!result && node){
result = registry.byNode(node);
if (node.parentElement)
node = node.parentElement;
else
node = null;
}
return result;
}
var focusedWidget = getWidgetByNode(focusUtil.curNode)
focusUtil.activeStack: gives you an array of the widgets (parent to child) that has the focus. so the last item in the array is the direct widget which has the focus. index values are widget ids, so you should get the widget by the following code
var focusedWidgetId = focusUtil.activeStack[focusUtil.activeStack.length-1];
var focusedWidget = registry.byId(focusedWidgetId);
now if you want to know if the currently focused widget is some specific one, it depends on what you have in hands from that specific widget:
widget itself: like the return values of above samples. now you have to compare if these are the same thing. you can not compare two widget objects using the == operator. you could compare their ids like this:
myWidget.id == focusedWidget.id
widget's id: this way you just easily get the id of the current node from focusUtil and compare it with the id you have liek this:
myWidgetId == focusedWidgetId
references:
http://dojotoolkit.org/reference-guide/1.9/dijit/focus.html
http://dojotoolkit.org/reference-guide/1.9/dijit/registry.html
require([ "dijit/focus" ], function(focusUtil){
var activeElement = focusUtil.curNode; // returns null if there is no focused element
});
check blow url here you can see some examples
http://dojotoolkit.org/reference-guide/1.8/dijit/focus.html#dijit-focus
a) For dojo 1.6: call dijit.getFocus(). This will return an object containing the currently focused dom node, among other things (selected text, etc.). To get the corresponding widget, simply do:
var activeElement = dijit.getEnclosingWidget(dijit.getFocus().node);
This is the full reference for dijit.getFocus(), from the source code:
// summary:
// Called as getFocus(), this returns an Object showing the current focus
// and selected text.
//
// Called as getFocus(widget), where widget is a (widget representing) a button
// that was just pressed, it returns where focus was before that button
// was pressed. (Pressing the button may have either shifted focus to the button,
// or removed focus altogether.) In this case the selected text is not returned,
// since it can't be accurately determined.
//
// menu: dijit._Widget or {domNode: DomNode} structure
// The button that was just pressed. If focus has disappeared or moved
// to this button, returns the previous focus. In this case the bookmark
// information is already lost, and null is returned.
//
// openedForWindow:
// iframe in which menu was opened
//
// returns:
// A handle to restore focus/selection, to be passed to `dijit.focus`.
b) For dojo 1.7 and up, use dijit/focus:
require([ "dijit/focus" ], function(focusUtil) {
var activeElement = focusUtil.curNode; // returns null if there is no focused element
});