Data not retrieving when clicking on search button - selenium

The search button is inside the frame and I am connecting to the frame with the below code.
driver.switchTo().frame("autoCompleteDialogIF");
I am able to go the frames section.
Search button syntax:
Find
Here in the frame section I have text box and when I enter the values in text box and perform search the data is not retrieving which matches with the text.
Code used:
WebElement elementclick = driver.findElement(By.xpath(".//*[#id='filterPanelFindButton']/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elementclick);

Try to click on element using java-script executor method.
WebElement elementclick = driver.findElement(By.xpath("//a[contains(text(), 'Find')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementclick);
OR
Try to click on element using xpath locator with Explicit wait method.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//a[contains(text(), 'Find')]"))));
driver.findElement(By.xpath("//a[contains(text(), 'Find')]")).click();

I think you can just use like this:
driver.findElement(By.linkText("Find"))

Related

Unable to click on "know more..." link on the page given below using the selenium webdriver

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();

Unable to switch to iframe using Selenium Webdriver

I am trying to learn the feature drag and drop in the webpage
Link
The Section Books is inside the iframe.
But i am unable to access the iframe I am getting below error
no such element: Unable to locate element:
Below are the Xpath i tried
// WebElement frame =driver.findElement(By.xpath("//div[#id='root']//iframe[#class='st-preview-body']"));
// WebElement frame =driver.findElement(By.tagName("iframe"));
WebElement frame =driver.findElement(By.xpath("//iframe[#src='https://snippet.webixcode.com/snippet.html?0.0.3']"));
driver.switchTo().frame(frame);
I also tried using by.id and also xpath using id and class name
What will be the mistake i am doing? To my knowledge there is only one iframe present
The code for drag and drop. will this work?
WebElement fromdrag=driver.findElement(By.xpath("//span[#class='dhx_tree-list-item__text'][normalize-space()='Lawrence Block']"));
WebElement todrop=driver.findElement(By.id("treeTarget"));
Actions act=new Actions(driver);
act.dragAndDrop(fromdrag, todrop).build().perform();
Its a Nested iframe. We need to switch to those iframes one by one to access Elements from All Books.
The page takes time to load, better apply Explicit waits to find the Elements. And you also need to driver.switchTo().defaultContent(); to come out of the iframe and find Elements outside the iframe
public void iframequestion() {
System.setProperty("webdriver.chrome.driver","C:\\expediaproject\\Chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
WebElement frame1 = driver.findElement(By.xpath("//iframe[#src='https://snippet.dhtmlx.com/3e0b5r57?mode=mobile']"));
js.executeScript("arguments[0].scrollIntoView(true);", frame1);
driver.switchTo().frame(frame1);
WebElement frame2 = driver.findElement(By.xpath("//iframe[#class='st-preview-body']"));
driver.switchTo().frame(frame2);
WebElement frame3 = driver.findElement(By.id("content"));
driver.switchTo().frame(frame3);
WebElement ele1 = driver.findElement(By.xpath("//span[text()='Fiction & Fantasy']"));
System.out.println(ele1.getText());
driver.switchTo().defaultContent();
WebElement ele2 = driver.findElement(By.xpath("//a[text()='View more demos']"));
System.out.println(ele2.getText());
driver.quit();
}
Fiction & Fantasy
View more demos

How to click on save button in chrome print privew page using selenium Java

I am currently looking for a solution to click on Sava button in chrome print preview window with selenium Java.
Is there any way we can handel chrome print preview page?
I have tried with the Robot class, but it seems not reliable/stable for my application.
Could you please someone help me to achieve this with selenium Java.
I fail to see any "Save" button on print preview page for Chrome browser
Here is how you can click "Cancel" button, I believe you will be able to amend the code to match your requirements:
First of all make sure to wait for the preview page to be available using Explicit Wait
Change the context to the print preview window via WebDriver.switchTo() function
All the elements at the print preview page are hidden in ShadowDom therefore you will need to:
Locate the element which is the first parent of the element you're looking for
Get its ShadowRoot property and cast the result to a WebElement
Use the WebElement.findElement() function to locate the next parent
repeat above steps until you reach the desired button
Example code just in case:
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
driver.switchTo().window(driver.getWindowHandles().stream().skip(1).findFirst().get());
WebElement printPreviewApp = driver.findElement(By.tagName("print-preview-app"));
WebElement printPreviewAppConten = expandShadowRoot(printPreviewApp, driver);
WebElement printPreviewSidebar = printPreviewAppConten.findElement(By.tagName("print-preview-sidebar"));
WebElement printPreviewSidebarContent = expandShadowRoot(printPreviewSidebar, driver);
WebElement printPreviewHeader = printPreviewSidebarContent.findElement(By.tagName("print-preview-header"));
WebElement printPreviewHeaderContent = expandShadowRoot(printPreviewHeader, driver);
printPreviewHeaderContent.findElements(By.tagName("paper-button")).get(1).click();
where expandShadowRoot function looks like:
private WebElement expandShadowRoot(WebElement parent, WebDriver driver) {
return (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot", parent);
}
Save button will not work because page is not part of webpage. selenium only support web based application.
But you can use SIKULI to handle above scenario.

When findElement function is not able to find an text box, how to sendKeys to the text box? Is there any alternative for findElement?

When findElement function is not able to find an text box, how to sendKeys to the text box? Is there any alternative for findElement?
This is a question asked in an interview
You can also use JavascriptExecutor to do the same,
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('someid').value=arguments[0]","text to be inserted in input box");
You can try using JavascriptExecutor in this case:
For eg. to input a text in Google search textfield, the following code should work:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById('gbqfq').value = 'Ripon Al Wasim';");
ref url: How to input a value in a text field/box by using JavasSript in Selenium WebDriver
Maybe try this script:
WebDriverWait wait = new WebDriverWait(driver, 1);
wait.until(ExpectedConditions.elementToBeClickable(By.id("element's id here"))).click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("element's id here"))).sendKeys("enter text you want to put in");
Yes, we have. Using Keys we can insert text in the textbox. But before going to insert the value into the textbox we need to inspect the element which is just before the textbox and then have to press tab button to jump to the text box.
Inspected element locator value:
d.findElement(By.xpath("")).sendKeys(Keys.TAB,"enter your text");
entered text will be inserted into your text box.

How to click a button created using <span> tag for Selenium webdriver?

I have span tag which looks like a button on html tag
<span class="middle">Next</span>
I tried using
xpath=driver.findElement(By.xpath(".//*[#id='modal-actions-panel']/div[2]/a/span/span/span")); // by considering fixed id as reference
Using absolute
xpath=driver.findElement(By.xpath("html/body/div[4]/div[2]/a/span/span/span")); // took this from firebug
and Using
driver.findElement(By.cssSelector("span[class='middle']"));
No success!! It is throwing below exception :
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[contains(., \"Next\")]"}
Command duration or timeout: 30.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
For all the ways I tried it is showing the same exception with change in selector details. Can someone please help me out in finding solution so that I can find Next button that is in span tag and click it.
Next button is in iFrame: Below is the part of html covering required span tag.
Next
I also tried with :
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
But throwing below error :
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Kindly let me know if needed something that I'm missing..
I think this element is inside a frame or iframe, if it is then you need to switch that frame or iframe before finding element as below :-
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
el.click();
//Now after all your stuff done inside frame need to switch to default content
driver.switchTo().defaultContent();
Edited1 :- If you are getting exception as element is not currently visible need to implement WebDriverWait to wait until element visible as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//span[#class = 'middle' and contains(text(), 'Next')]")));
el.click();
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Edited2 :- If unfortunately it's not getting visible try to click on it using JavascriptExecutor as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
//Find frame or iframe and switch
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
//Now find the element
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//span[#class = 'middle' and contains(text(), 'Next')]")));
//Now click using JavascriptExecutor
((JavascriptExecutor)driver).executeScript("arguments[0].click()" el);
//Once all your stuff done with this frame need to switch back to default
driver.switchTo().defaultContent();
Try this....
driver.findElement(By.xpath("//span[contains(#class,'middle') and contains(text(), 'Next')]"))
I tried it and it worked for me:
WebElement actionBtn=driver2.findElement(
By.xpath("//span[contains(#class,'v-menubar-menuitem-caption')
and contains(text(), 'Actions')]")
);
actionBtn.click();
Try this
new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(#class,'middle') and contains(text(), 'Next')]"))).click();