How can i scroll the panel using Selenium Webdriver - selenium

I want to move down panel using selenium webdriver.
public void AddCode()
{
try
{
getChromeDriver().findElementByClassName("odd").click();
Thread.sleep(5000);
getChromeDriver().manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
/*I want to move down panel here.
Panel tag : <div id="icd9-tab-data">
Scrollbar tag : <div class="mCSB_dragger_bar" style="position: relative; line-height: 505px;"></div>*/
/* "add-ic9-diagnosis-code" link has been displayed in bottom of panel, so not able to click on this link without scrolling panel. */
getChromeDriver().findElement(By.id("add-icd9-diagnosis-code")).click();
Thread.sleep(3000);
}
catch (Exception e)
{
e.printStackTrace();
}
}
I want to move down panel using selenium webdriver.
Panel tag :
Scrollbar tag :

Do you want something like:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,10);");
//This code is tested to scroll the browser scrollbar
Or you can use this code to focus (move to) the element:
Actions action = new Actions(driver);
action.moveToElement(WebElement).perform();
If both are not as per your requirement, please elaborate more about your requirement.

Related

Invalid selector exception for clicking an element using Selenium with Cucumber for W3Schools site

I am trying with the following Selenium Code using Cucumber for W3Schools site. When I click on "Try it yourself" button then it navigates to another page opening different window and the window control also goes to the new window opened. So,In the new window opened, if i click the run button, it throws an exception:
invalid Selector
code:
//This clicks on the Try it yourself button
#FindBy(how=How.XPATH,using="//*[#id=\"main\"]/div[4]/p/a")
private WebElement TryItYourself;
public void TryItYourSelfClick()
{
TryItYourself.click();
}
//Now,a new window opens up where I want to click on Run Button
#FindBy(how=How.LINK_TEXT,using="Run >>")
private WebElement RunButton;
public void RunClick()
{
RunButton.click();
}
Calling Run Method
#Then("^a new window should appear$")
public void a_new_window_should_appear() {
System.out.println("Run button before Clicking");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
obj1.RunClick();
System.out.println("Run after clicking");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Set <String> handle=driver.getWindowHandles();
String firstWinHandle=driver.getWindowHandle();
String WinHandle=handle.iterator().next();
if(WinHandle!=firstWinHandle)
{
driver.switchTo().window(WinHandle);
System.out.println("Working for new window opened");
}
}
why this invalid selector exception is coming?
HTML code:
<div class="w3-bar w3-light-grey" style="border-top:1px solid #f1f1f1;overflow:auto">
<a id="tryhome" href="https://www.w3schools.com" target="_blank" title="w3schools.com Home" class="w3-button w3-bar-item topnav-icons fa fa-home" style="font-size:28px;color:#999999;margin-top:-2px"></a>
<button class="w3-button w3-bar-item w3-green w3-hover-white w3-hover-text-green" onclick="submitTryit(1)">Run »</button>
<span class="w3-right w3-hide-medium w3-hide-small" style="padding:8px 8px 8px 8px;display:block"></span>
<span class="w3-right w3-hide-small" style="padding:8px 0;display:block;float:right;"><span id="framesize">Result Size: <span>433 x 439</span></span></span>
</div>
I'm not familiar with cucumber, but does it have an option to use cssSelector?
I looked at the button and found
body .trytopnav button
as a valid selector.
This error message...
invalid Selector Exception
...implies that the how=How.LINK_TEXT,using="Run >>" was not a valid selector.
The main issue is the element is not a LINK_TEXT but a <button> tag with text as Run ».
Solution
Change the Locator Strategy as follows:
#FindBy(how=How.XPATH,using="//button[contains(.,'Run »')]")
//or
#FindBy(how=How.XPATH,using="//button[contains(.,'Run')]")
private WebElement RunButton;
look here https://www.seleniumhq.org/exceptions/invalid_selector_exception.jsp we need to avoid using >> as possible, so try to use another locator by avoiding these

How to move down and move left the scroll bar that exists within the webpage?

