Selenium Web Driver - Error communicating with remote browser after searching - java

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

Related

Selenium: ElementNotInteractableException: element not interactable when trying to access autoComplete text box

I am stuck with the WebElement which I am trying to access on the Webpage with the below code but still getting mentioned error. The Element allows to autocomplete the subjects and multiple subjects to be entered in the single text box.
WebElement Subjects = driver.findElement(By.xpath("//*[#id='subjectsContainer']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", Subjects);
String subject1 = prop.getProperty("subject1");
String subject2 = prop.getProperty("subject2");
String subject3 = prop.getProperty("subject3");
Subjects.sendKeys(subject1);
Subjects.sendKeys(Keys.ENTER);
Subjects.sendKeys(subject2);
Subjects.sendKeys(Keys.ENTER);
Subjects.sendKeys(subject3);
Subjects.sendKeys(Keys.ENTER);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
WebElement accepts the multiple subjects to be entered by user which autocompletes
Selenium WebElement error
Looks like you need to add some delay or element visibility validation before you trying to send text to this element.
Additionally it's not recommended to use JavaScript click instead of WebDriver click unless you have no choice.
Please find below answer for my query as it is now working fine after putting Explict wait condition and locating correct webElement with tagname 'input'.
//Load the data from the Properties file
String subject1 = prop.getProperty("subject1");
String subject2 = prop.getProperty("subject2");
String subject3 = prop.getProperty("subject3");
//WebElement to capture the visibility condition
WebElement Subjects =driver.findElement(By.xpath("//*[#id='subjectsContainer']"));
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(Subjects)).click();
WebElement Sub1 = driver.findElement(By.xpath("//input[#id='subjectsInput']"));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#id='subjectsInput']")));
Sub1.sendKeys(subject1); //Send first Subject
Sub1.sendKeys(Keys.ENTER);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#id='subjectsInput']")));
System.out.println("ENtered subject 1" + subject1);
Sub1.sendKeys(subject2); //Send second subject
Sub1.sendKeys(Keys.ENTER);
System.out.println("ENtered subject 2" + subject2);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#id='subjectsInput']")));
Sub1.sendKeys(subject3); //Send thrid subject
Sub1.sendKeys(Keys.ENTER);
System.out.println("ENtered subject 3" + subject3);
A quick and dirty solution to many not interactable errors is simply this:
Thread.sleep(500);
I find this tactic to be extremely useful in quickly debugging problematic elements before implementing a more performant and elegant wait solution like you mentioned in your update.

Calling href value in Selenium Webdriver

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

selenium webdriver : sendkeys is not entirely send, it resets the text

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.

WebDriver is unable to perform click on an input element having onclick=function1() as an attribute

I am using Web driver 2.31 with Java. It seems the web driver is unable to perform click action on an input element having onclick() attribute.
The input element I need to perform click action on is having the following attributes - id (which is a random generated number), class, type=button, onclick, onmouseout, onmouseover, title and value.
I'm able to fetch the values of title and value attributes which means that the web driver is able to recognize the input element but is not able to perform click action on it.
I have tried with the following:
webdriver.findElement(By.xpath("xpath for the input")).click()
webdriver.findElement(By.xpath("xpath for the input")).sendKeys(Keys.ENTER);
new Actions(webdriver).moveToElement(webdriver.findElement(By.xpath("xpath for the input"))).click().perform();
None of the above options are working.
Do you get any exceptions from element.click()? It it enabled and visible? One of the problems we had was that WebDriver didn't handle position:static elements correctly, so during playback it would cover the button (and you won't see it on screenshot) and it would throw exception "Element is not clickable at point".
We had similar problem with element and had following code that did work sometimes (but also not 100% of times):
element.click();
if("button".equals(tagName)) {
if(element.isEnabled() && element.isDisplayed())
element.sendKeys(Keys.ENTER);
}
But the problem disappeared itself after upgrading WebDriver and we removed sendKeys(ENTER), also it was working fine in 2.29.0.
I faced exactly same problem in my project. The issue was not to locate the element but the onClick() event was not firing.
Then i found out that something else was there which stopped from the event to fire. I had used java script to enable the date picker box & did this,
((JavascriptExecutor)driver).executeScript ("document.getElementById('txtOriginDate').removeAttribute('readonly',0);");
WebElement originDateBox= driver.findElement(By.xpath(prop.getProperty("originDateBox")));
originDateBox.clear();
originDateBox.sendKeys("9-Dec-2014"); //Enter date
Developer designed this in such a way that if you don't use date picker to select date, a specific variable was not set. Which eventually made the **onclick event not to fire.
The date picker code was something like this,
var jsoncustdate = "";
var jsonorigindate = "";
function onSelectCalender( StrDt, obj )
{
if ( !varReadonly ) {
if ( $( "#txtCustDescisionDate" ).attr( "IsDisable" ) == "FALSE" )
{
if ( obj.id == "txtCustDescisionDate" )
{
custobjDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay, 0, 0, 0, 0 );
jsoncustdate = custobjDt.getTime();
jsoncustdate = "\/Date(" + jsoncustdate + ")\/";
DisabledBtnStage();
// $("#txtFromDate").datepicker("option", "maxDate", objDt);
}
if ( obj.id == "txtOriginDate" )
{
var objDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay,0, 0,0,0 );
jsonorigindate = objDt.getTime();
jsonorigindate = "\/Date(" + jsonorigindate + ")\/";
DisabledBtnStage();
// $("#txtToDate").datepicker("option", "minDate", objDt);
}
}
elogCommon.CheckMandatory();
}
}
I finally used date picker in normal way & the event fired smoothly.
I hope this answer will help . .cheers !!!

jericho Html parser error in jsp page

I have write code as
String sourceUrlString="http://some url";
Source source=new Source(new URL(sourceUrlString));
Element INFORM = source.getElementById("main").getAllElementsByClass("game").get(i-1);
String INFORM = INFORM.replaceAll("\\s",""); //shows error here
sendResponse(resp,+INFORM);
Now i want the text fetch from Element INFORM is Neglect white space how can i do so? above mentioned String INFORM Show error Duplicate local variable INFORM);
e.g
text fetch by Element INFORM is "my name is satish"
but it must send response as
"mynameissatish"
You have the name INFORM used twice - and thats not possible!
String sourceUrlString = "http://some url";
Source source = new Source(new URL(sourceUrlString));
Element INFORM = source.getElementById("main").getAllElementsByClass("game").get(i-1);
String response = INFORM.replaceAll("\\s",""); // ! Use another name here !
sendResponse(resp, respone); // or use '+' - not shure if 1 or 2 args

Categories

Resources