WebDriver: how to wait for text to disappear in the element - selenium-webdriver

I have a field that dynamically changes it's text. I need a way to wait for the text to be changed. I'm not aware about what text will appear, but I know what text is currently there. So i'd like to wait for it to disappear in the element. Is there a way for this?

You can Try ExpectedConditions function textToBePresentInElement and add a not, like,
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = driver.findElement(By.id("foo"));
wait.until(ExpectedConditions.not(
ExpectedConditions.textToBePresentInElement(element,"textToBePresent")));

Related

How to write a code for an 'Alert popup' in a web application which does not require any accept or decline condition

How to write a code for an 'Alert popup' in a web application which does not require any accept or decline condition. Just to check the popup is displaying and it will automatically vanishes after 2 seconds.
Do you have any text content in the popup?if so try to get the text element as reference and decide the visibility of the toast/popup
you can wait until the alert is presented. you can write something like this:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
System.out.println("Alert text:"+alert.getText());

building xpath with #document tag in html

How can I build the xpath for Try it button from this website:
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert
driver.findElement(By.xpath("//div[#id='iframewrapper']/iframe/????")).click();
Also I tried this xpath //body[#contenteditable='false']/button which I know that isn't recommended. In Chrome console apear ok, but in Selenium doesn't find it and I don't know why.
Write this code, First you need to switch to Frame and then you need to click that button
driver.switchTo().frame("iframeResult")
driver.findElement(xpath: "//button[text()='Try it']").click()
In case if you want to use WebDriverWait :
WebDriverWait wait = new WebDriverWait(driver, 20)
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Try it') and #onclick='myFunction()']"))).click();

Check box is not checked using xpath, it opens link

Please see attached screen shots
using xpath "//div[#id='divTermsNdConditions']/div/div/div/label" it clicks on the label and open term and condition link
If use input id 'IsAgreeToNorms' then element is not visible/reachable.
What will be the solution to click on check box and checked?
You can try to click it with Explicit Wait,
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#data-val-required="Your String Value"]")));
You can use any attribute value if its unique,
//input[#data-val-required="Your attribute Value"]
//input[#name="Your attribute Value"]
You can also use, ExpectedConditions.elementToBeClickable

webdriver warning the server did not provide any stacktrace information

I have written driver.findElement(By.id("kfiDocumentLink")).click(); code for clicking on the button 'KFI Document'.
Please find the HTML code.
<a class="button" id="kfiDocumentLink" href="/Quote/KFIDocument/The%20Co-operative%20Bank%20-%20Download%20Mortgage%20Illustration%20(PDF)%20160808104103" target="_blank">Download Mortgage Illustration (PDF)</a>
When I run the code, sometimes I am able to click on the button and sometimes I am unable to click the button.
Could someone assist on this please?
Actually some time when you goes to find element, It would not be present on the DOM at that time due to slow internet or other reason, that's why are sometime able to click and sometime not.
To overcome this issue you should try using WebDriverWait with ExpectedConditions.elementToBeClickable to wait before click on element until element visible on the DOM and clickable as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
el = wait.until(ExpectedConditions.elementToBeClickable(By.id("kfiDocumentLink")));
el.click();

once selenium web driver clicks any button, then it wont click any other button further

I had a problem with the web driver while page get refresh
scenario:
click on the 1st button and the page got refresh. Once the page refresh, then the driver cant able to find the position of the second button. the second button is save function so that I cant able to save that page.
The error which I got while running :
unknown error: Element is not clickable at point (257, 898).
Other element would receive the click:
<div id="divModel" class="dialog" style="display: block; height: 2037px; width: 1450px;">...</div>
1) is there any solution to refresh the variables on the page without refreshing the whole page?
2) is there any solution to find out the next button position after clicking the first one on that page?
First of all, as we do not have any clear idea what happens with you. First thing checks that is your Xpath is changing after refreshing the page. another thing is may be your page need some wait.
Try below code:-
WebElement element= driver.findElement(By.xpath("YOUR XPATH OR ANY LOCATOR"));
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated((By) element));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
At last if nothing work then try to use Thread.sleep(5000); .
It's not a good practice even it is not recommended to use Thread.sleep but you can at least try once to ensure that the problem is related to wait only
It's not a solution, it's workaround: if you use Chrome driver then use FF/IE driver instead. I encountered similar problem with Chrome driver not so long ago, turned out to be Chrome driver bug.
Also, if you use PO model, you can use #CacheLookup annotation:
#CacheLookup
#FindBy(id = "your_id")
private WebElement saveButton;
Marker annotation to be applied to WebElements to indicate that it
never changes (that is, that the same instance in the DOM will always
be used)

Resources