To upload image on the field in this URL: http://demoqa.com/registration/
I used the following code that works on chrome but not on Firefox. May I please know the reason and solution?
WebDriver driver;
System.setProperty("webdriver.gecko.driver","C:\\Users\\abc\\Desktop\\Selenium\\geckodriver-v0.17.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("http://demoqa.com/registration/");
WebElement elementUpload=driver.findElement(By.xpath("//*[#id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:\\roboraid.jpg");
Error shown here is:
Exception in thread "main" org.openqa.selenium.InvalidArgumentException: File not found: C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg
try this code with xpath with Explicit wait:
WebDriver driver = new FirefoxDriver();
driver.get("http://demoqa.com/registration/");
driver.manage().window().maximize();
WebElement elementUpload=driver.findElement(By.xpath("//*[#id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:\\1504857398686.png");
Please find output image:
try replacing spaces in the file location
from
C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg
to
C:\Users\Public\Pictures\Sample_Pictures\Chrysanthemum.jpg
Hope this works
Change the xpath to the below one and try. It worked for me.
//input[#name='profile_pic_10' and #type='file']
May I know the browser and environment details which you have tried?
Actually it was the issue with geckodriver of firefox
refer :-
https://github.com/mozilla/geckodriver/issues/659
You need to update your geckodriver
OR
Use chromedriver as below
System.setProperty("webdriver.chrome.driver", "D:\\Workspace\\StackOverlow\\src\\lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver( );
driver.get("http://demoqa.com/registration/");
driver.manage().window().maximize();
((JavascriptExecutor) driver).executeScript("scroll(0,300)");
WebElement elementUpload=driver.findElement(By.xpath("//*[#id='profile_pic_10']"));
WebDriverWait wait=new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(elementUpload));
elementUpload.sendKeys("D:"+File.separator+"22.jpeg");
Related
I cant locate a button on dialog pages, I tried to use cssselectors, xpaths, but simple i cant locate buttons/texts on modal dialogs.
I attached a screenshot from the code.
What can you recommend?
Thank you!
By.xpath(".//button[.='/"Submit/"'])
or
By.xpath(".//button[#class='btn btn-default'])
If it found but click doesnt work try that javascript from other comment
I presume you are able to identify the element.However unable to click on that.
Try use following options.
Use WebDriverWait and elementToBeClickable to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
elementBtn.click();
Use Action class to click on the element.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement elementBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default")));
Actions action=new Actions(driver);
action.moveToElement(elementBtn).click().build().perform();
Java Script Executor to click on the element.
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default")));
Note: If above all options doesn't work.Check if there any iframe avaialable.If so, you need to switch to iframe first.like below.
driver.switchTo().frame("framename"); //name of the iframe.
driver.switchTo().frame(0); //you can use index as well.
You could try this:
JavascriptExecutor js= (JavascriptExecutor) driver;
WebElement webElement=driver.findElement(By.cssSelector("div.modal-footer button.btn.btn-default"));
js.executeScript(“arguments[0].click()”, webElement);
Hope it helps.
Try the bellow xpath:
driver.findElement(By.xpath("//div[#class='modal-footer']//button[contains(#class,'btn-default')]")).click();
I am learning how to automate tests with Selenium WebDriver, however I got stuck and cannot make dropdown menu to work in Firefox. The same code runs perfectly fine in Chrome.
The site I am practicing on is:
http://www.executeautomation.com/demosite/index.html
and I want to click the following item from menu: Automation Tools > Selenium > Selenium WebDriver.
The error message suggest that the web element may not be loaded on the screen yet, so I have implemented some method to wait with every execution until the element shows up:
public static void ImplicitWait(WebDriver driver){
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
but it did not helped.
Then I read that it is better to "pipe" those moveToElement() methods instead of performing them one by one. So I changed this:
action.moveToElement(menu).perform();
action.moveToElement(selenium).perform();
action.moveToElement(seleniumWebDriver).click().build().perform();
to one line. At this point it started to work on Chrome, but I am still struggling to make it work on Firefox.
The current code looks like this:
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver-v0.24.0-win64\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver();
ImplicitWait(driver);
driver.navigate().to("http://executeautomation.com/demosite/index.html");
WebElement menu = driver.findElement(By.id("Automation Tools"));
WebElement selenium = driver.findElement(By.id("Selenium"));
WebElement seleniumWebDriver = driver.findElement(By.id("Selenium WebDriver"));
Actions action = new Actions(driver);
action.moveToElement(menu).moveToElement(selenium).moveToElement(seleniumWebDriver).click().build().perform();
As I mentioned above the same works fine when I switch to Chrome, but with Firefox I get the error message:
Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-9862, 206) is out of bounds of viewport width (1283) and height (699)
I am using:
* Firefox v66.0.2
* Java v1.8.0_201
* Selenium Java v3.141.59
* GeckoDriver v0.24.0
Please help.
The main issue with the Web Application is that the HTML DOM attains document.readyState equals to complete even before the sub-menu element with text as Selenium WebDriver gets rendered. Hence you see the error as:
Exception in thread "main" org.openqa.selenium.interactions.MoveTargetOutOfBoundsException: (-4899, 91) is out of bounds of viewport width (1366) and height (664)
Solution
So an ideal solution would be:
Induce WebDriverwait for the titleIs() Execute Automation
Induce WebDriverwait for the menu element with text as Automation Tools
Induce WebDriverwait for the sub-menu element with text as Selenium
Induce WebDriverwait for the sub-menu elementToBeClickable with text as Selenium
You can use the following solution:
Code Block:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class MouseHoverFirefox {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.executeautomation.com/demosite/index.html");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleIs("Execute Automation"));
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#id='Automation Tools']")))).build().perform();
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='active has-sub']/a/span//following::ul[1]/li[#class='has-sub']/a/span[#id='Selenium']")))).build().perform();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#class='active has-sub']/a/span//following::ul[1]/li/a/span[#id='Selenium']//following::ul[1]/li/a/span[text()='Selenium WebDriver']"))).click();
}
}
Browser Snapshot:
Please try the below code(if you are inside frame , you need to come out and use below code):
WebDriver driver=new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,400)");
Use WebDriverWait and try the following code.
driver.get("http://executeautomation.com/demosite/index.html");
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement menu= wait.until(ExpectedConditions.elementToBeClickable(By.id("Automation Tools")));
Actions action = new Actions(driver);
action.moveToElement(menu).build().perform();
WebElement selenium =wait.until(ExpectedConditions.elementToBeClickable(By.id("Selenium")));
action.moveToElement(selenium).build().perform();
WebElement seleniumWebDriver =wait.until(ExpectedConditions.elementToBeClickable(By.id("Selenium WebDriver")));
action.moveToElement(seleniumWebDriver).click().build().perform();
Try using it -
action.moveToElement(menu).build().perform();
Thread.sleep(500);
moveToElement(selenium).build().perform();
Thread.sleep(500);
moveToElement(seleniumWebDriver).click().build().perform();
I've observed the same issue with geckodriver and Actions class. Although you can go with following code
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver-v0.24.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://executeautomation.com/demosite/index.html");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement mainmenu = driver.findElement(By.xpath("//li[#class='active has-sub']"));
WebElement submenu = driver.findElement(By.xpath("//li[#class='has-sub'] [contains(.,'Selenium')]"));
WebElement intendedLink = driver.findElement(By.xpath("//li[#class='has-sub'] [contains(.,'Selenium')]//li[contains(.,'Selenium WebDriver')]"));
Actions action =new Actions(driver);
action.moveToElement(mainmenu).clickAndHold().build().perform();
Thread.sleep(1000);
action.moveToElement(submenu).clickAndHold().build().perform();
Thread.sleep(1000);
intendedLink.click();
Code is working fine at my end. let me know if any issue.
Note: Keep the mouse pointer out of web page screen else it override the current focus.
I am trying to perform automation testing using Selenium with Java in Eclipse IDE.
I am finding xpath using the 'Inspect Element' option in chrome browser. However the same xpath is working fine in Firefox browser but NOT in chrome and IE. Can someone help me to solve this in chrome and IE? It throws me 'Element not visible' error in Chrome and IE.
Try this xpath and Add a wait before it e.g.
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='lenderName']")));
driver.findElement(By.xpath("//*[#id='lenderName']")).click();
You can try following options to find element :
driver.findElement(By.id("lenderName")).click();
driver.findElement(By.cssSelector(".lenderName")).click();
driver.findElement(By.xpath("//*[#id='lenderName']")).click();
If still not work then use Explicit wait with JavascriptExecutor :
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("lenderName")));
((JavascriptExecutor)BrowserUtilities.driver).executeScript("arguments[0].click()", element);
Test Scenario: Trying to capture and test Gmail Login.
Current Output: Mozilla instance opens up. Username is entered but
the Password is not being entered by the WebDriver code.
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
FirefoxDriver varDriver=new FirefoxDriver();
varDriver.get("http://gmail.com");
WebElement webElem= varDriver.findElement(By.id("identifierId"));
webElem.sendKeys("error59878#gmail.com");
WebElement nextButton=varDriver.findElement(By.id("identifierNext"));
nextButton.click();
varDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement wePass=varDriver.findElement(By.cssSelector(".rFrNMe.P7gl3b.sdJrJc.Tyc9J"));
wePass.sendKeys("test1");
ElementNotInteractableException
As per the documentation, ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the DOM Tree, it is not in a state that can be interacted with.
Reasons & Solutions :
The reason for ElementNotInteractableException to occur can be numerous.
Temporary Overlay of other WebElement over the WebElement of our interest:
In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as follows:
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();
A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();
Permanent Overlay of other WebElement over the WebElement of our interest :
If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
Now addressing the error ElementNotInteractableException in this particular context we need to add ExplicitWait i.e. WebDriverWait as follows :
You need to induce a bit of wait for the Password field to be properly rendered in the HTML DOM. You can consider configuring an ExplicitWait for it. Here is the working code to login into Gmail using Mozilla Firefox:
System.setProperty("webdriver.gecko.driver","C:\\Users\\Ruchi\\workspace2\\SeleniumTest\\jar\\geckodriver-v0.17.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement email_phone = driver.findElement(By.xpath("//input[#id='identifierId']"));
email_phone.sendKeys("error59878#gmail.com");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[#name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("test1");
driver.findElement(By.id("passwordNext")).click();
I am writing a script to locate "login" and click on it for an web based application but I am getting exception:
no such element: Unable to locate element
My code:
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.wayn.com"); //to find login
driver.findElement(By.xpath("//*[#id='TopMenu']/div[1]/div/div[2]/login-buttons/div/div[1]/div[1]"))
.sendKeys(Keys.ENTER);
Try this way.
driver.get("http://www2.wayn.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='button big left red ng-isolate-scope'][#wayn-log-click='loginButtonsLogClick']")).click();
Explanation of xpath: Use class and wayn-log-click attribute along with <div> tag.
Suggestion :- Instead of using absolute xpath, use relative xpath.
try waiting a little for element to be visible:
WebDriverWait wait =new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("username")));
driver.findElement(By.name("username")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");