How to use Alert box on button click in selenium by using web driver in java [duplicate] - selenium

This question already has answers here:
Alert handling in Selenium WebDriver (selenium 2) with Java
(5 answers)
Closed 6 years ago.
Please help me Tester, i am frustrated this point i have apply more and more code but no solution found again and same message show.
................................................................................
org.openqa.selenium.UnhandledAlertException: Unexpected modal dialog (text: Meal Added to Shopping List): Meal Added to Shopping List
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 16:57:40'
System info: host: 'Saurabh', ip: '192.168.3.21', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 87eb9958-85a9-4571-8bbd-663fdaad2ad0

You can try this:
WebDriverWait wait = new WebDriverWait(driver, 3);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();
Hope this helps!!

Related

Missing error message from Selenium after button click

I'm doing a simple form submit using the click method:
$this->url('/');
$move = $this->byName('move');
$move->click();
$content = $this->byTag('body')->text();
But I can't understand the error message I get from Selenium:
PHPUnit_Extensions_Selenium2TestCase_WebDriverException:
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:40.131Z'
System info: host: 'olle-GA-H81M-S2H', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-51-generic', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=/tmp/rust_mozprofile.EeZF7cCPXFo3, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=LINUX, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=56.0, platformVersion=4.4.0-51-generic, moz:processID=6598, browserName=firefox, javascriptEnabled=true, platformName=LINUX}]
Session ID: eae99c32-7401-4892-9190-e242b3b81f5a
I can var_dump the $move button and confirm that is no null. Any ideas?
The PHP backtrace:
vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase/Driver.php:165
vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase/Driver.php:175
vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase/CommandsHolder.php:100
tests/questions/DateTimeValidationTest.php:215
vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:371
vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/Selenium2TestCase.php:337
Edit: Above was the gecko driver. I get a similar error for the Facebook driver:
Facebook\WebDriver\Exception\UnrecognizedExceptionException:
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:40.131Z'
System info: host: 'olle-GA-H81M-S2H', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-51-generic', java.version: '1.8.0_141'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=/tmp/rust_mozprofile.TpTQ5MWyL1hl, rotatable=false, timeouts={implicit=0, pageLoad=300000, script=30000}, pageLoadStrategy=normal, moz:headless=false, platform=LINUX, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=56.0, platformVersion=4.4.0-51-generic, moz:processID=9146, browserName=firefox, javascriptEnabled=true, platformName=LINUX}]
Session ID: 56036593-dc86-4132-9d0f-78beb369b35c
vendor/facebook/webdriver/lib/Exception/WebDriverException.php:158
vendor/facebook/webdriver/lib/Remote/HttpCommandExecutor.php:320
vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:535
vendor/facebook/webdriver/lib/Remote/RemoteExecuteMethod.php:40
vendor/facebook/webdriver/lib/Remote/RemoteWebElement.php:81
tests/questions/FacebookDriverTest.php:42
The HTML for the button is:
<button type="submit" value="movesubmit" name="move" accesskey="l" class="ls-move-btn ls-move-submit-btn btn btn-lg btn-primary">
Submeter
</button>
Edit 2: Exception from selenium:
15:35:15.177 INFO - Executing: [find element: By.name: move])
15:35:15.193 INFO - Done: [find element: By.name: move]
1509374115699 addons.productaddons INFO downloadXHR File download. status=200
1509374115702 addons.productaddons INFO Downloaded file will be saved to /tmp/tmpaddon-e50bf4
1509374116634 DeferredSave.webext.sc.lz4 DEBUG Starting write
1509374116639 DeferredSave.webext.sc.lz4 DEBUG Write succeeded
15:35:17.196 INFO - Executing: [click: 0 [[FirefoxDriver: firefox on LINUX (32c1dfec-9af4-448f-86fa-3a41035bf6b5)] -> name: move]])
15:35:17.501 WARN - Exception thrown
org.openqa.selenium.ElementNotInteractableException:
Edit 3: Mystery solved, I had two buttons with the same name, and the first one was hidden. Hence, the exception. Solved by putting a proper ID on the element I want to interact with.

Selenium: How to click on href link when it wont have absolute path

