StaleElementReferenceException Error on Java- selenium - java

I am getting StaleElementReferenceException when I run my code for selecting "Buy NOW" from flipkart.com. This is what I have, but its not working for me.
public void SelectItemfromPage(){
WebDriver wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
wd.get("http://www.flipkart.com");
WebElement element = wd.findElement(By.xpath(".//*[#id='fk-top-search-box']"));
element.sendKeys("moto g");
element.submit();
element.findElement(By.xpath(".//*[#id='products']/div/div[1]/div[1]/div/div[1]/a[1]/img")).click();
element.findElement(By.xpath(".//*[#id='fk-mainbody-id']/div/div[7]/div/div[3]/div/div/div[6]/div/div[3]/div[1]/div/div[2]/div/div[2]/form/input[9]")).click();
}

Your approach is all wrong.
You are saving an WebElement and reusing it, that's not the way to go.
When you save a WebElement in an object, in this case element, the WebElement will become stale whenever the DOM changes.
What you need to do is the following:
WebDriver wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
wd.get("http://www.flipkart.com");
WebElement element = wd.findElement(By.xpath(".//*[#id='fk-top-search-box']"));
element.sendKeys("moto g");
element.submit();
wd.findElement(By.xpath(".//*[#id='products']/div/div[1]/div[1]/div/div[1]/a[1]/img")).click();
wd.findElement(By.xpath(".//*[#id='fk-mainbody-id']/div/div[7]/div/div[3]/div/div/div[6]/div/div[3]/div[1]/div/div[2]/div/div[2]/form/input[9]")).click();

Related

I can't click on an element

the page is fully loaded and the element is located, but for some reason it can't be clicked, I can't understand why.
My test:
#Test
public void test(){
chromeDriver.get("https://alfabank.ru/currency/");
WebDriverWait wait = new WebDriverWait(chromeDriver, 8);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = chromeDriver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
clicking.click();
}
Full xpatch it doesn't work either:
WebElement clicking = chromeDriver.findElement(By.xpath("/html/body/div[1]/div/div[6]/div/div/div/div/div[1]/div[1]/button[2]"));
Exception:
org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element is not clickable at point (539, 1199)
None of the existing answer really explain what is their code is about.
Even if you launch the screen in full screen, USD is not in Selenium view port.
Also JS is only recommended when nothing works.
I am doing with Selenium class, actions, please see below :
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("https://alfabank.ru/currency/");
boolean b = wait.until(ExpectedConditions.titleIs("Курсы валют — «Альфа-Банк»"));
if (b) {
new Actions(driver).moveToElement(wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div[data-test-id='caption']")))).build().perform();
WebElement clicking = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clicking);
}
I use JS click, don't know why cannot click normally.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement clicking = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true); arguments[0].click()", clicking);
Apply scrollIntoView and then click on the USD using JavascriptExecutor.
JavascriptExecutor js = (JavascriptExecutor) driver;
WebDriverWait wait = new WebDriverWait(driver,30);
driver.get("https://alfabank.ru/currency/");
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]")));
WebElement usdoption = driver.findElement(By.xpath("//button[contains(#class, 'j1I7k')]//p[contains(text(), 'USD')]"));
js.executeScript("arguments[0].scrollIntoView(true);", usdoption);
js.executeScript("arguments[0].click();", usdoption);

Not able to click dropdown

I am a new learner in selenium and try to click on a dropdown to populate list but it keeps on giving me runtime exception:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException:
Element is not currently visible and so may not be interacted with )
Please help. Below is my code that I am executing.
WebDriver dr=new FirefoxDriver();
dr.get("https://jqueryui.com/selectmenu/");
dr.manage().window().maximize();
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
dr.switchTo().frame(dr.findElement(By.className("demo-frame")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("speed- menu"))).click();
To click on the Dropdown Select a speed, here is your own code with minimal changes:
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
WebDriver dr=new FirefoxDriver();
dr.get("https://jqueryui.com/selectmenu/");
dr.manage().window().maximize();
dr.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
dr.switchTo().frame(dr.findElement(By.className("demo-frame")));
dr.findElement(By.xpath("//*[#id='speed-button']/span[#class='ui-selectmenu-text']")).click();
Let me know if this Answers your Question.
Try This
WebDriver driver = new FirefoxDriver();
driver.get("https://jqueryui.com/selectmenu/");
WebElement DynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("DynamicElement")));
OR
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("yourid")));

