Having difficulty retrieving the alert text with selenium webdriver

I am currently facing an issue with verifying and dismissing alert text in my code. Despite following the steps to verify the alert text, I encountered failure.

WebDriverWait wait = new WebDriverWait(driver, 10);
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            alertText = alert.getText();

Unfortunately, the alertText is returning as null, causing the next step of asserting the text to fail. Next step:

driver.switchTo().alert().accept();

Surprisingly, accepting the alert proceeds without any problem.

In another peculiar scenario, when debugging in Eclipse, everything works fine. However, errors are thrown during running assertion tests.

A notable difference that I observed is the message displayed in the alert during running, stating: "don't let this page create more messages"

This situation seems quite unusual and puzzling.

Answer №1

When the message "don't let this page create more messages" is displayed, it's actually a built-in browser feature designed to prevent websites from bombarding users with multiple alerts.

If your code works during debugging but not during actual execution, it could be due to a synchronization issue. Adding a hard wait might help in such cases.

Another way to retrieve text from an alert popup is by using the following method:

public String getJsAlertText()
{
Object txt = ((JavascriptExecutor)driver).executeScript("return window.alert.myAlertText;");
return (String)txt;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the contrast between element.getAttribute() value and a String in protractor?

When using protractor and typescript, I need to verify that the text saved in a textbox matches a certain string by comparing it with the resulting value of element.getAttribute("value"). Unfortunately, getText() does not work for this scenario b ...

Generating a distinct username with Selenium for each script execution

I need assistance in generating unique usernames each time I create a new user. The current code I am using does not produce distinct usernames whenever the script is executed. Can someone provide guidance? int i = 0; for(int count1=0; count1 <10000 ...

Trouble displaying CSS content image in Internet Explorer

Usually, I use Chrome as my default browser. However, I recently decided to test whether my website functions properly on other browsers as well. In Chrome, the header displays my logo with great quality using content:url. But when I tried accessing my w ...

Using Python with Selenium to automate printing in Chrome

Season's Greetings and Happy Holidays! I have developed a script that automates the process of opening a webpage in Chrome, logging in, navigating to a specific page, and then attempting to print it. However, I am encountering difficulties when tryin ...

What is the best way to choose a specific passage from a lengthy piece of text?

Upon completing the registration process on the website, I am sent my login credentials via email in the following format: some text / login: [email protected] / password: example123 / some text I am struggling to extract and copy just the l ...

Determine the overall number of rows within a web table by utilizing XPath in Selenium with C#

I'm currently attempting to retrieve the total row count from a table using the code snippet below, however, it doesn't seem to be functioning as expected. public IWebElement DocTable => driver.FindElement(By.XPath("//*[@id='ctl00_Conten ...

Having trouble with clicking elements in IE 10 using Selenium?

I've been working on automating a website using Selenium in IE 10. The site loads successfully, but when I try to click on an element (button), it locates the element and clicks on it. However, the state of the element (such as the button name changin ...

How to read files string-by-string and perform operations in NodeJS using fs.readFileSync?

Apologies for the simple question, How can I read from a file in NodeJS, string by string, extract values such as URLs, and perform operations on each string? var contents = fs.readFileSync('test.txt', 'utf8'); What should I do next? ...

Capturing the shouldBe or shouldHave method in Selenide

I am in the process of creating a test, but I am encountering issues with loading all fields correctly. My initial thought was to use a simple try/catch method, however when I include the following code: $("select[formcontrolname=\"identifier\"] ...

Creating a landing page for a package

Currently, I am facing an issue with my "smoketests" which are straightforward selenium scripts consisting of 2-5 lines each and are executed using Xebium/Fitnesse. The problem arises when some tests need to modify data before running, causing the tests to ...

Experimenting with Selenium to automate processes involving dynamic class attributes

My issue involves a Button class = "searchbar__SearchButton-sc-1546roh-3 searchbar__CancelButton-sc-1546roh-4 glEceZ" I am attempting to locate this element in the browser using return browser.element('button[class^="searchbar__CancelButton-"]&ap ...

Creating a distinct file name for every execution in Log4jORGenerating a unique file name

I have an issue in my project with log4j where I am unable to create a new file each time I run the script. My requirement is to generate a file name in the format Filename_<DDMMYYYY_HHMMSS> every time the script runs. Despite trying different codes ...

The TouchAction class in the Appium-Python-client is now deprecated. What is the alternative way to execute press() and long_press() actions?

One of my methods allows for scrolling down on a menu, working well on both android and iOS devices due to it being a side menu. from appium.webdriver.common.touch_action import TouchAction def _scroll_down_menu_settings(self, distance=400): a ...

Optimizing the process of inputting the date, month, and year into separate dropdown menus

I'm new to automation and facing an issue with efficiently inputting date, month, and year from three different dropdowns with unique xpaths. I want to streamline this process without using the select class for each dropdown. Here is the sample code ...

Selenium encounters difficulty connecting to a docker container when executed with docker-compose run command

In my docker-compose.yml file, I have defined two services: a chrome-standalone container and a nodejs application. Here is the configuration: version: '3.7' networks: selenium: services: selenium: image: selenium/standalone-chrome-deb ...

Unable to access Firefox profile with Selenium WebDriver

Despite trying with both version 32 and the latest version of Firefox, Selenium is still not opening the specified Firefox Profile. Instead, it consistently opens a temporary profile. ProfilesIni profile = new ProfilesIni(); FirefoxProfile myprofile = ...

Having trouble importing Selenium files due to an error, unable to get them to work properly

After exhausting all my options, I have finally turned to Stack Overflow for help. from selenium import * from selenium import webdriver import time driver = webdriver.Firefox() driver.maximize_window() location = r"C:&bso ...

Slow loading times on websites while using Selenium with VBA

Recently, I have been utilizing Selenium VBA for some of my automation tasks. However, I have noticed that the webpages are taking significantly longer to load compared to when using the Chrome browser directly. Specifically, after successfully logging in ...

It requires approximately 25 minutes for the Selenium webdriver to locate and interact with a specific element on a

This is a project involving the Salesforce application using Selenium with Java. Scenario: The goal is to log in to Salesforce by entering username, password, and clicking on the login button. Once logged in, the next step is to click on the account tab o ...

How can you compare the values of two elements in Appium to confirm if they are equal?

After clicking on element1, it redirects me to a different page where I click on element2. When I return to element1, I expect the text to be updated to match that of element2. How can this be verified using Appium? I'm unsure how to verify this if e ...