I want to move the vertical and horizontal scrollbar that exists within the webpage itself, I couldn't do that with the below method,
When I inspected an element I got the below one,
<div id="DashboardPageContentDiv" class="DashboardPageContentDiv" style="height: 521px; overflow: auto;">
here is the method I created,
public void scrollbardown(RemoteWebDriver remoteWebDriver) throws Exception {
try
{
JavascriptExecutor jse = (JavascriptExecutor)remoteWebDriver;
jse.executeScript("window.scrollBy(0,2500)", "");
}
catch (Exception exc)
{
}
}
This scrollbars exists with in the frame, here is the frame source code:
<iframe allowtransparency="true" id="symbUrlIFrame2" src="/cos/start.swe?SWECmd=GetCachedFrame&SWEC=13&SWEFrame=symbUrlIFrame2&SRN=xaU5eD1S1IOkspAeHu524NMsHC5h5jzSUipwpmEq8bYb " height="800" width="100%" style="height: 607px; position: relative;"></iframe>
Find the nearest element and use scrollintoview function to move there, the below code works,
JavascriptExecutor js = (JavascriptExecutor) remoteDriver;
WebElement
a=remoteDriver.findElement(By.xpath(".//td[#id='titleView!1Subtitle' and
contains(text(),'Time run: ')]"));
js.executeScript("arguments[0].scrollIntoView();",a );

Selenium Button Click() or Submit()?

Need help on following scenario(using Java):
Doing it manually like this: after filling in some info in a parent page, clicking Continue button on it,
<INPUT TYPE='button' VALUE='Continue' onClick='sendForm()'>
A child window(UserConfirmationPage) pops up with those info from its parent window, clicking the Continue button on the child page, posting the data to server.
<FORM NAME='userConf' ACTION='user.jsp' METHOD='post'>
Do you want to continue?<BR>
<INPUT TYPE='button' VALUE='Continue' onClick='createUser()'>
</FORM>
However, when I do it using Selenium Web Driver, on the parent page,
btnContinue.submit()
a child page pops up with those info from parent page just like what I got when I do it manually but parent does not exist any more. While using
btnContinue.click()
a blank child page is opened without getting any info from parent page and it also complains "session is lost".
I also tried:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();",btnContinue);
and
new Actions(driver).moveToElement(driver.findElement(By.xpath("//input[#value='Continue']"))).click().perform();
but nothing works.
It seems that neither Submit() nor Click() could simulate what was done manually. Any idea?
Thanks a lot in advance!
You did not specify the language, so I assume you're using JAVA:
// since new tab has been opened - need to switch to this tab
// get a list of the currently open windows
Set<String> allTabs = driver.getWindowHandles();
// save the window handle for the current window
String programTab = driver.getWindowHandle();
// switching to the Save tab
String saveTab = ((String) allTabs.toArray()[1]);
driver.switchTo().window(saveTab);
// set timeout
// Click button
driver.findElement(By.id("buttonId")).click();
// set timeout
// switch back to the program tab
driver.switchTo().window(programTab);
Try this it is simplest and easiest code which help you to understand Parent and child scenario.
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://demo.guru99.com/popup.php");
driver.findElement(By.xpath("html/body/p/a")).click();
// return the parent window name as a String
String parentWindow=driver.getWindowHandle();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Pass a window handle to the other window
for(String childWindow: driver.getWindowHandles())
{
System.out.println("child");
//switch to child window
driver.switchTo().window(childWindow);
//find an element and print text of it
WebElement textLabel=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println(" text: "+textLabel.getText());
driver.close();
}
System.out.println("Come to parent window");
//switch to Parent window
driver.switchTo().window(parentWindow);
//find an element and print text of it
WebElement logotext=driver.findElement(By.xpath("html/body/div[1]/h2"));
System.out.println("text: "+logotext.getText());
driver.close();
}

selenium - submenu click not working

i am not able to click the submenu.. tried with different xpath/id .....
below is the html tags, Main Menu is Presentations(marked in red arrow) and submenus are under div.
can you please let me know how i can write xpath for this. i wanted to click hypothetical in the submenu.
here main menu tag is at the bottom of div(submenu).
also attached selenium code . please help me....
<div id="presentations" class="ToolbarSubMenu" align="left"parent="presentations_parent">
<a id="hypothetical" class="ToolbarMenu" href="">Hypothetical</a><br/>
</div>
<a id="presentations_parent" class="ToolbarMenu" href="">Presentations</a>
#Test
public void hypothetical()
{
WebElement ic = driver.findElement(By.id("presentations"));
Actions act = new Actions(driver);
// act.moveToElement(ic).click().build().perform();
//act.moveToElement(ic).doubleClick().build().perform();
act.moveToElement(ic).clickAndHold().release().build().perform();
//ic.click();
//driver.switchTo().window(myWindowHandle);
// driver.findElement(By.linkText("Hypothetical")).click();
// driver.findElement(By.xpath("//div[2][#id='presentations']/a[1]")).click();
//Actions act = new Actions(driver);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// WebElement hyp=driver.findElement(By.partialLinkText("Hypothetical"));
WebElement hyp=driver.findElement(By.id("//div[#id='presentations']/a[1]"));
//act.moveToElement(hyp).click().build().perform();
hyp.click();
Use below code:
//Click on main menu that will opens the sub Menu list
WebElement ic = driver.findElement(By.id("presentations"));
ic.click();
If your requirement is to click on each individual sub menu item,then use below code:
click for Hypothetical is:
ic.findElement(By.id("hypothetical")).click();
click for Profile is:
ic.findElement(By.id("profile")).click();
(Or)
You can also get all subMenu items at a time, like this:
//Get all the sub menu list.
List<WebElement> list = ic.findElements(By.tagName("a"));
for(int i=0;i < list.size; i++){
WebElement subMenuElement = list.get(i);
subMenuElement.click();
}
driver.findElement(By.id("presentations")).click();
WebElement hyp = driver.findElement(By.id("hypothetical"));
hyp.click();

not able to click on following link using selenium webdriver

I am not able to click on following link using selenium webdriver:
<center>
<a class="xyz" style="" href="/Folder">My Folders</a>
<span></span>
</center>
I am using the code:
abhiFX.findElement(By.partialLinkText("My Folders")).click();
I see these potential problems:
Are you sure that your HTML 'works' at all if you load the page in a browser and click on the link? What's the expected result of the click?
Is your driver abhiFX initialized properly? Does .click() on other elements work well?
Try to use xpath instead:
public void clickElement() {
try {
WebElement element = abhiFX.findElement(
By.xpath("//a[contains(text(),'My Folders')]"));
element.click();
} catch (InvalidSelectorException e) {
throw new AssertionError("[FAIL] Click Element: Xpath is invalid.");
} catch (NoSuchElementException e) {
throw new AssertionError(
"[FAIL] Click Element: Unable to locate element");
}
}