selectFrame is failing in Dojo pages when using selenium - java

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

Related

How to locate an element on this page?

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.

How to get the right Xpath for the <li> HTML element?

i should make Selenium to click on the element of drop down menu using Java and Inteliji. I should click on the "today" button. I tried to copy the xpath, use cssselector, i used extensions like xpath finder etc, no result. The element is <li> type, so i guess the problem is here. Any suggestions how to find the correct Xpath?
P.S. sorry for uploading the image, as a new user, i can't put them exactly in the text.
Drop down menu image
html code for the elements
You can't always get reusable XPath locator for selenium from the browser's tool. It returns an absolute XPath. You need to construct relative XPath for the elements.
Here you can learn about XPath and how XPath locators work.
The following locators based on the image you have posted.
XPath:
WebElement liToday = driver.findElement(By.xpath("//div[contains(#class,'daterangepicker') and contains(#class,'dropdown-menu')]/div[#class='ranges']/ul/li[text()='Today']"));
CSS Selector:
WebElement liToday = driver.findElement(By.cssSelector("div.daterangepicker.dropdown-menu > div.ranges > ul > li"));
After locating the element,
this part is for after you have clicked the date box and the dropdown is showing.
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOf(liToday));
liToday.click();

Get page element by title attribute - Selenium and Java

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.

How to get Text from this HTML code By using Selenium WebDriver with Java

HTML Code
<label for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21">Royality Free</label>
Selenium Code
driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21")).getText();
The above selenium code is not working even i tried getAttribute();
its showing NullPointerException
You are trying to read text from the label but you are finding an element which has id ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 This is not the id of the label.
Your code should be:
WebElement labelElement = driver.findElement(By.cssSelector("label[for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21"]"));
System.out.println(labelElement.getText());
This should work.
Moreover, the locator: ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 seems to be a randomly generated locator. Just confirm that it's not such a case. If it is then you will need to change your locating strategy.

Cannot find element in the popup window with Selenium Webdriver

I'm using InternetExplorerDriver with Selenium Webdriver in Java.
I load a URL which simultaneously opens another window which has the login box.
Using the method mentioned here
I could select the required window and also send keys into the focussed box.
popup.getKeyboard().sendKeys("yeuiryuiryweuiryeuiyterui");
However, the following:
popup.findElement(By.id("userName")).sendKeys("user")
is unable to find the element and throws an Element-not-found exception (something similar to this).
Where am I getting it wrong?
I will suggest you to try with xpath
String locator = "//*[#class='Class name of user name text box'and text()='if any']"
you can find the user name through firebug
List<WebElement> element = Driver.driver.findElements(By.xpath(locator));
thn use click functions on web element , do the same on password and then click on login directly using Driver.selenium.click(xpath); and provide the xpath of login button in case id's are not present.

Categories