Unable to perform Drag and Drop in Selenium Web-Driver using Java

Below is the code . Drag and Drop is not working. Please let me know what is wrong in my code.
WebDriver driver = new FirefoxDriver();
String URL ="http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop";
driver.get(URL);
driver.manage().window().maximize();
WebDriverWait frame_Test = new WebDriverWait(driver, 10);
frame_Test.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframeResult")));
WebElement From = driver.findElement(By.xpath("//img[#id='drag1']"));
WebElement To =driver.findElement(By.xpath("//div[#id='div1']"));
Actions builder = new Actions(driver);
builder.clickAndHold(From).moveToElement(To).release(From).build( ).perform();
what error are you getting? Have you tried below ?
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();

Selenium webdriver :org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions

I am getting this error while trying to write to simple code in selenium webdriver to enter a value in google search page and enter.
Following is my code -:
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement element=driver.findElement(By.xpath("//input[#id='gs_htif0']"));
boolean b = element.isEnabled();
if (b){
System.out.println("Enabled");
}
element.sendKeys("Test Automation");
element.submit();
Can anyone please help me out with this? How to enable a disabled element?
You are using the wrong 'input' for entering the text. You should be using the following XPath:
//input[#name='q']
Like
WebElement element=driver.findElement(By.xpath("//input[#name='q']"));
This 'input' element accepts the input text just fine.
You can try to run javascript on page:
((JavascriptExecutor) driver).executeScript("document.getElementById('gs_htif0').disabled = false");
or
((JavascriptExecutor) driver).executeScript("arguments[0].disabled = false", element);
See, if this might help,
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
if(element.isEnabled()) {
System.out.println("Enabled");
element.sendKeys("Test Automation");
element.submit();
}
Try this:
WebDriverWait wait = new WebDriverWait(driver, 40);
driver.get("http://www.google.com");
wait.until(ExpectedConditions.visibilityOfElementLocated(By
.xpath("//input[#id='gs_htif0']")));
driver.findElement(By.xpath("//input[#id='gs_htif0']"))
.sendKeys("Test Automation" + Keys.ENTER);
Or:
public boolean isElementPresent(WebDriver driver, By by)
{
try {
driver.findElement(by);
System.out.print("Enabled");
return true;
} catch (NoSuchElementException ignored) {
return false;
}
}
isElementPresent = isElementPresent(
driver, By.xpath("//input[#id='gs_htif0']"));
if (isElementPresent) {
element.sendKeys("Test Automation");
element.submit();
}
Or change xPath to name selector.
If i am correct then you are using the firebug add-on in the firefox driver to get the path for the searchbox. But the firebug seems to provide a path where the Id for the searchbox is not correct. If you use the inspect element option you can see the difference (in the below image you can spot the difference yourself).

WebDriverWait is not working

I am using Windows 8, IE 10 (java - WebDriver 2.37.0) and I am trying to wait until the element is loaded on the page. I used following code:
WebDriver driver = new FirefoxDriver();
driver.get("http://abc.com");
WebElement myDynamicElement = (
new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
But it's throwing a timeout exception. If I remove this code, it's able to identify the element on the webdriver.
I tried the same code in other browsers as FireFox, Chrome but its still throwing error.
Any help is appreciated.
Thanks
You're assigning that wait to the variable myDynamicElement. If you don't give the WebElement variable something to do, Selenium will throw that timeout exception. If you just want to wait for the element to be present then there is no need to assign it to a WebElement variable.
WebDriver driver = new FirefoxDriver();
driver.get("http://abc.com");
new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
If you need to assign that variable for later use then do something with the element.
WebElement myDynamicElement =
new WebDriverWait(driver, 10).until(
ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
myDynamicElement.isDisplayed();
public static void waitForElementToAppear(Driver driver, By selector, long timeOutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
}

Categories