Selenium unable to click on the webelement element ( file upload button) - selenium-webdriver

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://stackoverflow.com/");
driver.findElement(By.linkText("log in")).click();
driver.findElement(By.cssSelector("a.google.openid_large_btn")).click();
driver.findElement(By.id("Email")).sendKeys("username#gmail.com");
driver.findElement(By.id("Passwd")).sendKeys("password");
driver.findElement(By.id("hlinks-user")).click();
driver.findElement(By.linkText("user2131252")).click();
Thread.sleep(3000L);
// None of these work to click on the upload button , actions, robot..
//driver.findElement(By.xpath(".//*[#id='change-picture']")).click();
//Actions actions = new Actions(driver);
//WebElement changepic = driver.findElement(By.xpath(".//*[#id='change-picture']"));
//actions.moveToElement(changepic).click().perform();
//actions.click();
//actions.perform();
/*Point coordinates = driver.findElement(By.xpath(".//*[#id='change-picture']")).getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
//robot.mousePress(coordinates.getX());
driver.findElement(By.xpath(".//*[#id='change-picture']")).clear();
*/
Actions actions = new Actions(driver);
//for hovering over the username field
WebElement menuHoverLink = driver.findElement(By.xpath(".//*[#id='change- picture']"));
actions.moveToElement(menuHoverLink).perform();
//for clicking the logout link
WebElement logoutLink = driver.findElement(By.xpath(".//*[#id='change-picture']"));
logoutLink.click();
Please I am unable to click on the change-picture link, I get different errors on different browsers.
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: Element cannot be scrolled into view: (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 13 milliseconds
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_17'
Session ID: 4534c06c-0622-4610-8d7d-1cf99f9577ee
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, browserConnectionEnabled=true, nativeEvents=true, webStorageEnabled=true, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=24.0}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at org.openqa.selenium.remote.RemoteExecuteMethod.execute(RemoteExecuteMethod.java:34)
at org.openqa.selenium.remote.RemoteMouse.mouseMove(RemoteMouse.java:88)
at org.openqa.selenium.interactions.MoveMouseAction.perform(MoveMouseAction.java:36)
at org.openqa.selenium.interactions.CompositeAction.perform(CompositeAction.java:31)
at org.openqa.selenium.interactions.Actions.perform(Actions.java:356)
at test.stackoverflow.main(stackoverflow.java:52)

you can try using javascriptExcecuter after sleep(),
((JavascriptExecutor)driver).executeScript("$('#change-picture').click();");

Related

Mauze over functionality is not working

public class AdminUser {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","geckodriver");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.carmax.com/");
Actions builder= new Actions(driver);
WebElement d= driver.findElement(By.xpath("html/body/div[1]/header/div/nav[2]/ul/li[1]/a"));
builder.moveToElement(d).build().perform();
driver.findElement(By.linkText("Why CarMax?")).click();
}
}
I am getting o/p as:
1490543196098 geckodriver INFO Listening on 127.0.0.1:7086
1490543196344 mozprofile::profile INFO Using profile path /var/folders/dg/glyvppvs1dn31sqrrgt7yhjc0000gn/T/rust_mozprofile.l2gu30iSNpnT
1490543196345 geckodriver::marionette INFO Starting browser /Applications/Firefox.app/Contents/MacOS/firefox-bin with args []
1490543196348 geckodriver::marionette INFO Connecting to Marionette on localhost:59046
1490543197117 Marionette INFO Listening on port 59046
Mar 26, 2017 9:16:38 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
2017-03-26 21:16:39.067 plugin-container[2943:127691] * CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x9337, name = 'com.apple.tsm.portname'
See /usr/include/servers/bootstrap_defs.h for the error codes.
2017-03-26 21:16:39.072 plugin-container[2943:127691] * CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x933b, name = 'com.apple.CFPasteboardClient'
See /usr/include/servers/bootstrap_defs.h for the error codes.
JavaScript warning: https://s-cdn-tag.medialytics.com/cp, line 19: Error: WebGL: getParameter: parameter: invalid enum value
JavaScript warning: https://s-cdn-tag.medialytics.com/cp, line 19: Error: WebGL: getParameter: parameter: invalid enum value
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException:
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: 'Javeds-MacBook-Pro.local', ip: 'fd50:1d9:9d9b:eb00:14:5aa0:4eca:be9d', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.3', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{moz:profile=/var/folders/dg/glyvppvs1dn31sqrrgt7yhjc0000gn/T/rust_mozprofile.l2gu30iSNpnT, rotatable=false, timeouts={implicit=0, page load=300000, script=30000}, pageLoadStrategy=normal, platform=ANY, specificationLevel=0, moz:accessibilityChecks=false, acceptInsecureCerts=false, browserVersion=52.0.1, platformVersion=16.4.0, moz:processID=2941, browserName=firefox, platformName=darwin}]
Session ID: ae3c536b-197f-8941-91b4-f630e7a4dab5
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:133)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:99)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:43)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:604)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:274)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
at AdminUser.main(AdminUser.java:24)

Selenium Web driver