I am trying to identify a link in the CQ5 webpage and click on it.
Below is the html code:
<a href="/siteadmin" title="Websites">
<div class="icon siteadmin">
<div class="title">Websites</div>
<div class="description">Create and manage multiple websites.
</div>
</div>
</a>
The below are method i tried to find the information and click on it.
driver.findElement(By.linkText("Websites")).click();
driver.findElement(By.xpath("//a[#href='/siteadmin']")).click();
Getting following exception:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (528, 174). Other element would receive the click: <div class="resourcebox">...</div>
(Session info: chrome=52.0.2743.116)
(Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 39 milliseconds
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46'
System info: host: 'pilapwin7020', ip: '10.116.252.187', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b), userDataDir=C:\Users\User\AppData\Local\Temp\scoped_dir44444_30971}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=52.0.2743.116, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 5116ba44126c39293ac466aed3d6608a
You should try using JavascriptExecutor to perform click in this case as :-
((JavascriptExecutor)driver).executeScript("arguments[0].click()", driver.findElement(By.cssSelector("a[href = '/siteadmin'][title = 'Websites']")));
You need to check if the attr contains the string, because it returns the fullpath even if in the HTML shows only /siteadmin.
XPath Solution:
//a[contains(#href, '/siteadmin') and #title='Websites']
Css Selector Solution:
a[href*='/siteadmin'][title='Websites']

protractor NoSuchElementError: no such element

When I was try to send keys in non-angular page with protractor it shows an error "NoSuchElementError: no such element"
NoSuchElementError: no such element
(Session info: chrome=47.0.2526.80)
(Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 24 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.45.0', revision: '5017cb8', time: '2015-02-26 23:59:50'
System info: host: 'Sekhar-PC', ip: '192.168.1.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_13'
* Element info: {Using=id, value=ius-userids}
Session ID: 352e36f285f80dee03eb5c88697ebc08
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Sekhar\AppData\Local\Temp\scoped_dir3128_18281}, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=47.0.2526.80, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, applicationCacheEnabled=false, takesScreenshot=true}]
**
When testing non-Angular pages with Protractor, you need to include this line in all of your specs:
browser.ignoreSynchronization = true;
You also need to wait for the page to completely load. Using ExpectedConditions is one way to do that. Example:
var EC = protractor.ExpectedConditions;
browser.wait(EC.elementToBeClickable(element(by.css('.wait-for-me'))), 10000);
Use sleep condition.
browser.driver.sleep(500);
This will wait the browser for sometime.
Looks like timing issue, write some "expect" statements so that it will verify for particular functionality is available.
Try to use implicit wait.
Try writing your script in promises so that it will wait.
field.sendKeys(value).then(function(){
submitButton.click().then(function(){
expect('xyz').toBe(xyz);
});
});

Selenium executeScript fails sometimes

i've created the following function to click elements
private WebDriverWait wait;
public void clickElement(By element) throws InterruptedException {
// wait element is clickable
wait.until(ExpectedConditions.presenceOfElementLocated(element));
// driver.findElement(element).click();
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(element));
waitForAjaxLoadScreenDisappear();
}
we click thousands of elements, but sometimes randomly an error occurs:
arguments[0] is undefined Command duration or timeout: 223 milliseconds Build info: version: '2.45.0', revision: '5017cb8e7ca8e37638dc3091b2440b90a1d8686f', time: '2015-02-27 09:10:26' System info: host: 'linux-1au3', ip: '192.168.1.194', os.name: 'Linux', os.arch: 'amd64', os.version: '3.1.10-1.23.1-desktop', java.version: '1.7.0_21' Session ID: 471aca55-ae19-439e-aaa2-b2ba970a1252 Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=20.0}]
How can arguments[0] be undefined, when the wait.until has finished?
usually buttons have enable/disable attributes, so along with asserting it's visible you also might want to check if they are clickable/enabled...
you should be able to do something like the below:
element.isEnabled()
Then you probably want to loop until it's true before interacting with the element...

WebDriverBackedSelenium in Internet Explorer is not working

An error can be seen while running the test in Selenium, I have tried removing and adding the capabilities but in vain:
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(capabilities);
WebDriverBackedSelenium selenium = new WebDriverBackedSelenium(driver, url);
Thread.sleep(3000);
selenium.open(url);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
selenium.type("userId", "TEst");
The error is thrown because of the line selenium.type("userId", "TEst") and the error is as follows:
JavaScript error (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 81 milliseconds
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'slc05apo', ip: '10.241.115.70', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_26'
Session ID: 4368aa6a-ba4a-42a9-8428-1ce9a8f71dcc
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=8, ie.usePerProcessProxy=false, cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=http://localhost:39326/, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
But no error can be seen if I use this:
WebDriver driver = new InternetExplorerDriver( DesiredCapabilities.internetExplorer());
driver.get(url);
driver.manage().timeouts().setScriptTimeout(30, TimeUnit.SECONDS);
driver.findElement(By.id("userId")).sendKeys("test");
But I want to use the first given case. Please help.
Whatever the capabilities you are setting, should be passed when instantiating the driver.
Please change this
WebDriver driver = new InternetExplorerDriver( DesiredCapabilities.internetExplorer());
to
WebDriver driver = new InternetExplorerDriver(capabilities);
Hope this works.