I tried a lot to locate elements on this page with this link
ALL I want to do is to select "1 queen bed" or "1 double large bed" and then select amount from the drop down list then press, I'll reserve button.
But I totally failed trying all of these:-
using Action --> moveToElement --> perform()
using JS Execution --> scrollToView, scrollBy(0,100) for example to make the page scroll down
using all types of locators (ID, xPaths, cssSelectors, names) but always an error message that Expected condition failed: waiting for visibility of element located by By.xpath- By.id .. etc.
tried to search for an iFrame but I didn't find but I think there are some but not related to the locators I want
Thread.sleep() to wait for a time if the element is not loaded on the web page.
I am using Selenium JAVA TestNG
so in the page I write this function:-
Page class I write this function :-
private By btnBed = By.xpath("(//i[#class='bicon bicon-double'])[1]");
public void clickBed(){
// JavascriptExecutor exe = (JavascriptExecutor)driver;
// exe.executeScript("window.scrollBy(0,1000)");
click(btnBed);
}
In my Test I write this:-
hotelPage.clickBed();
Error message:-
Expected condition failed: waiting for visibility of element located by By.xpath
The problem was that this page was opened in a new tab so the code can't locate any elements on the screen.
So I just switch tabs, the code works fine.
ALL I want to do is to select "1 queen bed" or "1 double large bed"
I inspecting the DOM on the link you provided, I see that it is radio button that you might want to select. Try locate the radio element button something like this
<input type="radio" value="1" name="bedPreference_78883120" data-bed-type="">
Locate by name
By.name("bedPreference_78883120")
then perform the click.
Related
I have a Angular SPA application which is having several dialog windows:
Full code: https://pastebin.com/81ikb5gE
<mat-dialog-container aria-modal="true" class="mat-dialog-container ng-tns-c18-93 ng-trigger ng-trigger-dialogContainer ng-star-inserted" tabindex="-1" id="mat-dialog-12" role="dialog" style="transform: none;"><!----><mobileweb-inquiry-menu-dialog _nghost-shn-c52="" class="ng-star-inserted"><mobileweb-client-area _ngcontent-shn-c52="" id="clientarea" _nghost-shn-c3="">
.....
</mat-dialog-container>
I facing the following issue: Into this dialog I have the same html tag ids. Sometimes I have the same ids 4 times. The application is working fine bug I get always the first id and the rest are not found.
I use this code to open the dialog window:
WebDriverWait webDriverWait = new WebDriverWait(driver, 25);
System.out.println("Click on Button " + name + " using id locator " + buttonId);
WebElement webDriverElement = webDriverWait.until(ExpectedConditions.visibilityOfElementLocated(By.id(buttonId)));
webDriverElement.click();
Is it possible to isolate the driver instance of WebDriver only to the elements of the current active dialog?
For example is it possible to get all elements into the dialog window and cut all of the rest html elements?
Are there any other solutions?
The application is working fine bug I get always the first id and the rest are not found :-
for this issue you can try to use xpath axes or xpath indexing.
let's say you have a xpath like this :
//input[#class='some-class']
that represent 4 items, you can perfrom xpath indexing like this :
(//input[#class='some-class'])[1]
or
(//input[#class='some-class'])[2]
and for this :
Is it possible to isolate the driver instance of WebDriver only to the elements of the current active dialog? For example is it possible to get all elements into the dialog window and cut all of the rest html elements?
I would say to have a locator that represent that particular model dialogue.
and you can call getAttribute('innerHTML') and should represent the particular HTML content.
Xpath axes
Consider the following <select> from an internal Selenium test page:
<select id="invisi_select" style="opacity:0;">
<option selected value="apples">Apples</option>
<option value="oranges">Oranges</option>
</select>
It is used to simulate an invisible element as the id suggests, which is done by setting opacity to 0.
Although the element is not visible, a user can actually interact with it. If I open the page in a browser and click on the element's position, the select menu opens. I believe this is also why WebElement#isDisplayed() returns true for this element, which is also what these old Selenium issues suggest:
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1610
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/1941
To execute actions such as clicks, we recently switched to the Java interactions API for several reasons, e.g., to prevent ElementClickInterceptedExceptions. (Please note that this is not about refactoring a bunch of Selenium tests, this happens in the context of a generic action executor that operates on top of the Selenium API.) However, if I do something like:
WebElement applesOption = /* get apples option */
new Actions(webDriver).moveToElement(applesOption)
.click()
.perform();
Moving to the element throws the following exception:
org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
I guess this is because elementsFromPoint() via the WebDriver Actions API seems to return a "non-finite" double for transparent elements like this?
Is there a way to prevent this from happening when using Actions? Maybe, in addition to checking if the element is clickable (ExpectedConditions#elementToBeClickable(...)), I would have to parse—which sounds horrible—attributes such as opacity?
I just tried your sample file locally and the code below is working with no exceptions.
WebElement e = driver.findElement(By.id("invisi_select"));
Select select = new Select(e);
select.selectByValue("apples");
System.out.println(select.getFirstSelectedOption().getText());
select.selectByValue("oranges");
System.out.println(select.getFirstSelectedOption().getText());
It prints
Apples
Oranges
This error message...
org.openqa.selenium.JavascriptException: javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite.
...implies that the WebDriver instance was unable to focus on the element for one or other reasons:
The element havn't loaded properly when you tried to interact with it.
The element haven't got the focus.
Solution
Instead of using the Java interactions API you can use the Select Class and you can use either of the following Locator Strategies:
Using cssSelector and selectByValue():
Select s = new Select(driver.findElement(By.cssSelector("select#invisi_select")));
s.selectByValue("apples");
Using xpath and selectByVisibleText():
Select s = new Select(driver.findElement(By.xpath("//select[#id='invisi_select']")));
s.selectByVisibleText("Apples");
References
You can find a couple of relevant detailed discussions in:
javascript error: Failed to execute 'elementsFromPoint' on 'Document': The provided double value is non-finite
I'm trying to get the image which has the word "Collector" in its title and click on it.
This is the html code for the image and its link:
<a href="javascript:*command*" title="Level III: KPI Collector RYG of D D/Testing - SYS">
<img src="*unusable link because it's only valid for the specific page*" title="Level III: KPI Collector RYG of D D/Testing - SYS">
The <a> and <img> tags are nested in a table cell and some divs. I didn't write the html code so don't yell at me if it's ugly :p
Here is the java code where I try to do it:
WebElement trafficLight = driver.findElement(By.xpath("//img[contains(#title,'Collector')]"));
trafficLight.click();
The error I get is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//img[contains(#title,'Collector')]"}
I'm pretty sure the xpath is ok so I don't think that's the issue.
As the img WebElement is within a frame, you will need to switch focus to that frame before you perform any action on that WebElement. You can do that using WebDriver's switchTo() method like so:
driver.switchTo().frame(frameLocator);
The frame locator can be either its (zero-based) index, name or id attribute, or a previously located WebElement.
Once you have switched focus to the required frame, you should then be able to interact with the WebElement using the same code in your initial post.
Please try this. It will resolve your problem.
WebElement frameSwitch = driver.findElement(By.xpath("Give iframe Xpath location"));
driver.switchTo().frame(frameSwitch); //Switch control to iframe.
//Perform your steps (I.e Click on Image)
driver.findElement(By.xpath("//img[contains(#title,'Collector')]")).click();
driver.switchTo().defaultContent(); //Come out of iframe.
I am trying to select a value available in a read only drop down and I have tried so many options but still failing to select the desired option. The drop down has two values available ValueOne and ValueTwo. By default the ValueOne is selected and in my case I need to select ValueTwo. I used firebug to get the below code when I click on the drop down and do Inspect Element with firebug
The Code is :
<td class="rcbInputCell rcbInputCellLeft" style="width:100%;">
<input id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input" class="rcbInput radPreventDecorate" type="text" readonly="readonly" value="ValueOne" name="ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl02$EditFormControl$rcbControllerType1" autocomplete="off">
</td>
So far I have tried
1----------
Select DropDown = new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")));
DropDown.selectByVisibleText("ValueTwo");
and I get an exception as
:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
2------------
WebElement Dropdown = driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input"));
Select clickThis = new Select (Dropdown);
clickThis.selectByVisibleText("ValueTwo");
Get Exception:
org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"
I also tried selectByIndex but still get the above exception message.
3--------------
driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo");
Nothing happens and the case is marked as Pass. No error no exception.
Also I am running my webscript on firefox 38.0.5 with selenium 2.46.0 with eclipse TestNG.
I have confirmed the frame is not an iframe.
Please suggest the solution.
The root problem might be, that this is not a standard select box but an javascript based solution. The code above just shows the 'host' html element. I am pretty sure that there will be a) a previous hidden element that becomes visible or b) a newly created element in your DOM that holds the values. You have to find those (dev tools or firebug) to interact with.
Some pseudo code that might appear (just to get a hint):
<ul>
<li id="element1">ValueOne</li>
<li id="element2">ValueTwo</li>
</ul>
And after it appears (wait for in selenium) you just have to click the desired element.
Find your xpath through firepath addon in firefox.
driver.findElement(By.xpath(".//*#id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']")).click();
Select value in dropdown ->goto firpath by right click and copy xpath
driver.findElement(By.xpath(".//*#id='ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input']/span[3]")).click();
hope you will find your solution :-)
You can use this :-
driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl02_EditFormControl_rcbControllerType1_Input")).sendKeys("ValueTwo", Keys.ARROW_DOWN, Keys.ENTER)
you can use the following code. Here what I have done is find the dropdown, click on it and find the option to select and send down key until we see the element to select and after click on it.
public class InputDropdownselect {
#Test
public void login() throws Exception
{
System.setProperty("webdriver.chrome.driver", "G:\\drivers\\chrome\\chromedriver_win32\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://yourwebsite.com");
driver.manage().window().maximize();
driver.findElement(By.id("txtuser")).sendKeys("123456");
driver.findElement(By.id("txtpassword")).sendKeys("Abc!#1");
driver.findElement(By.id("log-btn")).click();
Thread.sleep(2000);
driver.findElement(By.id("enrollment")).click();
//driver.findElement(By.xpath("//*[#id=\"enrollment\"]")).click();
driver.findElement(By.xpath("//*[#id=\"studentBasicForm\"]/div[2]/div[9]/div/div/input")).click();
Actions action= new Actions(driver);
WebElement joiningYear=driver.findElement(By.xpath("//input[#placeholder=\"Joining Year Group\"]/following::ul[1]/descendant::li/following::span[contains(text(),\"8\")]"));
do {
action.sendKeys(Keys.ARROW_DOWN).perform();
} while (!joiningYear.isDisplayed());
joiningYear.click();
}
}
I am trying to select a frame in a dojo page with the selenium method shown below is failing
driver.switchTo().frame(target);
I have passed title attribute as my target value.
For the target i do not have id or name attributes, how could i pass the target value for the below element.
<iframe frameborder="0" title="Universal Line Template Configuration" style="width:100%;height:100%;" src="/ucmadmin/ccmadmin-latest?name=universalLineTemplate&key=a91ffd45-a34f-bc90-c159-1bef9e7c9921&permission=3">
When i check by clicking a button in a dojo page, the selenium IDE is showing two operations for a single click, as shown below
click id=g_btnExpandAll_label
click name=g_btnExpandAll
Is anything to be done apart from the line driver.switchTo().frame(target);?
You need to locate the frame first. For example, by xpath relying on the title tag:
WebElement frame = driver.findElement(By.xpath('//iframe[#title="Universal Line Template Configuration"]'));
The use it as a target:
driver.switchTo().frame(frame);
See also:
How to switch between frames in Selenium WebDriver using Java