I'm working on Safari Browser and i got a problem.
"Unknown command: {"id":"5qhlf8uni92m","name":"mouseMoveTo","parameters":{"yoffset":25,"xoffset":10}}
(WARNING: The server did not provide any stacktrace information)"
How can i deal with this ?
NOTE: In my scenario, f book shows a notification pop-up and i can't select any element because when pop-up showed up, black screen appeared and i have to click anywhere to enable elements. That's why i used this code;
WebElement knownElement = null;
Actions builder = new Actions(driver);
builder.moveToElement(knownElement, 10, 25).click().build().perform();
In my opinion, it cause this problem. How can i change this code to fit in Safari ?
Please Refer this link : https://ynot408.wordpress.com/2011/09/22/drag-and-drop-using-selenium-webdriver/
OR :
public boolean onMouseOver(WebElement element){
boolean result = false;
try{
String mouseOverScript = "if(document.createEvent){
var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover',
true, false); arguments[0].dispatchEvent(evObj);
} else if(document.createEventObject){
arguments[0].fireEvent('onmouseover');}";
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(mouseOverScript, element);
result = true;
}catch (Exception e){
e.printStackTrace();
result = false;
}
return result;
}
Related
I have trying trouble with trying to run Selenium in headless mode. I have using a find element for an out of stock button, when I run normal with the headless option commented out, I get expected behavior. When I run in headless, I get the wrong result. This is on the new Selenium beta for Java. (4.0) I'm using the beta because the old version does not have options to run Edge headless.
I have tried setting a duration, just takes longer to get the same wrong answer. And I have tried enabling the window size to full screen. No dice.
public class BestBuy extends Thread
{
public static void main(String[] args)
{
System.setProperty("webdriver.edge.driver", "Driver file path");
final EdgeOptions edgeOptions = new EdgeOptions();
//edgeOptions.addArguments("--headless");
WebDriver BestBuyDriver = new EdgeDriver(edgeOptions);
BestBuyDriver.get("https://www.bestbuy.com/site/dyson-airwrap-complete-styler-for-multiple-hair-types-and-styles-fuchsia-nickel/6284230.p?skuId=6284230");
try
{
List<WebElement> buttonText = BestBuyDriver.findElements(By.xpath("//button[text()='Sold Out']"));
List<WebElement> text = BestBuyDriver.findElements(By.xpath("//*[contains(text(),'Sold Out')]"));
//BestBuyDriver.findElement(By.className("btn-disabled"));
if(buttonText.size() > 0 || text.size() > 0)
System.out.println("Item out of stock");
else
System.out.println("Item in Stock");
}
catch (Exception e)
{
System.out.println(e);
e.printStackTrace();
System.out.println("Exception");
}
}
}
I am trying to write a Selenium test against Amazon site. I want to get "Sign in" element so that I can click on it.
url: www.amazon.es
Here is my Selenium Code:
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");
try
{
driver.findElement(By.id("nav-link-accountList")).click();
}
catch (Exception e)
{
System.out.println("Not Found");
}
Sometimes the code works correctly but sometimes it does not find the ID "nav-link-yourAccount". What is the problem? and how can I solve it?
Provide few seconds of wait, before click to this webelement so your driver may able to find the webelement.
For wait i am using Explicit wait method.
WebDriverWait wait = new WebDriverWait(driver,10);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("nav-link-accountList"))));
driver.findElement(By.id("nav-link-accountList")).click();
The element which you are trying to click with id as nav-link-yourAccount is not clickable. To proceed further you need to click either the link with text Hola. Identifícate or the link with text Mi cuenta using one of the following xpaths:
//a[#id='nav-link-yourAccount']/span[text()='Hola. Identifícate']
or
//a[#id="nav-link-yourAccount"]/span[contains(text(),'Mi cuenta')]
Instead using implicit wait try using explicit wait for the login element.
I've tried with explicit wait over 50 click and it did works.
Here is code you can use.
public class dump {
public static void main(String a[]){
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 15);
for(int i=0; i<=50; i++){
driver.get("https://www.amazon.es");
try{
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='nav-link-accountList']")));
driver.findElement(By.xpath("//*[#id='nav-link-accountList']")).click();
System.out.println("clicked\t"+i);
}catch (Exception e){
e.printStackTrace();
System.out.println("Not Found");
}
}
}
}
Here is the proof of run:
All the best!!
Apply wait until element is appeared, so that it avoids NoSuchElementException and code is working without any error.
Below code is working fine:
driver.get("https://www.amazon.es");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement accontButton=driver.findElement(By.id("nav-link-accountList"));
WebDriverWait waitforelement=new WebDriverWait(driver,20);
waitforelement.until(ExpectedConditions.elementToBeClickable(accontButton));
try{
accontButton.click();
}
catch (Exception e){
System.out.println("Not Found");
}
Have you tried to find elements by xpath?
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.es");
try
{
driver.findElement(By.xpath("//*[#id='nav-link-accountList']")).click();
}catch (Exception e)
{
System.out.println("Not Found");
}
I have the below code which will click on a button in window. On clicking the button,the current window is closed and new window will be opened. Some text will be inputted in a textbox in new window.
WebElement element=null;
try {
driver.getWindowHandles();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
try {
element = driver.findElement(By.xpath("//*[#id='username']"));
} catch (Exception e) {
continue;
}
if (element.isDisplayed()) {
windowFound = 1;
break;
}
}
}
element.sendKeys("Testingusername");
Last line to input send keys is not failing. But the actual text is not entered into the textbox.
This works well in chrome. Issue is with Internet explorer only.
Selenium : 2.53.1
IE 11
Try to focus on the element let say
element.Clear();
element.sendKeys("testingUserName");
and put this code to try catch to see if you get any exceptions
Few things :
verify if you've located the correct element in IE as it sometimes XPath behavior is different in IE.
try to confirm the attributes of the element under question with the attributes observed in other browsers.
try using IE Driver 32 bit version for IE11 browser.
if nothing works then there is no harm in using javascript sendKeys. it's not a bad practise
Actions a = new Actions(driver);
a.SendKeys(element, "Your text to input").Build().Perform();
Note: Works in IE11
try this one This works for me
WebElement element=null;
try {
driver.getWindowHandles();
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
try {
element = driver.findElement(By.xpath("//*[#id='username']"));
} catch (Exception e) {
continue;
}
if (element.isDisplayed()) {
windowFound = 1;
break;
}
}
}
element.click();
String text = "your text that you want to enter";
StringSelection stringSelection = new StringSelection(text);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
I think it's all about timing.
You should consider adding Thread.Sleep(3000); to your code:
Thread.Sleep(3000);
element.sendKeys("Testingusername");
Explicit wait is not working if the element located is of 'text'. But it is working fine if the driver performs some action i.e, entering text into text box or clicking a webelement etc.
public boolean waitForPageToLoad(String timeOutInSeconds) throws ScreenShotException, InterruptedException {
boolean bFlag = false;
WebElement element;
boolean bStatus = true;
int timeinseconds1 = Integer.parseInt(timeOutInSeconds);
try {
WebDriverWait wait = new WebDriverWait(webDriver, timeinseconds1);
while(timeinseconds1 > 0) {
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myDynamicElement")));
Log.info("Element status at runtime -->"+element.isDisplayed());
if(!element.isDisplayed()) {
timeinseconds1 = timeinseconds1 - 500;
Thread.sleep(1000);
}
else {
System.out.println("working");
bFlag = bStatus;
Log.info("Element status: - >"+bFlag);
break;
}
}
}
catch (Exception e) {
screenShot.screenShot(e);
}
return bFlag;
}
The above code doesnt work if my locator is of text i.e, say if I want to check whether 'Title' of the question in the stackoverflow is visible or not within 40seconds.Driver will wait for 40seconds though the title appears less than that. But, it works fine if the locator is Title text box. Please let me know how to resolve this.
You are using the wait quite differently that it is supposed to be used.
try {
WebDriverWait wait = new WebDriverWait(webDriver, timeinseconds1);
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("myDynamicElement")));
} catch (TimeOutException toe) {
//handle the page not loading
}
//from now on continue the code as synchronous knowing that the page is loaded
Webdriver launches multiple windows after performing click action. I have tried driver.close() but it close the webdriver and test fails.
WebDriver driver = new FirefoxDriver ();
driver.get("http://www.xyz.com/");
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement page = driver.findElement(By.className("coupon-rows"));
List <WebElement> coupontrigger = page.findElements(By.className("code"));
System.out.println("number of couponsTriggers on carousel = "+ "coupontrigger.size());
for (int j=0; j<=coupontrigger.size(); j++) {
js.executeScript("$('.ccode.coupon-trigger').eq("+j+").click()");
System.out.println(driver.getTitle());
driver.switchTo().defaultContent();
driver.get("http://www.xyz.com/");
page = driver.findElement(By.className("coupon-rows"));
coupontrigger = page.findElements(By.className("code"));
}
}
If I understood your requirement you want to close the other popups rather than the main window. In that case you can do below. Though I am not 100% sure of your requirement.
String mwh=driver.getWindowHandle(); // Get current window handle
Set<String> s=driver.getWindowHandles();
Iterator<String> ite=s.iterator();
String popupHandle = "";
while(ite.hasNext())
{
popupHandle = ite.next().toString();
if(!popupHandle.contains(mwh)) // If not the current window then shift focus and close them
{
driver.switchTo().window(popupHandle);
driver.close();
}
}
driver.switchTo().window(mwh); // finally move control to main window.
You can introduce a helper method which will do that task for you. You just need to find out what your current view is (WebDriver#getWindowHandle will give the one you have focus on) and then just close the rest of the windows.
private String closeAllpreviouslyOpenedWindows() {
String firstWindow = webDriver.getWindowHandle();
Set<String> windows = webDriver.getWindowHandles();
windows.remove(firstWindow);
for (String i : windows) {
webDriver.switchTo().window(i);
webDriver.close();
}
webDriver.switchTo().window(firstWindow);
return firstWindow;
}