How to Click on a Link after You click a Submit button - selenium

After I click a submit button, a new page opens over the existing window. Inside this new window, there is a link. How would you use WebDriver to click on this link? When I right-click on the link text and Inspect Element with Firebug, i get the following:
<a id="ctl100_ContentPlaceHolder1" class="prev-next previousYear" href="Enrollment2013.aspx?4fd70df97f0748ea82e787e5cf5b8552"><<previous year</a>
Thank you.

Java:
driver.findElement(By.id("ctl100_ContentPlaceHolder1")).click();
Python:
driver.find_element_by_id("ctl100_ContentPlaceHolder1").click();

In the case you described, you have to switch to new window then operate there. Look at the code below:
String winHandle = driver.getWindowHandle(); //Gets your current window handle.
for(String windowsHandle : driver.getWindowHandles()){
driver.switchTo().window(windowsHandle); //Switch to new window.
}
driver.findElement(By.id("ctl100_ContentPlaceHolder1")).click(); //Click on the link in new window
driver.close(); //Close the new window after your operations are performed.
driver.switchTo().window(winHandle); //Switch back to original window.
I believe this should help you :)

Related

Right click is not happening and element is getting clicked on the same tab

I have a Selenium script to execute the following steps:
Launch the website - https://vusevapor.com/
Hit "I am 21+" button
Move to element devices.
Right-click on ciro complete kit.
The issue is that the right-click menu remains as is, and the element is getting clicked on the same page.
Here is my code:
//website
driver.get("https://vusevapor.com/");
//clicking on i am 21+ button
driver.findElement(By.xpath("/html/body/aside/div[2]/div/div/div[2]/div/a[1]/span")).click();
Thread.sleep(5000);
//xpath of devices menu
WebElement devices = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/a/span"));
//move to element devices
Actions act = new Actions(driver);
act.moveToElement(devices).build().perform();
Thread.sleep(3000);
//xpath of ciro complete kit
WebElement ciroKit = driver.findElement(By.xpath("//*[#id=\"store.menu\"]/nav/ul/li[2]/ul/li[2]/ul/li[1]/a/span"));
//right click on ciro complete kit
//*****Issue********right click is happening but the element is getting clicked on the same tab and right click menu remains as is
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
try this:
option 1:
act.contextClick(ciroKit).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
option 2-
Use Action and Robot class:
act.contextClick(ciroKit).build().perform();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyPress(KeyEvent.VK_ENTER);
Hope this helps.

Trying to hover on a menu and click on a link in the sub-menu but no luck

I am learning to hover over a menu and click on a link int he sub-menu.
The scenario is go to the URL "https://www.amazon.in", hover over "Hello Sign in" and then click on the link "Start here".
I can hover over "Hello Sign in" using moveToElement() and sub menu is opening, but couldn't click on the link "Start here".
Here is my code.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement startHere =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Start here")));
startHere.click();
The link text includes a dot at the end, you missed it in your code, Try By.linkText("Start here.") or By.partialLinkText("Start here")
Please try the below modified code and it's working as expected. First you can find the new Customer Div and then directly access the start here link as below.
WebElement signUp = driver.findElement(By.id("nav-link-yourAccount"));
Actions action = new Actions(driver);
action.moveToElement(signUp).build().perform();
WebElement newCustomer=driver.findElement(By.id("nav-flyout-ya-newCust"));
newCustomer.findElement(By.xpath(".//a")).click();
To access the URL https://www.amazon.in then hover over Hello Sign in and then click on the link Start here. you have to induce WebDriverWait for the element with text as Hello Sign in to be visible, then Mouse Hover over it and then induce WebDriverWait for the element with text as Start here. to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.in/");
WebElement signUp = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("nav-link-yourAccount")));
new Actions(driver).moveToElement(signUp).build().perform();
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.linkText("Start here."))).click();
Browser Snapshot :

How to click on a check box in a new window through selenium webdriver

I am trying to make this check box click but not I'm not able to click on check box.
Please send me the xpath or css
I tried:
xpath
//div//*[#id='thCheckBox']
.//*[#id='searchOrderModel']/div/div/div[3]/div/button[2]
There is a label next to the checkbox. You can try to click on the label then it will click the checkbox using below x-path.
//label[#for='thCheckbox']
As per the HTML you have provided and your question as the Check Box is on a new window to click() on the Check Box you have to induce WebDriverWait for the Check Box to be clickable and you can use the following Locator Strategy :
cssSelector :
"table.table.table-hover.dataTable#orderNoDropdown label[for='thCheckBox']"
xpath :
"//table[#class='table table-hover dataTable' and #id='orderNoDropdown']//label[#for='thCheckBox']"
As you have mentioned it is new window, then you will have to switch to new windows in order to interact with the web element. after reaching to new window you can use this code :
ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Now you have focused on new Windows , and you can interact with check box now :
WebElement checkBox = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("thCheckBox")));
checkBox.click();
Then again you have to switch back to page from where you went to this Page, if you want that.
driver.close();
driver.switchTo.windows(tabs.get(0));

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

How to open multiple windows using selenium webdriver?

I have to check switching behavior of sites but when i test it on site http://www.baidu.com , which in usual browsers, opens options in a new tab, opens the clicked option in the same tab while using selenium. How to make it execute as it executes normally in browsers?
Following code will help you to open link in new window :
driver.get("http://www.google.com"); //Replace your URL
WebElement link = driver.findElement(By.xpath("Your Element Xpath"]"));
Actions act = new Actions(driver);
act.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// It will close new opened window
driver.close();
You can use id or other locator for element as per your flexibility.
We are just pressing keys Shift + Up by selenium code.