Hi i'm getting error while running Selenium web driver , please help to resolve
Exception in thread "main" org.openqa.selenium.WebDriverException:
Element must be user-editable in order to clear it. Command duration
or timeout: 103 milliseconds Build info: version: '2.53.0', revision:
'35ae25b', time: '2016-03-15 17:00:58' System info: host: 'anil-PC',
ip: '192.168.0.131', os.name: 'Windows Vista', os.arch: 'x86',
os.version: '6.0', java.version: '1.7.0-ea' Session ID:
0736ad53-a638-44e4-a24a-bcda63bb6f4e Driver info:
org.openqa.selenium.firefox.FirefoxDriver Capabilities
[{platform=WINDOWS, acceptSslCerts=true, javascriptEnabled=true,
browserName=firefox, rotatable=false, locationContextEnabled=true,
version=43.0.1, cssSelectorsEnabled=true, databaseEnabled=true,
handlesAlerts=true, nativeEvents=false, webStorageEnabled=true,
applicationCacheEnabled=true, takesScreenshot=true}] at
sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:531)
at
org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at
org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at
org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:140)
at demo.TestFB.main(TestFB.java:26) Caused by:
org.openqa.selenium.WebDriverException: Element must be user-editable
in order to clear it. Build info: version: '2.53.0', revision:
'35ae25b', time: '2016-03-15 17:00:58' System info: host: 'anil-PC',
ip: '192.168.0.131', os.name: 'Windows Vista', os.arch: 'x86',
os.version: '6.0', java.version: '1.7.0-ea' Driver info:
driver.version: unknown at .bot.Error(file:///C:/Users/anil/AppData/Local/Temp/anonymous9127022654718385403webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:681)
at .bot.action.clear(file:///C:/Users/anil/AppData/Local/Temp/anonymous9127022654718385403webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:11685)
at .WebElement.clearElement(file:///C:/Users/anil/AppData/Local/Temp/anonymous9127022654718385403webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12136)
at .DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/anil/AppData/Local/Temp/anonymous9127022654718385403webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at .DelayedCommand.prototype.executeInternal_(file:///C:/Users/anil/AppData/Local/Temp/anonymous9127022654718385403webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at .DelayedCommand.prototype.execute/<(file:///C:/Users/anil/AppData/Local/Temp/anonymous9127022654718385403webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
You are calling clear() on an element that is not user editable, so an element that is not an input or textarea.
Element must be user-editable in order to clear it.
From the selenium java docs about clear()
/**
* If this element is a text entry element, this will clear the value. Has no effect on other
* elements. Text entry elements are INPUT and TEXTAREA elements.
*
* Note that the events fired by this event may not be as you'd expect. In particular, we don't
* fire any keyboard or mouse events. If you want to ensure keyboard events are fired, consider
* using something like {#link #sendKeys(CharSequence...)} with the backspace key. To ensure
* you get a change event, consider following with a call to {#link #sendKeys(CharSequence...)}
* with the tab key.
*/

Context menu isn't opened using Safari WebDriver

Unable to open context menu in Safari browser using SafariDriver Selenium. Getting following exceptions:
org.openqa.selenium.WebDriverException: Unknown command:
{"id":"az1mvyq9x4ly","name":"mouseMoveTo","parameters":
{"element":":wdc:1463726481487"}} (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 5 milliseconds
Build info: version: '2.52.0', revision: '4c2593c', time: '2016-02-11 19:06:42'
System info: host: 'RVSKCH19LT', ip: '192.168.10.30', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_11'
Driver info: org.openqa.selenium.safari.SafariDriver
Capabilities [{browserName=safari, takesScreenshot=true, javascriptEnabled=true, version=5.1.7, cssSelectorsEnabled=true, platform=WINDOWS, secureSsl=true}]
Session ID: null
try this code to resolve your issue.
WebElement obj = driver.findelement(by.xpath(""));
Action act = new Action(driver);
act.contextClick(obj).perform();
share your code review.
Actions/Interactions API is not implemented with SafariDriver yet.
Please try with the following JavaScript solution:
String script = "var element = document.querySelector('SOME_CSS_SELECTOR');var e = element.ownerDocument.createEvent(\"MouseEvents\"); e.initMouseEvent(\"contextmenu\", true, true, element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false,2, null); element.dispatchEvent(e);";
driver.executeScript(script);
Replace SOME_CSS_SELECTOR with the css selector of the element where you want the right click.

ElementNotVisibleError: Cannot click on element-error in Protractor IE browser

When I try to execute my protractor scripts in IE_11 browser I am getting following error.However same script working fine in chrome and Firefox browsers.Saying element is not visible.However when I execute the script manually by pausing the browser,respective element is visible.
ElementNotVisibleError: Cannot click on element (WARNING: The server did no
t provide any stacktrace information)
Command duration or timeout: 141 milliseconds
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 03:03:16'
System info: host: 'XXXXXX', ip: '10.XXX.CCC.33', os.name: 'Windows 7', os.arc
h: 'amd64', os.version: '6.1', java.version: '1.8.0_51'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreat
eProcessApi=false, pageLoadStrategy=normal, ie.usePerProcessProxy=false, ignoreZ
oomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents
=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLi
neSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBro
wserUrl=http://localhost:3653/, takesScreenshot=true, javascriptEnabled=true, ig
noreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEna
bled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 91db8197-e87b-4564-b1b4-9dd7389e19c6
It sounds like you are having timing issues. You might try using Expected Conditions functionality (http://www.protractortest.org/#/api?view=ExpectedConditions). It even looks like their example fits what you need:
var EC = protractor.ExpectedConditions;
var button = $('#xyz');
var isClickable = EC.elementToBeClickable(button);
browser.get(URL);
browser.wait(isClickable, 5000); //wait for an element to become clickable
button.click();

" StaleElementReferenceException: Element is no longer attached to the DOM " issue while running script. (Firefox browser)

My requirement is
Get all data from grid
Search specific text, if found
Click check box
My code is,
List<WebElement> totalRows = driver.findElements(By.xpath("//*[#id='ContentPlaceHolder1_ContentPlaceHolderChild_grdGroupSelection']/tbody/tr"));
System.out.println("Total Size: "+totalRows.size());
for (int count=1;count<totalRows.size()-1;count++){
System.out.println("Loop count: "+count);
WebElement rowElement = totalRows.get(count);
System.out.println("WebElement Tag name: "+rowElement.getTagName());
}
Unable to get the value from rowElement.
Here posted Logs, HTML source & snapshot for reference. Please help me out. Thanks in advance.
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
Command duration or timeout: 294 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:33'
System info: host: 'Saravanan-PC', ip: '192.168.101.22', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Session ID: 6ea5596b-9f0f-477b-9a11-dc534a0197ee
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=WINDOWS, databaseEnabled=true, cssSelectorsEnabled=true, javascriptEnabled=true, acceptSslCerts=true, handlesAlerts=true, browserName=firefox, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=35.0.1}]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:268)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:187)
at org.openqa.selenium.remote.RemoteWebElement.findElementsByTagName(RemoteWebElement.java:264)
at org.openqa.selenium.By$ByTagName.findElements(By.java:323)
at org.openqa.selenium.remote.RemoteWebElement.findElements(RemoteWebElement.java:163)
at com.aetherpal.scripts.ClientPolicyProfiles.selectCreatedCSPGroupID(ClientPolicyProfiles.java:95)
at com.aetherpal.scripts.ClientPolicyProfiles.main(ClientPolicyProfiles.java:80)
Caused by: org.openqa.selenium.StaleElementReferenceException: Element is no longer attached to the DOM
For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html
Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:33'
System info: host: 'Saravanan-PC', ip: '192.168.101.22', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_51'
Driver info: driver.version: unknown
at .fxdriver.cache.getElementAt(resource://fxdriver/modules/web-element-cache.js:8325:24)
at .Utils.getElementAt(file:///C:/Users/SARAVA~1/AppData/Local/Temp/anonymous631926879527649788webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:7922:10)
at .FirefoxDriver.prototype.findElementsInternal_(file:///C:/Users/SARAVA~1/AppData/Local/Temp/anonymous631926879527649788webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:9636:32)
at .FirefoxDriver.prototype.findChildElements(file:///C:/Users/SARAVA~1/AppData/Local/Temp/anonymous631926879527649788webdriver-profile/extensions/fxdriver#googlecode.com/components/driver-component.js:9657:3)
at .DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/SARAVA~1/AppData/Local/Temp/anonymous631926879527649788webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:11612:16)
at .DelayedCommand.prototype.executeInternal_(file:///C:/Users/SARAVA~1/AppData/Local/Temp/anonymous631926879527649788webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:11617:7)
at .DelayedCommand.prototype.execute/<(file:///C:/Users/SARAVA~1/AppData/Local/Temp/anonymous631926879527649788webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:11559:5)
Grid:
HTML Source:
The checkbox, ID and the GroupName will be inside the tr.
Checkbox is the only input element inside the tr. You can use the following code which will wait for the checkbox of particular GroupName(in your case ACL all) to appear and click on it as soon as it is visible.
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(20));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//table[#id='ContentPlaceHolder1_ContentPlaceHolderChild_grdGroupSelection']//td[text()='ACL all']/..//input")));
element.Click();
The webpage might keep changing due to various reasons ie Reloading/Etc which are not very visible ,
Approach 1 :
One approach to this stale element exception is Trying to reduce the time between 'Find element' and 'Do some action on element'
Approach 2 :
Have a Repeated while loop to check the availablity of element until some condition is true , Also make sure to use Implicit/Explicit/Fluent waits , it helps a lot but never do a thread.sleep .
Finally I tried with JavascriptExecutor.executeScript() method. Its working fine...I don't know whether this approach is correct way or not..
my sample code is,
JavascriptExecutor scriptExecute = (JavascriptExecutor)driver;
WebElement el = (WebElement) scriptExecute.executeScript("var noOfRows =document.querySelectorAll('table[id=ContentPlaceHolder1_ContentPlaceHolderChild_grdGroupSelection]>tbody>tr'); " +
"for(var i=1;i<noOfRows.length-1;i++){" +
"return noOfRows[i]; }");
el.click();

Resources