I am using the method FindElementsByCssSelector like
var iframes = RemoteWebDriver.FindElementsByCssSelector("iframe");
and that gives me two iframes.
If I write
$('iframe');
in the browser development console, it displays only one.
In the DOM, one iframe is inside another like:
iframe
html
body
form
div
table
tbody
tr
td
iframe
Locate the firs frame and use it to locate the other one
IWebElement firstIframe = RemoteWebDriver.FindElementByCssSelector("iframe");
RemoteWebDriver.SwitchTo().Frame(firstIframe);
IWebElement secondIframe = iframe.FindElementByCssSelector("iframe");
RemoteWebDriver.SwitchTo().Frame(secondIframe);
Related
page url: https://netbanking.hdfcbank.com/netbanking/
enter image description here
I tried using switch to frames from selenium webdriver, still it doesn't help me
Below is the code sample
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//div[#id='nortonimg2']/div/span/following-sibling::span/a")).click();
You just need to switch to the frame and then click the proper element inside it.
The following code should work:
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//div[#id='welcomesec2']//a")).click();
The result is
You were almost there but the locator strategy for the link Know More... was slightly off as there are multiple elements with almost similar attributes:
Solution
You can use either of the following locator strategies:
Using partialLinkText:
WebElement frame = DriverManager.getDriver().findElement(By.cssSelector("frame[name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.partialLinkText("More...")).click();
Using cssSelector:
WebElement frame = DriverManager.getDriver().findElement(By.cssSelector("frame[name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("div#welcomesec1 span.lightbluecolor > a")).click();
Using xpath:
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//div[#id='welcomesec1']//span[starts-with(., 'Your security is of utmost')]//following::span/a[starts-with(., 'Know') and contains(., 'More...')]")).click();
Although your XPath for 'Know More' link is correct, somehow web driver is not able to click on it. Try a different XPath for the same 'Know More' link, like below:
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//*[#id=\"nortonimg1\"]/div[2]/span[2]/a")).click();
I am using TestNG and selenium for testing web app.
<div class="infoMessage">The request has been successfully submitted.</div>
In the my TestNG class file, like any other HTML elments for div element also
I have
#FindBy(xpath="//*[#id='wrapper']/table/tbody/tr[1]/td/div")
WebElement resultdiv;
Now that I got the webelement in resultdiv, how can i read the content "The request has been successfully submitted" ?
Just at a quick glance, it seem like you can try use className instead xpath:
#FindBy(className="infoMessage")
WebElement resultdiv;
Use .getText(); to achieve:
String text = resultdiv.getText();
Hi #bnath002 You can use Contains text Xpath, below is the code. this code always work for text.
WebElement element = driver.findElement(By.xpath("//div[contains(text(),'The request has been successfully submitted.')]"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);
I'm a little unfamiliar with #FindBy, but i'm assuming you could use getText() as normal. But if everything went as planned and the xpath located your element successfully, you should be able to retrieve the text with the following :)
WebElement element = driver.findElement(By.xpath("//*[#id='wrapper']/table/tbody/tr[1]/td/div"));
String innerText= element.getText();
System.out.println("Your inner text is: "+innerText);
I need some help in selecting a div form many div's that for every session number of div's are changing.
For example:
one time a have the div in that position(absolute path): /html/body/div[97]
other time in that position (absolute path):/html/body/div[160]
and so on...
At a moment only one div is active and the other div's are hidden.
I attached a picture to show the code.
I try the xpath below but doesn't work,I get the error "no such element: Unable to locate element ...
driver.findElement(By.xpath(".//*[#class=\'ui-selectmenu-menu ui-selectmenu-open\']/ul/li[1]")).click();
Picture with html code is here:
XPath is great if you expect the target element to be in the exact location every time the page is displayed. However, CSS is the way to go if the content is constantly changing.
In the example below I have found the DIV and the Element that you highlighted in your screen capture.
WebElement targetElementDiv = null;
WebElement targetElement = null;
targetElementDiv = driver.findElement(By.cssSelector("[class='ui-selectmenu-menu ui-selectmenu-open']"));
if (targetElementDiv != null) {
targetElement = targetElementDiv.findElement(By.cssSelector("[class='ui-menu-item ui-state-focus'])");
}
I want to get URL's of hyperlinks present on current page using Selenium Web driver. Can anyone help.
To get the URL of all the links on a page, you can store all the elements with tagname 'a' in a WebElement list and then you can fetch the href attribute to get the link of each WebElement.
you can refer to the following code :
List<WebElement> links = driver.findElements(By.tagName("a")); //This will store all the link WebElements into a list
for(WebElement ele: links) // This way you can take the Url of each link
{
String url = ele.getAttribute("href"); //To get the link you can use getAttribute() method with "href" as an argument
System.out.println(url);
}
Just get them from the href attribute using getAttribute() (assuming you are in java):
WebElement link = driver.findElement(By.tagName("a"))
String url = link.getAttribute("href")
This is my html code:
YIKKU, TFYTUR
I want to click on the link name YIKKU TFYTUR, i have tried the following but nothing worked-
driver.findElement(By.partialLinkText("YIKKU, TFYTUR")).click();
driver.findElement(By.cssSelector("a[href*='Y']")).click();
can anyone please help me??
The only solution to these kind of Href tags are find the nearest "id" element, in my case was this-
<table id="resSearchResultsTBL">
then find this element using css selector:
WebElement guest = driver.findElement(By.cssSelector("table[id='resSearchResultsTBL']"));
and then find again in this element a sub element of "a href" tag:
guest.findElement(By.cssSelector("a[href*='guestProfile.do']")).click();
This worked perfectly for me.:)
Try -
WebElement link = driver.findElement(By.xpath("//a[#name=\"Y\"]"));
wait.until(ExpectedConditions.elementToBeClickable(link));
link.click();
or
WebElement link = driver.findElement(By.xpath("//a[#target=\"sgr\"]"));
wait.until(ExpectedConditions.elementToBeClickable(link));
link.click();