Could you please help me to call href value which is changing every time. Below is the code for your reference:
click here
Thanks.
As I understand from your comment, Test cases needed the href (link) attribute value. So code can be written by this way :
String strLinkHref = driver.findElement(By.linkText("click here")).getAttribute("href");
or
String strLinkHref = driver.findElement(By.xpath("//a[text()='click here']")).getAttribute("href");
Note : here you can store in String and print. It will get link dynamically every time.
If test case needed to open it, then you can use :
driver.get(strLinkHref);
If you require to move to TAB window, Please use below code :
String handle= driver.getWindowHandle();
System.out.println(handle);
// Click on the Button "New Message Window"
driver.findElement(By.name("New Message Window")).click();
// Store and Print the name of all the windows open
Set handles = driver.getWindowHandles();
System.out.println(handles);
// Pass a window handle to the other window
for (String handle1 : driver.getWindowHandles()) {
System.out.println(handle1);
driver.switchTo().window(handle1);
currentURL = driver.getCurrentUrl();
System.out.println(currentURL);
}
// Closing Pop Up window
driver.close();
Reference
Related
I am doing a sendkeys on an element of input type but when I watch the navigator it writes for example "And", it erases, it writes "ré M", it erases, and it writes again "uzin" instead of just writting "André Muzin".
So my test is failed.
Here the code to find the element :
#FindBy(how = How.CSS, using = "input[data-automation-id='searchBox']")
public WebElement TB_MENTOR2;
Here the method which is calling it :
public void AddMentor(String functionality, String mentorName, String mentorType, String comment){
System.out.println(" ----- Going to the Mentor Page");
TB_SEARCH.sendKeys(functionality);
TB_SEARCH.sendKeys(Keys.ENTER);
TB_GOTO_ADDMENT.click();
TB_MENTOR1.click();
TB_MENTOR2.sendKeys(mentorName);
...
}
Do you have an idea ?
The webpage make a reset. And Selenium is typing really fast !
That's why I needed to wait 2s before sendKeys, to wait the reset.
Select se = new Select(driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select")));
se.selectByIndex(7);
driver.findElement(By.xpath(".//*[#id='33629']/div/div[1]/div[2]/div[1]/select/option[8]")).click();
Above code doesn't work,please help
Error returned:
Exception in thread "main" org.openqa.selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found
org.openqa.selenium.NoSuchWindowException: no such window
Means the browser is close when you are trying to interact with it. Remove driver.close() from your code and put it only after you have finished all you interactions with the browser.
Edit
If you need to return to parent window after closing child window use driver.switchTo() again
// get parent window ID
String parentHandle = driver.getWindowHandle();
// switch to the new window
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(parentHandle))
{
driver.switchTo().window(handle);
}
}
//do something with the new window
// switch back to the old window
driver.close();
driver.switchTo().window(parentHandle);
windowIdbefore = driver.getWindowHandle();
System.out.println(windowIdbefore);
Set<String> windowid = driver.getWindowHandles();
for (String string : windowid) {
System.out.println(string);
driver.switchTo().window(string);
// enter code here
}
WebDriver driver=new FirefoxDriver();
Select s=new Select(driver.findElement(By.xpath("xpathExpression")));
s.selectByVisibleText("text");
s.selectByValue("value");
s.selectByIndex(1);
as i see here the dropdown box is present in div tag. i think with your code dropdown has been located but you are not able to select the value present in dropdown. Then follow below code
WebDriverWait wait = new WebDriverWait(d, 10);
Actions builder = new Actions(d);
WebElement selectvalue = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("your drop down xpath value")));
builder.mouse.mouseMove(((Locatable)selectvalue).coordinates);
selectvalue.click();
WebElement option = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("locator value of dropdown value(your dropdown value)")));
builder.mouse.mouseMove(((Locatable)option).coordinates);
option.click();
System.out.println("dropdown value slected...");
thanks for your time.
I have the following code (Using this to test the filter/search on my web app):
(Don't worry so much about the words I'm searching etc, I just want to get the browser working after pressing search):
// Store my words
String sWordOne = "HTML1";
String sWordTwo = "WONT2";
String sWordThree = "WONTWORK3";
// Add These Entries
Business_Surveys_Survey.Surveys_AddIndividualSql(sWordOne, "I have added this to check spelling", "Questions Tab Spelling");
Business_Surveys_Survey.Surveys_AddIndividualSql(sWordTwo, "I have added this to check spelling", "Questions Tab Spelling");
Business_Surveys_Survey.Surveys_AddIndividualSql(sWordThree, "I have added this to check spelling", "Questions Tab Spelling");
navigateToSurveysManageSurveys();
WebElement sSearchBtn = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_btnSearch"));
WebElement sSearchBoxText = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_txtSearchBox"));
String sExpectedMessage = "Displaying 3 Records";
String sDisplayingXRecords = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList")).getText().trim();
String sSurveysOne = sDisplayingXRecords.substring(0, 20);
assertThat(sSurveysOne, containsString(sExpectedMessage));
sSearchBoxText.sendKeys("<HTML>");
sSearchBtn.click();
sSearchBoxText.clear();
String sDisplayingXRecordsOne = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_upSurveysList")).getText().trim();
String sDisplayingSurveys = sDisplayingXRecordsOne.substring(0, 20);
String sExpectedMessageTwo = "Displaying 1 Record";
assertThat(sDisplayingSurveys, containsString(sExpectedMessageTwo));
Business_Surveys_Survey.Surveys_NukeSurveyListbox();
Everytime it executes the following:
sSearchBoxText.sendKeys("<HTML>");
sSearchBtn.click();
it breaks and I can't talk to the browser via webdriver:
org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died.
I thought maybe its because im adding in some HTML tags, but even when I search for a basic word, same result. I've tried just sending the return key in the box and pressing the search button on the page.
Any idea's? I have a suspicion its the following,
navigateToSurveysManageSurveys();
WebElement sSearchBtn = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_btnSearch"));
WebElement sSearchBoxText = driver.findElement(By.id("ctl00_ctl00_MainContentPlaceHolder_MainContentPlaceHolderSurvey_txtSearchBox"));
If it is can you please explain what I have done wrong and how I should be handling this better?
Thanks for your time
Here is my config:
Windows 7 VM + Selenium Web Driver + IE 11 + IE Driver Server 64 bit
I have a weird problem, where in, when a click event is encountered in code, the execution just suspends and doesn't proceed. I was able to overcome this by using sendKeys(Keys.Enter) instead of click. But i see the same behavior while trying to switch to a popup window and returning back to the parent frame.
This is the code i use for switching to child and returning to parent window :
WebDriver popup = null;
String popwin = "";
boolean present;
try {
System.out.println(driver.getCurrentUrl());
String winHandleBefore = driver.getWindowHandle();
for (String winHandle : driver.getWindowHandles()) {
System.out.println("WinHandle : " + winHandle);
popwin = winHandle;
}
popup = driver.switchTo().window(popwin);
Actions action = new Actions(popup); action.moveToElement(popup.findElement(by.id("idContinue")))
.sendKeys(Keys.ENTER).build().perform(); //works fine till above, clicks continue button in popup and the popup closes automatically
driver.switchTo().window(winHandleBefore);
The execution suspends at the last line, the Thread keeps running with no results and no exceptions are thrown.
Thank you!!
Note: The application under test is an IE only compatible system and hence there is no way to test it in other browsers.(the App doesn't even load in other browsers)
Is this part of the code executed after the popup is displayed on the screen or before it?
If this is executed after the popup is shown then the line :
String winHandleBefore = driver.getWindowHandle();
is getting the instance of the popup, and on closing the pop up by clicking the button you are destroying that instance itself.
So there are 2 solutions to this issues:
Place the line : [String winHandleBefore = driver.getWindowHandle();] before the popup appears on the screen.
Modify the present code:
WebDriver popup = null;
String popwin = "";
boolean present;
try {
System.out.println(driver.getCurrentUrl());
for (String winHandle : driver.getWindowHandles()) {
System.out.println("WinHandle : " + winHandle);
driver.switchTo().window(winHandle);
Actions action = new Actions(popup);
action.moveToElement(popup.findElement(by.id("idContinue"))).sendKeys(Keys.ENTER).build().perform();
break;
}
These two tricks will surely solve the issue you are facing
I have a registration page that contains many input fields. This registration page will open when I click on a Hyperlink i.e. registration link, it will open a new tab and then I need to put the values for the respective input fields such as Email Address, First Name, Last Name and so forth through the script (selenium webdriver - java).
As of now, it opens a new window with the registration page but its not placing the values in the respective fields..
I have used the following script :
webDriver.findElement(By.name("email")).sendKeys("paul#vendormate.vm");
webDriver.findElement(By.id("vmBtnSubmitExpReg")).click(); webDriver.findElement(By.name("confirmEmail")).sendKeys("paul#vendormate.vm");
Can anyone tell me what shall I do to place the values in the registration page?
Thanks
You need to change window for webdriver
String currentWindow = driver.getWindowHandle(); //saves current window name
//opens new window
String popupWindow = webDriverExtensions.popupHandle(currentWindow); //now you have second window name
driver.switchTo().window(popupWindow); // switched to registration window
// doing your job
driver.close(); //closing windows
driver.switchTo().window(currentWindow); //return back to main window
public String popupHandle(String existingWindowHandle) {
String popupHandle = null;
Set<String> windowHandles = driver.getWindowHandles();
for (String handle : windowHandles) {
if (!handle.equals(existingWindowHandle)) {
popupHandle = handle;
break;
}
}
return popupHandle;
}